关于 Repeater 控件嵌套的使用。在嵌套中添加 其它控件(如:按钮),并影响其它控件的方法,很重要哦,测试通过。

这里的Repeater 嵌套,是指在 外层的Repeater的 ItemTemplate 中 又添加了一个 Repeater ,并且在两层的Repeater中都添加了一个Button控件。我们要实现两个控件的事件响应。也就是点两个Button时执行相应的方法。
看 .aspx 代码:
1 ="Repeater1_ItemDataBound" 3 _disibledevent=>="Repeater1_ItemCommand"> 4 // 这是外层的Repeater。注意看最后一行。Onitemcommand 事件 5 6 7 8 9 10 11 12 15 16 17 18 19 20 21 22
<%# Eval("UserName") %><%# Eval("Title") %>
13 //这是外层的Button。 14
<%# Eval("cont") %>
&nbsp; 23 ="Repeater2_ItemCommand"> 24 //这里嵌套到里面的第二Repeater。在这个控件中,也有一个Onitemcommand 事件的处理。 25 26 27 28 29 30 31 32 35 36 37 38 39 40 41 42 43
<%# Eval("UserName") %><%# Eval("Title") %>
33 // 这是内层的 Button。 34
<%# Eval("Reply") %>
44 45

我们就是依靠 这两个 Onitemcommand 事件来执行代码的。
来看一下, .cs 代码
1 protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e) 2 //在这里处理第1个Button事件,因为这个Button位于第一个 Repeater中。 3 //在这里是不能捕捉到第二个Repeater控件中的Button事件的。 4 { 5 if (e.CommandName == "deletely") 6 { 7 int dlid = Convert.ToInt32(e.CommandArgument.ToString()); 8 opclass opc = new opclass(); 9 opc.ExectCmd(string.Format("Delete From [liuyan] Where ID={0}", dlid)); 10 opclass opc1 = new opclass(); 11 opc1.ExectCmd(string.Format("Delete From [Reply] Where lyID={0}", dlid)); 12 bangdingdata(); 13 14 } 15 } 16 17 protected void Repeater2_ItemCommand(object source, RepeaterCommandEventArgs e) 18 //在这里处理第2个Button事件,因为这个Button位于第二个 Repeater中。 19 { 20 if (e.CommandName == "deletehf") 21 { 22 int dlid = Convert.ToInt32(e.CommandArgument.ToString()); 23 opclass opc = new opclass(); 24 opc.ExectCmd(string.Format("Delete From [Reply] Where ID={0}", dlid)); 25 bangdingdata(); 26 27 28 } 29 30 }
提示:
点击外层的 Repeater 控件的时候,在属性对话框中,可以找到 _disibledevent=>
Tags: 

延伸阅读

最新评论

发表评论