Repeater 为什么不激发 ItemCommand事件了?
2006-05-12 14:16 晓风残月 阅读(3180) 评论(1) 收藏 举报
设计了这么一个Repeater:
 <asp:repeater id="Repeater1" runat="server" onitemcommand="Repeater1_ItemCommand" onitemcreated="Repeater1_ItemCreated">
        <asp:repeater id="Repeater1" runat="server" onitemcommand="Repeater1_ItemCommand" onitemcreated="Repeater1_ItemCreated">
 <headertemplate>
        <headertemplate>
 <table border="1">
           <table border="1">
 <tr>
           <tr>
 <td>ItemID</td>
            <td>ItemID</td>
 <td>ItemName</td>
            <td>ItemName</td>           
 <td><asp:button id="Button1" runat="server" commandname="Button1" text="Button1" /></td>
            <td><asp:button id="Button1" runat="server" commandname="Button1" text="Button1" /></td>
 </tr>
           </tr>           
 </headertemplate>
        </headertemplate>
 <itemtemplate>
        <itemtemplate>
 <tr>
        <tr>
 <td><%# Eval("ItemID") %></td>
            <td><%# Eval("ItemID") %></td>
 <td><%# Eval("ItemName") %></td>
            <td><%# Eval("ItemName") %></td>
 <td><asp:linkbutton id="LinkButton1" runat="server" commandname="LinkButton1" text="LinkButton1" /></td>
            <td><asp:linkbutton id="LinkButton1" runat="server" commandname="LinkButton1" text="LinkButton1" /></td>           
 </tr>
        </tr>
 </itemtemplate>
        </itemtemplate>
 <footertemplate>
        <footertemplate>
 </table>
        </table>
 </footertemplate>
        </footertemplate>
 </asp:repeater>
        </asp:repeater>
加载数据:
 public DataTable CreateDataTable( int count)
    public DataTable CreateDataTable( int count)
 {
    {
 DataTable tbl = new DataTable();
        DataTable tbl = new DataTable();
 tbl.Columns.Add( "ItemID", typeof(int) );
        tbl.Columns.Add( "ItemID", typeof(int) );
 tbl.Columns.Add( "ItemName", typeof(string) );
        tbl.Columns.Add( "ItemName", typeof(string) );            

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

 return tbl;
        return tbl;
 }
    }

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

 LinkButton lnkbtn1 = e.Item.FindControl( "LinkButton1" ) as LinkButton;
        LinkButton lnkbtn1 = e.Item.FindControl( "LinkButton1" ) as LinkButton;
 if (lnkbtn1 != null)
        if (lnkbtn1 != null)
 {
        {
 // do nothing, just to access the ClientID owing to the linkbutton
            // do nothing, just to access the ClientID owing to the linkbutton 
 //
            //
 string temp = lnkbtn1.ClientID;
            string temp = lnkbtn1.ClientID;
 }
        }
 }
    }
Repeater的ItemCommand事件:
 protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
    protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
 {
    {
 Response.Write( "CommandName: " + e.CommandName );
        Response.Write( "CommandName: " + e.CommandName );
 }
    }
运行结果:

Problem:
然而,非常奇怪的是 HeaderTempate里面的Button和ItemTemplate里面的LinkButton都不会激发Repeater的ItemCommand了。
经过多次的尝试,发现,只要在ItemCreated访问了 内嵌的 Control的ClientID,改Control的ClientID就不会改变了,即使父控控件实现了INamingContainer接口。
查看页面生成的HTML源:
 <table border="1">
