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

时间:2015-07-31 16:12:29

标签: c# asp.net gridview

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

  1. 删除该行的记录。

  2. 从与该行相关的文件夹中删除图像。

  3. 我可以执行上述操作,但我想知道如何获取按钮事件,以便该按钮仅适用于特定行?

3 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

按钮具有CommandArgument属性,您可以使用该属性存储行ID,然后使用代码

在单击事件中的代码中获取此属性
string RowID = (sender as Button).CommandArgument

答案 2 :(得分:0)

实际问题&#34;我想知道如何获取按钮事件,以便该按钮仅适用于特定行?&#34;

答案在这里: 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. . . 
            }

        }


    }
}