GridView TemplateField OnClientClick event -using confirmation message for deleting records
RecordsDeleteConfirmationMessage.aspx
- <%@ Page Language="C#" AutoEventWireup="true" %>
- <%@ Import Namespace="System.Drawing" %>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <script runat="server">
- void SqlDataSource1_Deleted(object sender, SqlDataSourceStatusEventArgs e)
- {
- if (e.Exception == null)
- {
- if (e.AffectedRows == 1)
- {
- Label1.Text = "One Record deleted successfully!";
- Label1.ForeColor = Color.SeaGreen;
- }
- else
- {
- Label1.Text = "An error occured!";
- Label1.ForeColor = Color.DeepPink;
- }
- }
- else
- {
- Label1.Text = "Error Details: " + e.Exception.Message;
- }
- }
- </script>
- <html xmlns="http://www.w3.org/1999/xhtml" >
- <head id="Head1" runat="server">
- <title>GridView TemplateField OnClientClick event -using confirmation message for deleting records</title>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- <h2 style="color:Navy; font-style:italic;">GridView Example: Using TemplateField OnClientClick Event</h2>
- <!-- Remove relation between Products and OrderDetails tables for test this example -->
- <asp:SqlDataSource
- ID="SqlDataSource1"
- runat="server"
- ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
- SelectCommand="Select ProductID, ProductName, QuantityPerUnit, UnitPrice From products"
- SelectCommandType="Text"
- DeleteCommand="Delete From Products Where ProductID=@original_ProductID"
- OldValuesParameterFormatString="original_{0}"
- OnDeleted="SqlDataSource1_Deleted"
- >
- <SelectParameters>
- <asp:Parameter Direction="Output" Name="Count" Type="Int32" />
- </SelectParameters>
- </asp:SqlDataSource>
- <asp:Label
- ID="Label1"
- runat="server"
- Font-Bold="true"
- Font-Size="Large"
- Font-Italic="true"
- ForeColor="SeaGreen"
- >
- </asp:Label>
- <br /><br />
- <asp:GridView
- ID="GridView1"
- runat="server"
- DataSourceID="SqlDataSource1"
- AutoGenerateColumns="false"
- AllowPaging="true"
- DataKeyNames="ProductID"
- PageSize="8"
- BorderColor="Salmon"
- AutoGenerateDeleteButton="false"
- Font-Names="Comic Sans MS"
- Width="850"
- >
- <HeaderStyle BackColor="Crimson" ForeColor="Snow" Height="45"/>
- <RowStyle BackColor="OrangeRed" ForeColor="Snow" Font-Italic="true" />
- <PagerStyle
- Height="45"
- HorizontalAlign="Right"
- BackColor="BurlyWood"
- Font-Bold="true"
- Font-Size="X-Large"
- ForeColor="Snow"
- BorderColor="RosyBrown"
- />
- <PagerSettings Mode="Numeric" />
- <Columns>
- <asp:TemplateField HeaderText="Delete">
- <ItemTemplate>
- <asp:Button
- ID="Button1"
- runat="server"
- Text="Delete"
- CommandName="Delete"
- OnClientClick="return confirm('Are you want to delete this record?')"
- Height="35"
- ForeColor="Crimson"
- Font-Bold="true"
- />
- </ItemTemplate>
- </asp:TemplateField>
- <asp:BoundField HeaderText="Product ID" DataField="ProductID" />
- <asp:BoundField HeaderText="Product Name" DataField="ProductName" />
- <asp:BoundField HeaderText="Quantity Per Unit" DataField="QuantityPerUnit" />
- <asp:BoundField HeaderText="Unit Price" DataField="UnitPrice"/>
- </Columns>
- </asp:GridView>
- </div>
- </form>
- </body>
- </html>
No comments:
Post a Comment