设计了这么一个Repeater:
Repeater 为什么不激发 ItemCommand事件了?        <asp:repeater id="Repeater1" runat="server" onitemcommand="Repeater1_ItemCommand" onitemcreated="Repeater1_ItemCreated">
Repeater 为什么不激发 ItemCommand事件了?        
<headertemplate>
Repeater 为什么不激发 ItemCommand事件了?           
<table border="1">
Repeater 为什么不激发 ItemCommand事件了?           
<tr>
Repeater 为什么不激发 ItemCommand事件了?            
<td>ItemID</td>
Repeater 为什么不激发 ItemCommand事件了?            
<td>ItemName</td>           
Repeater 为什么不激发 ItemCommand事件了?            
<td><asp:button id="Button1" runat="server" commandname="Button1" text="Button1" /></td>
Repeater 为什么不激发 ItemCommand事件了?           
</tr>           
Repeater 为什么不激发 ItemCommand事件了?        
</headertemplate>
Repeater 为什么不激发 ItemCommand事件了?        
<itemtemplate>
Repeater 为什么不激发 ItemCommand事件了?        
<tr>
>

加载数据:
Repeater 为什么不激发 ItemCommand事件了?    public DataTable CreateDataTable( int count)
    }

Repeate的ItemCreated事件:
Repeater 为什么不激发 ItemCommand事件了?    protected void Repeater1_ItemCreated(object sender, RepeaterItemEventArgs e)
    }

Repeater的ItemCommand事件:
Repeater 为什么不激发 ItemCommand事件了?    protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
    }

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

注释掉ItemCreated中访问ClientID代码:

Repeater 为什么不激发 ItemCommand事件了?    protected void Repeater1_ItemCreated(object sender, RepeaterItemEventArgs e)
    }

生成的HTML源:

Repeater 为什么不激发 ItemCommand事件了?<table border="1">
Repeater 为什么不激发 ItemCommand事件了?           
<tr>
Repeater 为什么不激发 ItemCommand事件了?            
<td>ItemID</td>
Repeater 为什么不激发 ItemCommand事件了?            
<td>ItemName</td>           
Repeater 为什么不激发 ItemCommand事件了?            
<td><input type="submit" name="Repeater1:_ctl0:Button1" value="Button1" id="Repeater1__ctl0_Button1" /></td>
Repeater 为什么不激发 ItemCommand事件了?           
</tr>           
Repeater 为什么不激发 ItemCommand事件了?        
Repeater 为什么不激发 ItemCommand事件了?        
<tr>
Repeater 为什么不激发 ItemCommand事件了?            
<td>9</td>
Repeater 为什么不激发 ItemCommand事件了?            
<td>Item#9</td>
Repeater 为什么不激发 ItemCommand事件了?            
<td><id="Repeater1__ctl1_LinkButton1" href="javascript:__doPostBack('Repeater1$_ctl1$LinkButton1','')">LinkButton1</a></td>           
Repeater 为什么不激发 ItemCommand事件了?        
</tr>
Repeater 为什么不激发 ItemCommand事件了?        
Repeater 为什么不激发 ItemCommand事件了?        
<tr>
Repeater 为什么不激发 ItemCommand事件了?            
<td>8</td>
Repeater 为什么不激发 ItemCommand事件了?            
<td>Item#8</td>
Repeater 为什么不激发 ItemCommand事件了?            
<td><id="Repeater1__ctl2_LinkButton1" href="javascript:__doPostBack('Repeater1$_ctl2$LinkButton1','')">LinkButton1</a></td>           
Repeater 为什么不激发 ItemCommand事件了?        
</tr>

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

相关文章:

  • 2022-12-23
  • 2021-09-22
  • 2022-02-12
  • 2022-12-23
  • 2022-12-23
  • 2019-02-16
猜你喜欢
  • 2022-01-18
  • 2022-12-23
  • 2022-02-11
  • 2022-12-23
  • 2022-12-23
  • 2021-09-19
  • 2022-12-23
相关资源
相似解决方案