<table border="1">
 <tr>
           <tr>
 <td>ItemID</td>
            <td>ItemID</td>
 <td>ItemName</td>
            <td>ItemName</td>           
 <td><input type="submit" name="Button1" value="Button1" id="Button1" /></td>
            <td><input type="submit" name="Button1" value="Button1" id="Button1" /></td>
 </tr>
           </tr>           
 
        
 <tr>
        <tr>
 <td>9</td>
            <td>9</td>
 <td>Item#9</td>
            <td>Item#9</td>
 <td><a id="LinkButton1" href="javascript:__doPostBack('LinkButton1','')">LinkButton1</a></td>
            <td><a id="LinkButton1" href="javascript:__doPostBack('LinkButton1','')">LinkButton1</a></td>           
 </tr>
        </tr>
 
        
 <tr>
        <tr>
 <td>8</td>
            <td>8</td>
 <td>Item#8</td>
            <td>Item#8</td>
 <td><a id="LinkButton1" href="javascript:__doPostBack('LinkButton1','')">LinkButton1</a></td>
            <td><a id="LinkButton1" href="javascript:__doPostBack('LinkButton1','')">LinkButton1</a></td>           
 </tr>
        </tr>
 protected void Repeater1_ItemCreated(object sender, RepeaterItemEventArgs e)
    protected void Repeater1_ItemCreated(object sender, RepeaterItemEventArgs e)
 {
    {
 Button btn1 = e.Item.FindControl( "Button1" ) as Button;
        Button btn1 = e.Item.FindControl( "Button1" ) as Button;
 if (btn1 != null)
        if (btn1 != null)
 {
        {
 // do nothing, just to access the ClientID owing to the button
            // do nothing, just to access the ClientID owing to the button 
 //
            //
 //string temp = btn1.ClientID;  // comment this
            //string temp = btn1.ClientID;  // comment this
 }
        }

 LinkButton lnkbtn1 = e.Item.FindControl( "LinkButton1" ) as LinkButton;
        LinkButton lnkbtn1 = e.Item.FindControl( "LinkButton1" ) as LinkButton;
 if (lnkbtn1 != null)
        if (lnkbtn1 != null)
 {
        {
 // do nothing, just to access the ClientID owing to the linkbutton
            // do nothing, just to access the ClientID owing to the linkbutton 
 //
            //
 //string temp = lnkbtn1.ClientID;  // comment this
            //string temp = lnkbtn1.ClientID;  // comment this
 }
        }
 }
    }
 <table border="1">
