0

In ASP.NET using C# I want to put a button in every row of Gridview which should perform two actions

  1. Delete the Record of That Row.

  2. 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?

3 Answers 3

2
<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
    }
Sign up to request clarification or add additional context in comments.

Comments

1

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. . . 
            }

        }


    }
}

1 Comment

Dylan's answer actually more efficient and easier to maintain. Yours, while it may be more abstract, still only works with GridView button clicks they way you currently have it, but doesn't take advantage of GridViewEventArgs. That would keep you from having to cast the container and row type. Also, if you are going to quote "the actual question was..." you should probably not remove "The actual question" (as you call it) from your original post.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.