Followers

Friday, 18 January 2013

GridView TemplateField OnClientClick event -using confirmation message for deleting records

RecordsDeleteConfirmationMessage.aspx

  1. <%@ Page Language="C#" AutoEventWireup="true" %>  
  2. <%@ Import Namespace="System.Drawing" %>  
  3.   
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  5. <script runat="server">  
  6.     void SqlDataSource1_Deleted(object sender, SqlDataSourceStatusEventArgs e)  
  7.     {  
  8.         if (e.Exception == null)  
  9.         {  
  10.             if (e.AffectedRows == 1)  
  11.             {  
  12.                 Label1.Text = "One Record deleted successfully!";  
  13.                 Label1.ForeColor = Color.SeaGreen;  
  14.             }  
  15.             else  
  16.             {  
  17.                 Label1.Text = "An error occured!";  
  18.                 Label1.ForeColor = Color.DeepPink;  
  19.             }  
  20.         }  
  21.         else  
  22.         {  
  23.             Label1.Text = "Error Details: " + e.Exception.Message;  
  24.         }  
  25.     }  
  26. </script>  
  27. <html xmlns="http://www.w3.org/1999/xhtml" >  
  28. <head id="Head1" runat="server">  
  29.     <title>GridView TemplateField OnClientClick event -using confirmation message for deleting records</title>  
  30. </head>  
  31. <body>  
  32.     <form id="form1" runat="server">  
  33.     <div>  
  34.         <h2 style="color:Navy; font-style:italic;">GridView Example: Using TemplateField OnClientClick Event</h2>  
  35.         <!-- Remove relation between Products and OrderDetails tables for test this example -->  
  36.         <asp:SqlDataSource   
  37.             ID="SqlDataSource1"  
  38.             runat="server"  
  39.             ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"  
  40.             SelectCommand="Select ProductID, ProductName, QuantityPerUnit, UnitPrice From products"  
  41.             SelectCommandType="Text"  
  42.             DeleteCommand="Delete From Products Where ProductID=@original_ProductID"  
  43.             OldValuesParameterFormatString="original_{0}"  
  44.             OnDeleted="SqlDataSource1_Deleted"  
  45.             >  
  46.             <SelectParameters>  
  47.                 <asp:Parameter Direction="Output" Name="Count" Type="Int32" />  
  48.             </SelectParameters>  
  49.         </asp:SqlDataSource>  
  50.         <asp:Label   
  51.             ID="Label1"   
  52.             runat="server"  
  53.             Font-Bold="true"  
  54.             Font-Size="Large"  
  55.             Font-Italic="true"  
  56.             ForeColor="SeaGreen"  
  57.             >  
  58.         </asp:Label>  
  59.         <br /><br />  
  60.         <asp:GridView   
  61.             ID="GridView1"  
  62.             runat="server"  
  63.             DataSourceID="SqlDataSource1"  
  64.             AutoGenerateColumns="false"  
  65.             AllowPaging="true"  
  66.             DataKeyNames="ProductID"  
  67.             PageSize="8"  
  68.             BorderColor="Salmon"  
  69.             AutoGenerateDeleteButton="false"  
  70.             Font-Names="Comic Sans MS"  
  71.             Width="850"  
  72.             >  
  73.             <HeaderStyle BackColor="Crimson" ForeColor="Snow" Height="45"/>  
  74.             <RowStyle BackColor="OrangeRed" ForeColor="Snow" Font-Italic="true" />  
  75.             <PagerStyle   
  76.                 Height="45"   
  77.                 HorizontalAlign="Right"   
  78.                 BackColor="BurlyWood"  
  79.                 Font-Bold="true"  
  80.                 Font-Size="X-Large"  
  81.                 ForeColor="Snow"  
  82.                 BorderColor="RosyBrown"  
  83.                 />  
  84.             <PagerSettings Mode="Numeric" />  
  85.             <Columns>  
  86.                 <asp:TemplateField HeaderText="Delete">  
  87.                     <ItemTemplate>  
  88.                         <asp:Button   
  89.                             ID="Button1"  
  90.                             runat="server"  
  91.                             Text="Delete"  
  92.                             CommandName="Delete"  
  93.                             OnClientClick="return confirm('Are you want to delete this record?')"  
  94.                             Height="35"  
  95.                             ForeColor="Crimson"  
  96.                             Font-Bold="true"  
  97.                             />  
  98.                     </ItemTemplate>  
  99.                 </asp:TemplateField>  
  100.                 <asp:BoundField HeaderText="Product ID" DataField="ProductID" />  
  101.                 <asp:BoundField HeaderText="Product Name" DataField="ProductName" />  
  102.                 <asp:BoundField HeaderText="Quantity Per Unit" DataField="QuantityPerUnit" />  
  103.                 <asp:BoundField HeaderText="Unit Price" DataField="UnitPrice"/>  
  104.             </Columns>  
  105.         </asp:GridView>  
  106.     </div>  
  107.     </form>  
  108. </body>  
  109. </html>  






No comments:

Post a Comment