<table border="1">
 <tr>
           <tr>
 <td>ItemID</td>
            <td>ItemID</td>
 <td>ItemName</td>
            <td>ItemName</td>           
 <td><input type="submit" name="Repeater1:_ctl0:Button1" value="Button1" id="Repeater1__ctl0_Button1" /></td>
            <td><input type="submit" name="Repeater1:_ctl0:Button1" value="Button1" id="Repeater1__ctl0_Button1" /></td>
 </tr>
           </tr>           
 
        
 <tr>
        <tr>
 <td>9</td>
            <td>9</td>
 <td>Item#9</td>
            <td>Item#9</td>
 <td><a id="Repeater1__ctl1_LinkButton1" href="javascript:__doPostBack('Repeater1$_ctl1$LinkButton1','')">LinkButton1</a></td>
            <td><a id="Repeater1__ctl1_LinkButton1" href="javascript:__doPostBack('Repeater1$_ctl1$LinkButton1','')">LinkButton1</a></td>           
 </tr>
        </tr>
 
        
 <tr>
        <tr>
 <td>8</td>
            <td>8</td>
 <td>Item#8</td>
            <td>Item#8</td>
 <td><a id="Repeater1__ctl2_LinkButton1" href="javascript:__doPostBack('Repeater1$_ctl2$LinkButton1','')">LinkButton1</a></td>
            <td><a id="Repeater1__ctl2_LinkButton1" href="javascript:__doPostBack('Repeater1$_ctl2$LinkButton1','')">LinkButton1</a></td>           
 </tr>
        </tr>
 <asp:repeater id="Repeater1" runat="server" onitemcommand="Repeater1_ItemCommand" onitemcreated="Repeater1_ItemCreated">
        <asp:repeater id="Repeater1" runat="server" onitemcommand="Repeater1_ItemCommand" onitemcreated="Repeater1_ItemCreated"> <headertemplate>
        <headertemplate> <table border="1">
           <table border="1"> <tr>
           <tr> <td>ItemID</td>
            <td>ItemID</td> <td>ItemName</td>
            <td>ItemName</td>            <td><asp:button id="Button1" runat="server" commandname="Button1" text="Button1" /></td>
            <td><asp:button id="Button1" runat="server" commandname="Button1" text="Button1" /></td> </tr>
           </tr>            </headertemplate>
        </headertemplate> <itemtemplate>
        <itemtemplate> <tr>
        <tr> <td><%# Eval("ItemID") %></td>
            <td><%# Eval("ItemID") %></td> <td><%# Eval("ItemName") %></td>
            <td><%# Eval("ItemName") %></td> <td><asp:linkbutton id="LinkButton1" runat="server" commandname="LinkButton1" text="LinkButton1" /></td>
            <td><asp:linkbutton id="LinkButton1" runat="server" commandname="LinkButton1" text="LinkButton1" /></td>            </tr>
        </tr> </itemtemplate>
        </itemtemplate> <footertemplate>
        <footertemplate> </table>
        </table> </footertemplate>
        </footertemplate> </asp:repeater>
        </asp:repeater>加载数据:
 public DataTable CreateDataTable( int count)
    public DataTable CreateDataTable( int count) {
    { DataTable tbl = new DataTable();
        DataTable tbl = new DataTable(); tbl.Columns.Add( "ItemID", typeof(int) );
        tbl.Columns.Add( "ItemID", typeof(int) ); tbl.Columns.Add( "ItemName", typeof(string) );
        tbl.Columns.Add( "ItemName", typeof(string) );            
 DataRow row;
        DataRow row;   int i = count;
        int i = count; while (i-- > 0)
        while (i-- > 0) {
        { row = tbl.NewRow();
            row = tbl.NewRow(); row[0] = i;
            row[0] = i; row[1] = "Item#" + i.ToString();
            row[1] = "Item#" + i.ToString(); tbl.Rows.Add( row );
            tbl.Rows.Add( row ); }
        }
 return tbl;
        return tbl; }
    }
 protected void Page_Load(object sender, EventArgs e)
    protected void Page_Load(object sender, EventArgs e) {
    { if (!Page.IsPostBack)
        if (!Page.IsPostBack) {
        { Repeater1.DataSource = CreateDataTable(10);
            Repeater1.DataSource = CreateDataTable(10); Repeater1.DataBind();
            Repeater1.DataBind(); }
        } }
    }Repeate的ItemCreated事件:
 protected void Repeater1_ItemCreated(object sender, RepeaterItemEventArgs e)
    protected void Repeater1_ItemCreated(object sender, RepeaterItemEventArgs e) {
    { Button btn1 = e.Item.FindControl( "Button1" ) as Button;
        Button btn1 = e.Item.FindControl( "Button1" ) as Button; if (btn1 != null)
        if (btn1 != null) {
        { // do nothing, just to access the ClientID owing to the button
            // do nothing, just to access the ClientID owing to the button  //
            // string temp = btn1.ClientID;
            string temp = btn1.ClientID; }
        }
 LinkButton lnkbtn1 = e.Item.FindControl( "LinkButton1" ) as LinkButton;
        LinkButton lnkbtn1 = e.Item.FindControl( "LinkButton1" ) as LinkButton; if (lnkbtn1 != null)
        if (lnkbtn1 != null) {
        { // do nothing, just to access the ClientID owing to the linkbutton
            // do nothing, just to access the ClientID owing to the linkbutton  //
            // string temp = lnkbtn1.ClientID;
            string temp = lnkbtn1.ClientID; }
        } }
    }Repeater的ItemCommand事件:
 protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
    protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e) {
    { Response.Write( "CommandName: " + e.CommandName );
        Response.Write( "CommandName: " + e.CommandName ); }
    }运行结果:
Problem:
然而,非常奇怪的是 HeaderTempate里面的Button和ItemTemplate里面的LinkButton都不会激发Repeater的ItemCommand了。
经过多次的尝试,发现,只要在ItemCreated访问了 内嵌的 Control的ClientID,改Control的ClientID就不会改变了,即使父控控件实现了INamingContainer接口。
查看页面生成的HTML源:
 <table border="1">
