Reputation:
In ASP.NET using C# I want to put a button in every row of Gridview which should perform two actions
Delete the Record of That Row.
Delete an image from folder related to that row.
I can perform the above operations but I want to know how to get the event of the button so that button will work only for the specific row?
Upvotes: 0
Views: 6432
Reputation:
Actual Question Was "I want to know how to get the event of the button so that button will work only for the specific row?"
Answer is Here : HTML
<%@ Page Language="C#" EnableEventValidation="false" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="gridViewDeals.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" >
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="LabelName" runat="server" Text=<%#Eval("Name") %>>></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
C# Code Behind:
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace gridViewDeals
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
SqlConnection con = new SqlConnection("Data Source=HAMMADMAQBOOL;Initial Catalog=ModulesDB;Integrated Security=True;Connect Timeout=15;Encrypt=False;TrustServerCertificate=False");
con.Open();
SqlDataAdapter da = new SqlDataAdapter("Select * From GVDemo", con);
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds.Tables[0];
GridView1.DataBind();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
Button btn = (Button)sender;
GridViewRow gvr = (GridViewRow)btn.NamingContainer;
if (gvr.RowType == DataControlRowType.DataRow)
{
string Namme = (gvr.FindControl("LabelName") as Label).Text;
//Write Query here to Delete Data. . .
//Call Functon Here to Delete the Image From Folder. . .
}
}
}
}
Upvotes: 1
Reputation: 1118
<asp:GridView ID="GridView1" runat="server" OnRowDeleting="GridView1_RowDeleting">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="Button" Text="BUTTON" runat="server" CommandName="Delete" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
Then in the gridview RowDeleting Action,in the code behind method for that, do your logic, it will pull the row in for you.
protected void GridView1_RowDeleting(object sender, GridViewDeletedEventArgs e)
{
//ROW YOU ARE DELETING
int rowindex = e.RowIndex;
//Do your Delete Logic Here
}
Upvotes: 2
Reputation: 414
buttons have a CommandArgument property you can use to store the row id and then get this in code within the on click event with the code
string RowID = (sender as Button).CommandArgument
Upvotes: 0