繁体   English   中英

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

[英]Button in every Row of Gridview C# Asp.NET

在使用C#的ASP.NET中,我想在Gridview的每一行中放置一个按钮,该按钮应执行两个操作

  1. 删除该行的记录。

  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>

然后在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

实际问题是“我想知道如何获取按钮的事件,以便该按钮仅适用于特定行吗?”

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

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