ASP.NET,gridview控件中button按钮的方法怎么写?
我这个gridview是visualstudio生成的,直接和数据库绑定,我通过gridview控件的“编辑列”中,添加“buttonfield”来添加预约按钮,但是不知...
我这个gridview是visual studio生成的,直接和数据库绑定,我通过gridview控件的“编辑列”中,添加“buttonfield”来添加预约按钮,但是不知道在哪里写button的方法?
展开
3个回答
展开全部
button有个CommandName属性,在这里填上点东西,比如说“delete”
然后在gridview的RowCommand事件里写这个
{
if (e.CommandName == "delete")
{你的代码 }
}
然后在gridview的RowCommand事件里写这个
{
if (e.CommandName == "delete")
{你的代码 }
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
aspx 中
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<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"
Width="181px" onrowcommand="GridView1_RowCommand">
<Columns>
<asp:TemplateField HeaderText="列1">
<ItemTemplate>
<div><%#Eval("Count")%></div>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="删除">
<ItemTemplate>
<asp:LinkButton ID="button" runat="server" Text="删除" CommandName="delet" CommandArgument='<%#Eval("count") %>'/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
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<aaa> list = new List<aaa>();
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());
}
}
}
}
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<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"
Width="181px" onrowcommand="GridView1_RowCommand">
<Columns>
<asp:TemplateField HeaderText="列1">
<ItemTemplate>
<div><%#Eval("Count")%></div>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="删除">
<ItemTemplate>
<asp:LinkButton ID="button" runat="server" Text="删除" CommandName="delet" CommandArgument='<%#Eval("count") %>'/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
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<aaa> list = new List<aaa>();
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());
}
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询