Deleted event in SqlDataSource for deleting records.
aspx:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 | <%@ 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.Green; } else { Label1.Text = "An error occured!"; Label1.ForeColor = Color.Red; } } else { Label1.Text = "Error Details: " + e.Exception.Message; } } </script> <html xmlns="http://www.w3.org/1999/xhtml" > <head id="Head1" runat="server"> <title>How to use OnDeleted event in SqlDataSource for deleting records</title> </head> <body> <form id="form1" runat="server"> <div> <h2 style="color:Navy; font-style:italic;">SqlDataSource Example: Using OnDeleted Events</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-Italic="true" Font-Size="Large" ForeColor="SeaGreen" > </asp:Label> <br /><br /> <asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1" AutoGenerateColumns="false" DataKeyNames="ProductID" AllowPaging="true" PageSize="8" BorderColor="Salmon" AutoGenerateDeleteButton="true" 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" /> <PagerSettings Mode="Numeric" /> <Columns> <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> |
Output: