例如:
<table id="thisTable">
<tr>
<td>
<asp:Button ID="btn_Save" runat="server" Text="保存模塊信息" Width="120px" />
<asp:Button ID="btn_Save1" runat="server" Text="保存模塊信息" Width="120px" />
<asp:Button ID="btn_Savea" runat="server" Text="保存模塊信息" Width="120px" />
<asp:Button ID="btn_Savex" runat="server" Text="保存模塊信息" Width="120px" />
<asp:Button ID="btn_Save6" runat="server" Text="保存模塊信息" Width="120px" />
<asp:Button ID="btn_Save3" runat="server" Text="保存模塊信息" Width="120px" />
<asp:Button ID="btn_Save2" runat="server" Text="保存模塊信息" Width="120px" />
</td>
</tr>
</table>因為按鈕是動態+上去的,我唯一知道的是類型(Button)
我想實現的是通過范圍(thisTable),類型(Button)
找出在thisTable里,類型是Button的所有控件id
不知道要怎么去實現,請教教我!!!!!!!!!

解决方案 »

  1.   


    List<Button> list = new List<Button>();
    private void SearchButton(Control ctrl)
    {
        foreach(Control c in ctrl.Controls)
        {
            if(c is Button)
            {
                 Button btn = c as Button;
                 list.Add(btn); 
            } 
            else if(c.HasControls())
                 SearchButton(c);
        }
    }
      

  2.   


    using System;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;
    using System.Collections;
    namespace Mail
    {
    public class FindControl
    {
    public FindControl()
    {
    }
    public void Find(System.Web.UI.Control page)
    {
    int iPageControls = page.Controls.Count;
    for (int i = 0; i < iPageControls; i++)
    {
    foreach (System.Web.UI.Control control in page.Controls)
    {
    if (control is Button)
    {
    (control as Button).Text="123";
    }
    if(control is HtmlTableCell)
    {
    Find(control as HtmlTableCell);
    }
    if(control is HtmlTableRow)
    {
    Find(control as HtmlTableRow);
    }
    }
    }
    }

    }
    }刚写的,没测试..
      

  3.   

    那為什么針對checkboxlist就不行了呢?
      

  4.   

    200分,提供给你一个方法,也许能解决你的问题
    以下是我测试的方法,动态添加了六个CheckBoxList            for (int i = 0; i < 6; i++)
    {
                    CheckBoxList chk = new CheckBoxList();
                    chk.ID = "chk" + i;
                    ListItem lst=new ListItem("abc");
                    chk.Items.Add(lst);
                    form1.Controls.Add(chk);
    }然后获取这六个动态添加的控件 for (int i = 0; i < form1.Controls.Count; i++)
    {
                     if (form1.Controls[i].GetType()==typeof(CheckBoxList))
                    {
                        CheckBoxList radl = (CheckBoxList)(form1.Controls[i]);
                    }
    }    
      

  5.   

    你动态生成网页后,看看你自动生成的html源文件!!!
      

  6.   

    看看里的CheckBoxList 有没有ID!!
      

  7.   

    問題已經解決,因為動態控件,只要page刷新就什么都沒了
    我設置cbl_ParentModule.EnableViewState = true;
    也沒效果