Description:-
Now you can check UHConnectionString is coming from web.config File there one Connection String File will be Added and Name whatever you can set. And in link button create OnClientClick event for deleting row with Confirmation. If user click YES then record should be deleted other wise not.
Now Generate Gridview RowDatabound event from Gridview Controls events and Code for delete file from Database.
Now run your Webpage and delete any rows from Gridview then ask for deleting click Yes to delete if No then file will not delete from Database.
In this example we will see how
to delete records from database using link button and in Gridview RowDatabound
event. In RowDatabound we will get Row Id and using that Row Id we will delete
record from Database using DataSource. Bind Gridview using SQL DataSource Bind
table and add Link button in end of that Gridview Column so we can check column
and delete particular row from database. And with Confirmation dialog we will
delete this record from data base.
Now Create Webpage and Drag
Gridview Controls. Bind Gridview using SQL DataSource from the SQL Server.
First design your Gridview like below.
<div> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" AllowSorting="True" DataSourceID="SqlDataSource1" AllowPaging="True" DataKeyNames="stor_id" OnRowDataBound="GridView1_RowDataBound" OnRowDeleted="GridView1_RowDeleted" OnRowDeleting="GridView1_RowDeleting" BackColor="White" BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px" CellPadding="3" ForeColor="Black" GridLines="Vertical"> <AlternatingRowStyle BackColor="#CCCCCC" /> <Columns> <asp:BoundField ReadOnly="True" HeaderText="Store Id" DataField="stor_id" SortExpression="stor_id"> </asp:BoundField> <asp:BoundField HeaderText="Store Name" DataField="stor_name" SortExpression="stor_name"> </asp:BoundField> <asp:BoundField HeaderText="Store Address" DataField="stor_address" SortExpression="stor_address"> </asp:BoundField> <asp:BoundField HeaderText="City" DataField="city" SortExpression="city"></asp:BoundField> <asp:BoundField HeaderText="State" DataField="state" SortExpression="state"></asp:BoundField> <asp:BoundField HeaderText="ZIP" DataField="zip" SortExpression="zip"></asp:BoundField> <asp:TemplateField> <ItemTemplate> <asp:LinkButton ID="DeleteBtn" runat="server" CommandName="Delete" OnClientClick="return isDelete();">Delete </asp:LinkButton> </ItemTemplate> </asp:TemplateField> </Columns> <FooterStyle BackColor="#CCCCCC" /> <HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" /> <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" /> <SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" /> <SortedAscendingCellStyle BackColor="#F1F1F1" /> <SortedAscendingHeaderStyle BackColor="#808080" /> <SortedDescendingCellStyle BackColor="#CAC9C9" /> <SortedDescendingHeaderStyle BackColor="#383838" /> </asp:GridView> <div> <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:UHConnectionString %>" SelectCommand="SELECT * FROM [stores]" DeleteCommand="DELETE From [stores] WHERE [stor_id] = @stor_id"> </asp:SqlDataSource> </div> </div>
Now you can check UHConnectionString is coming from web.config File there one Connection String File will be Added and Name whatever you can set. And in link button create OnClientClick event for deleting row with Confirmation. If user click YES then record should be deleted other wise not.
Now create Script file for
Confirmation dialog box so when we delete from Gridview then we need
confirmation Dialog box.
<script type="text/javascript"> function isDelete() { return confirm("Do you want to delete this row ?"); } </script>
Now Generate Gridview RowDatabound event from Gridview Controls events and Code for delete file from Database.
Protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { string cellID = e.Row.Cells[0].Text; LinkButton deleteButton = (LinkButton)e.Row.Cells[6].FindControl("DeleteBtn"); if (deleteButton != null) { deleteButton.Attributes.Add("onclick", "return isDelete();"); } } }
Now run your Webpage and delete any rows from Gridview then ask for deleting click Yes to delete if No then file will not delete from Database.
Thanks for comments.....