代码改变世界

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

2006-05-12 14:16  晓风残月  阅读(3166)  评论(1编辑  收藏  举报
设计了这么一个Repeater:
        <asp:repeater id="Repeater1" runat="server" onitemcommand="Repeater1_ItemCommand" onitemcreated="Repeater1_ItemCreated">
        
<headertemplate>
           
<table border="1">
           
<tr>
            
<td>ItemID</td>
            
<td>ItemName</td>           
            
<td><asp:button id="Button1" runat="server" commandname="Button1" text="Button1" /></td>
           
</tr>           
        
</headertemplate>
        
<itemtemplate>
        
<tr>
            
<td><%Eval("ItemID"%></td>
            
<td><%Eval("ItemName"%></td>
            
<td><asp:linkbutton id="LinkButton1" runat="server" commandname="LinkButton1" text="LinkButton1" /></td>           
        
</tr>
        
</itemtemplate>
        
<footertemplate>
        
</table>
        
</footertemplate>
        
</asp:repeater>

加载数据:
    public DataTable CreateDataTable( int count)
    
{
        DataTable tbl 
= new DataTable();
        tbl.Columns.Add( 
"ItemID"typeof(int) );
        tbl.Columns.Add( 
"ItemName"typeof(string) );            

        DataRow row;  
        
int i = count;
        
while (i-- > 0)
        
{
            row 
= tbl.NewRow();
            row[
0= i;
            row[
1= "Item#" + i.ToString();
            tbl.Rows.Add( row );
        }


        
return tbl;
    }


    
protected void Page_Load(object sender, EventArgs e)
    
{
        
if (!Page.IsPostBack)
        
{
            Repeater1.DataSource 
= CreateDataTable(10);
            Repeater1.DataBind();
        }

    }

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


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

    }

Repeater的ItemCommand事件:
    protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
    
{
        Response.Write( 
"CommandName: " + e.CommandName );
    }

运行结果:

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

注释掉ItemCreated中访问ClientID代码:

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


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

    }

生成的HTML源:

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

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