Repeater 为什么不激发 ItemCommand事件了?

设计了这么一个Repeater:
None.gif          < asp:repeater  id ="Repeater1"  runat ="server"  onitemcommand ="Repeater1_ItemCommand"  onitemcreated ="Repeater1_ItemCreated" >
None.gif        
< headertemplate >
None.gif           
< table  border ="1" >
None.gif           
< tr >
None.gif            
< td > ItemID </ td >
None.gif            
< td > ItemName </ td >            
None.gif            
< td >< asp:button  id ="Button1"  runat ="server"  commandname ="Button1"  text ="Button1"   /></ td >
None.gif           
</ tr >            
None.gif        
</ headertemplate >
None.gif        
< itemtemplate >
None.gif        
< tr >
ExpandedBlockStart.gifContractedBlock.gif            
< td > <% dot.gif Eval("ItemID" %> </ td >
ExpandedBlockStart.gifContractedBlock.gif            
< td > <% dot.gif Eval("ItemName" %> </ td >
None.gif            
< td >< asp:linkbutton  id ="LinkButton1"  runat ="server"  commandname ="LinkButton1"  text ="LinkButton1"   /></ td >            
None.gif        
</ tr >
None.gif        
</ itemtemplate >
None.gif        
< footertemplate >
None.gif        
</ table >
None.gif        
</ footertemplate >
None.gif        
</ asp:repeater >

加载数据:
None.gif      public  DataTable CreateDataTable(  int  count)
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        DataTable tbl 
= new DataTable();
InBlock.gif        tbl.Columns.Add( 
"ItemID"typeof(int) );
InBlock.gif        tbl.Columns.Add( 
"ItemName"typeof(string) );            
InBlock.gif
InBlock.gif        DataRow row;  
InBlock.gif        
int i = count;
InBlock.gif        
while (i-- > 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            row 
= tbl.NewRow();
InBlock.gif            row[
0= i;
InBlock.gif            row[
1= "Item#" + i.ToString();
InBlock.gif            tbl.Rows.Add( row );
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
return tbl;
ExpandedBlockEnd.gif    }

None.gif
None.gif    
protected   void  Page_Load( object  sender, EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        
if (!Page.IsPostBack)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Repeater1.DataSource 
= CreateDataTable(10);
InBlock.gif            Repeater1.DataBind();
ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif    }

Repeate的ItemCreated事件:
None.gif      protected   void  Repeater1_ItemCreated( object  sender, RepeaterItemEventArgs e)
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        Button btn1 
= e.Item.FindControl( "Button1" ) as Button;
InBlock.gif        
if (btn1 != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
// do nothing, just to access the ClientID owing to the button 
InBlock.gif            
//
InBlock.gif
            string temp = btn1.ClientID;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        LinkButton lnkbtn1 
= e.Item.FindControl( "LinkButton1" ) as LinkButton;
InBlock.gif        
if (lnkbtn1 != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
// do nothing, just to access the ClientID owing to the linkbutton 
InBlock.gif            
//
InBlock.gif
            string temp = lnkbtn1.ClientID;
ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif    }

Repeater的ItemCommand事件:
None.gif      protected   void  Repeater1_ItemCommand( object  source, RepeaterCommandEventArgs e)
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        Response.Write( 
"CommandName: " + e.CommandName );
ExpandedBlockEnd.gif    }

运行结果:

Problem:
然而,非常奇怪的是 HeaderTempate里面的Button和ItemTemplate里面的LinkButton都不会激发Repeater的ItemCommand了。
经过多次的尝试,发现,只要在ItemCreated访问了 内嵌的 Control的ClientID,改Control的ClientID就不会改变了,即使父控控件实现了INamingContainer接口。
查看页面生成的HTML源:
None.gif < table  border ="1" >
None.gif           
< tr >
None.gif            
< td > ItemID </ td >
None.gif            
< td > ItemName </ td >            
None.gif            
< td >< input  type ="submit"  name ="Button1"  value ="Button1"  id ="Button1"   /></ td >
None.gif           
</ tr >            
None.gif        
None.gif        
< tr >
None.gif            
< td > 9 </ td >
None.gif            
< td > Item#9 </ td >
None.gif            
< td >< id ="LinkButton1"  href ="javascript:__doPostBack('LinkButton1','')" > LinkButton1 </ a ></ td >            
None.gif        
</ tr >
None.gif        
None.gif        
< tr >
None.gif            
< td > 8 </ td >
None.gif            
< td > Item#8 </ td >
None.gif            
< td >< id ="LinkButton1"  href ="javascript:__doPostBack('LinkButton1','')" > LinkButton1 </ a ></ td >            
None.gif        
</ tr >

注释掉ItemCreated中访问ClientID代码:

None.gif      protected   void  Repeater1_ItemCreated( object  sender, RepeaterItemEventArgs e)
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        Button btn1 
= e.Item.FindControl( "Button1" ) as Button;
InBlock.gif        
if (btn1 != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
// do nothing, just to access the ClientID owing to the button 
InBlock.gif            
//
InBlock.gif            
//string temp = btn1.ClientID;  // comment this
ExpandedSubBlockEnd.gif
        }

InBlock.gif
InBlock.gif        LinkButton lnkbtn1 
= e.Item.FindControl( "LinkButton1" ) as LinkButton;
InBlock.gif        
if (lnkbtn1 != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
// do nothing, just to access the ClientID owing to the linkbutton 
InBlock.gif            
//
InBlock.gif            
//string temp = lnkbtn1.ClientID;  // comment this
ExpandedSubBlockEnd.gif
        }

ExpandedBlockEnd.gif    }

生成的HTML源:

None.gif < table  border ="1" >
None.gif           
< tr >
None.gif            
< td > ItemID </ td >
None.gif            
< td > ItemName </ td >            
None.gif            
< td >< input  type ="submit"  name ="Repeater1:_ctl0:Button1"  value ="Button1"  id ="Repeater1__ctl0_Button1"   /></ td >
None.gif           
</ tr >            
None.gif        
None.gif        
< tr >
None.gif            
< td > 9 </ td >
None.gif            
< td > Item#9 </ td >
None.gif            
< td >< id ="Repeater1__ctl1_LinkButton1"  href ="javascript:__doPostBack('Repeater1$_ctl1$LinkButton1','')" > LinkButton1 </ a ></ td >            
None.gif        
</ tr >
None.gif        
None.gif        
< tr >
None.gif            
< td > 8 </ td >
None.gif            
< td > Item#8 </ td >
None.gif            
< td >< id ="Repeater1__ctl2_LinkButton1"  href ="javascript:__doPostBack('Repeater1$_ctl2$LinkButton1','')" > LinkButton1 </ a ></ td >            
None.gif        
</ tr >

对比一下发现,Button的客户端ID不一样,猜想下原因可能是由于 ClientID不符合NameContainer规则,PostBack以后无法找到事件控件源。
Any Solutions?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值