Default.aspx------------------------------------<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" EnableEventValidation="false" %><!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:MultiView ID="MultiView1" runat="server" ActiveViewIndex="0"> <asp:View runat="server" ID="View0"> <asp:GridView ID="gv" runat="server" AutoGenerateColumns="False" DataKeyNames="ID" OnRowDeleting="gv_RowDeleting" OnRowCommand="gv_RowCommand"> <Columns> <asp:BoundField DataField="ID" HeaderText="编号" /> <asp:BoundField DataField="Name" HeaderText="姓名" /> <asp:BoundField DataField="Age" HeaderText="年龄" /> <asp:TemplateField HeaderText="编辑"> <ItemTemplate> <asp:Button ID="gv1A" runat="server" CommandName="Update" Text="Update" /> <asp:Button ID="gv1B" runat="server" CommandName="Delete" Text="Delete" /> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> <br /> <asp:Label ID="lb" runat="server" Text="Label"></asp:Label> </asp:View> </asp:MultiView> </div> </form></body></html>Default.aspx.cs-------------------------------using System;using System.Collections.Generic;using System.Web.UI.WebControls;public partial class _Default : System.Web.UI.Page{ protected void Page_Load(object sender, EventArgs e) { IList<People> list = new List<People>(); list.Add(new People(0, "张三", 21)); list.Add(new People(1, "李四", 15)); list.Add(new People(2, "王五", 56)); gv.DataSource = list; gv.DataBind(); } protected void gv_RowDeleting(object sender, GridViewDeleteEventArgs e) { GridView gv = View0.FindControl("gv") as GridView; lb.Text = gv.DataKeyNames[e.RowIndex]; } protected void gv_RowCommand(object sender, GridViewCommandEventArgs e) { lb.Text = e.CommandName; }}public class People{ public int ID { get; set; } public int Age { get; set; } public string Name { get; set; } public People(int id, string name, int age) { ID = id; Name = name; Age = age; }}
-----------------------
以上代码我想求解的是在GridView中的两个按钮、不管我怎么点都没反应,我是指在GridView的事件中、、 十分痛苦,上Google百度了很久仍无头绪。希望在这里能获得解疑!
4 回答
潇潇雨雨
TA贡献1833条经验 获得超4个赞
把Page_Load中的GridView绑定代码放在!IsPostBack内!
if (!IsPostBack) //事件回发过来不执行,不然每次单击按钮都重新绑定一次!
{
IList<People> list = new List<People>();
list.Add(new People(0, "张三", 21));
list.Add(new People(1, "李四", 15));
list.Add(new People(2, "王五", 56));
gv.DataSource = list;
gv.DataBind();
}
- 4 回答
- 0 关注
- 310 浏览
添加回答
举报
0/150
提交
取消