ASP.NET,gridview控件中button按钮的方法怎么写?

2025-04-11 08:05:25
推荐回答(3个)
回答(1):

选中GridView控件,然后选中属性工具中的时间按钮(图标是一个闪电),然后找到RowCommand
,然后双击,自动生成了RowCommand事件。

然后再代码中写

switch(e.CommandName )
{
case "edit":
你的代码
break;

case "delete":
你的代码
break;

}

回答(2):

button有个CommandName属性,在这里填上点东西,比如说“delete”
然后在gridview的RowCommand事件里写这个
{
if (e.CommandName == "delete")
{你的代码 }
}

回答(3):

aspx 中
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>










Width="181px" onrowcommand="GridView1_RowCommand">



<%#Eval("Count")%>





'/>









cs中
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication1
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
List list = new List();
for (int i = 0; i < 20; i++)
{
aaa a = new aaa();
a.Count = i;
list.Add(a);
}
this.GridView1.DataSource = list;
this.DataBind();

}

public class aaa
{
private int count;
public int Count
{
get { return count; }
set { count = value; }
}
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "delet") {
Response.Write("当前"+e.CommandArgument.ToString());
}
}
}
}