首页 新闻 会员 周边 捐助

在MultiView中GridView的事件

0
[已解决问题] 解决于 2010-08-07 15:51
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百度了很久仍无头绪。希望在这里能获得解疑!

 

Κ′的主页 Κ′ | 初学一级 | 园豆:200
提问于:2010-08-06 22:55
< >
分享
最佳答案
0

把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();
}

Astar | 高人七级 |园豆:40805 | 2010-08-07 08:17
太谢谢你了!现在我有点理解了!
Κ′ | 园豆:200 (初学一级) | 2010-08-07 15:49
其他回答(2)
0

写错了吧

 

protected void gv_RowCommand(object sender, GridViewCommandEventArgs e)
{
 
     if (e.CommandName.ToUpper() == "DELETE")
{
        //do something
}
else if(e.CommandName.ToUpper() == "UPDATE")
{
        //do something
}

 
}

 

 这样试下,我觉得你上面写的好像有问题

jowo | 园豆:2834 (老鸟四级) | 2010-08-07 09:33
也谢谢你的回答、虽然你并没有回答出我问题的难点,但也十分感谢你的热心参与!!
支持(0) 反对(0) Κ′ | 园豆:200 (初学一级) | 2010-08-07 15:51
0

我试了下,直接使用你的代码,我这边gv_RowCommand事件可以调用到,同时e.CommandName也可以获取到。

你可以检查下是不是其他地方出了问题。

kyo-yo | 园豆:5587 (大侠五级) | 2010-08-07 15:40
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册