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

这里的Repeater 嵌套,是指在 外层的Repeater的 ItemTemplate 中 又添加了一个 Repeater ,并且在两层的Repeater中都添加了一个Button控件。我们要实现两个控件的事件响应。也就是点两个Button时执行相应的方法。

看 .aspx 代码:

 
  
1 < asp:Repeater ID = " Repeater1 " runat = " server "
2 onitemdatabound = " Repeater1_ItemDataBound "
3 onitemcommand = " Repeater1_ItemCommand " >
4   // 这是外层的Repeater。注意看最后一行。Onitemcommand 事件
5   < HeaderTemplate >
6   < table width = " 500 " border = " 0 " cellspacing = " 3 " cellpadding = " 0 " >
7 </ HeaderTemplate >
8
9 < ItemTemplate >
10 < tr >
11 < td width = " 72 " height = " 30 " bgcolor = " #669966 " ><% # Eval( " UserName " ) %></ td >
12 < td height = " 30 " bgcolor = " #669966 " ><% # Eval( " Title " ) %>< div style = " text-align: right; clear: right " >< asp:Button Text = " 删除留言 " runat = " server " CommandName = " deletely " CommandArgument = ' <%# Eval("ID") %> ' />
13   // 这是外层的Button。
14 </ div ></ td >
15 </ tr >
16 < tr >
17 < td height = " 30 " ></ td >
18 < td height = " 30 " bgcolor = " #99FFCC " ><% # Eval( " cont " ) %></ td >
19 </ tr >
20 < tr >
21 < td height = " 30 " >& nbsp; </ td >
22 < td height = " 30 " >
23 < asp:Repeater ID = " Repeater2 " runat = " server " onitemcommand = " Repeater2_ItemCommand " >
24 // 这里嵌套到里面的第二Repeater。在这个控件中,也有一个Onitemcommand 事件的处理。
25 < HeaderTemplate >
26 < table width = " 415 " border = " 0 " cellspacing = " 3 " cellpadding = " 0 " >
27 </ HeaderTemplate >
28 < ItemTemplate >
29
30 < tr >
31 < td width = " 20% " height = " 30 " bgcolor = " #0099FF " ><% # Eval( " UserName " ) %></ td >
32 < td height = " 30 " bgcolor = " #0099FF " ><% # Eval( " Title " ) %>< div style = " text-align: right; clear: right " >< asp:Button ID = " Button1 " Text = " 删除回复 " runat = " server " CommandName = " deletehf " CommandArgument = ' <%# Eval("ID") %> ' />
33 // 这是内层的 Button。
34 </ div ></ td >
35 </ tr >
36 < tr >
37 < td height = " 30 " colspan = " 2 " ><% # Eval( " Reply " ) %></ td >
38 </ tr >
39
40 </ ItemTemplate >
41
42 < FooterTemplate >
43 </ table >
44 </ FooterTemplate >
45 </ asp:Repeater >

我们就是依靠 这两个 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 控件的时候,在属性对话框中,可以找到 OnitemCommand 事件,双击可以自动创造出对应的代码。

(也就是可以在属性对话框中点击那个小闪电符号找到对应的事件)但是当点击内层的Repeater的时候,是没有那个小闪电的。所以,在第二个

Repeater 中的 OnitemCommand事件只能手动添加上去,要在.aspx文件和.cs文件中分别添加对应的代码才行。添加完成后,程序会执行对应的代码。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值