简体   繁体   中英

Button in every Row of Gridview C# Asp.NET

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?

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

按钮具有CommandArgument属性,可用于存储行ID,然后在带有代码的on click事件内的代码中获取该ID

string RowID = (sender as Button).CommandArgument

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

        }


    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM