简体   繁体   English

Gridview C#Asp.NET每行中的按钮

[英]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 在使用C#的ASP.NET中,我想在Gridview的每一行中放置一个按钮,该按钮应执行两个操作

  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. 然后在gridview RowDeleting Action中,在该方法后面的代码中,执行您的逻辑,它将为您拉入行。

 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 答案在这里: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: 后面的C#代码:

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

        }


    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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