当前位置导航:炫浪网>>网络学院>>网页制作>>ASP.NET教程

ASP.NET遍历页面的所有button控件

1 通用代码:

    public static ArrayList BtnList(System.Web.UI.Control page)
    ...{

        int nPageControls = page.Controls.Count;
       
        ArrayList btnText = new ArrayList();

        for (int i = 0; i < nPageControls; i++)
        ...{
            foreach (System.Web.UI.Control control in page.Controls[i].Controls)
            ...{
                if (control is Button)
                ...{
                    //btnText.Add(control.ID);
                    btnText.Add((control as Button).Text);
                }
            }
        }
       
        return btnText;
    }

 2 页面调用代码

    protected void Page_Load(object sender, EventArgs e)
    ...{
        ArrayList arrayList = new ArrayList();
        System.Web.UI.Page page1 = (System.Web.UI.Page)HttpContext.Current.Handler;
        arrayList = BtnCtrList.BtnList(page1);
 
        foreach (object o in arrayList)
        ...{
            ListBox1.Items.Add(o.ToString());
        }
    }

相关内容
赞助商链接