<table border="1"> <tr>
           <tr> <td>ItemID</td>
            <td>ItemID</td> <td>ItemName</td>
            <td>ItemName</td>            <td><input type="submit" name="Button1" value="Button1" id="Button1" /></td>
            <td><input type="submit" name="Button1" value="Button1" id="Button1" /></td> </tr>
           </tr>            
         <tr>
        <tr> <td>9</td>
            <td>9</td> <td>Item#9</td>
            <td>Item#9</td> <td><a id="LinkButton1" href="javascript:__doPostBack('LinkButton1','')">LinkButton1</a></td>
            <td><a id="LinkButton1" href="javascript:__doPostBack('LinkButton1','')">LinkButton1</a></td>            </tr>
        </tr> 
         <tr>
        <tr> <td>8</td>
            <td>8</td> <td>Item#8</td>
            <td>Item#8</td> <td><a id="LinkButton1" href="javascript:__doPostBack('LinkButton1','')">LinkButton1</a></td>
            <td><a id="LinkButton1" href="javascript:__doPostBack('LinkButton1','')">LinkButton1</a></td>            </tr>
        </tr>注释掉ItemCreated中访问ClientID代码:
 protected void Repeater1_ItemCreated(object sender, RepeaterItemEventArgs e)
    protected void Repeater1_ItemCreated(object sender, RepeaterItemEventArgs e) {
    { Button btn1 = e.Item.FindControl( "Button1" ) as Button;
        Button btn1 = e.Item.FindControl( "Button1" ) as Button; if (btn1 != null)
        if (btn1 != null) {
        { // do nothing, just to access the ClientID owing to the button
            // do nothing, just to access the ClientID owing to the button  //
            // //string temp = btn1.ClientID;  // comment this
            //string temp = btn1.ClientID;  // comment this }
        }
 LinkButton lnkbtn1 = e.Item.FindControl( "LinkButton1" ) as LinkButton;
        LinkButton lnkbtn1 = e.Item.FindControl( "LinkButton1" ) as LinkButton; if (lnkbtn1 != null)
        if (lnkbtn1 != null) {
        { // do nothing, just to access the ClientID owing to the linkbutton
            // do nothing, just to access the ClientID owing to the linkbutton  //
            // //string temp = lnkbtn1.ClientID;  // comment this
            //string temp = lnkbtn1.ClientID;  // comment this }
        } }
    }生成的HTML源:
 <table border="1">
<table border="1"> <tr>
           <tr> <td>ItemID</td>
            <td>ItemID</td> <td>ItemName</td>
            <td>ItemName</td>            <td><input type="submit" name="Repeater1:_ctl0:Button1" value="Button1" id="Repeater1__ctl0_Button1" /></td>
            <td><input type="submit" name="Repeater1:_ctl0:Button1" value="Button1" id="Repeater1__ctl0_Button1" /></td> </tr>
           </tr>            
         <tr>
        <tr> <td>9</td>
            <td>9</td> <td>Item#9</td>
            <td>Item#9</td> <td><a id="Repeater1__ctl1_LinkButton1" href="javascript:__doPostBack('Repeater1$_ctl1$LinkButton1','')">LinkButton1</a></td>
            <td><a id="Repeater1__ctl1_LinkButton1" href="javascript:__doPostBack('Repeater1$_ctl1$LinkButton1','')">LinkButton1</a></td>            </tr>
        </tr> 
         <tr>
        <tr> <td>8</td>
            <td>8</td> <td>Item#8</td>
            <td>Item#8</td> <td><a id="Repeater1__ctl2_LinkButton1" href="javascript:__doPostBack('Repeater1$_ctl2$LinkButton1','')">LinkButton1</a></td>
            <td><a id="Repeater1__ctl2_LinkButton1" href="javascript:__doPostBack('Repeater1$_ctl2$LinkButton1','')">LinkButton1</a></td>            </tr>
        </tr>对比一下发现,Button的客户端ID不一样,猜想下原因可能是由于 ClientID不符合NameContainer规则,PostBack以后无法找到事件控件源。
Any Solutions? 
 
                    
                     
                    
                 
                    
                 
            
 
         
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号