簡體   English   中英

如何使用網頁對象查找控件?

[英]How to find Controls using Web Page Object?

這是代碼

private void Get_Controls() 
{

     _Default def = new _Default();

     for (int i = 0; i < def.Controls.Count; i++)
     {

     }

}

我在Default.aspx中具有控件,但是Count給我0。

嘗試這個:

  foreach (var control in this.Controls)
  {
  }

嘗試以下一項。

foreach (Control c in Page.Controls)
{
    foreach (Control childc in c.Controls)
    {
        if (childc is TextBox)
        {
            allTextBoxValues += ((TextBox)childc).Text + ",";
        }
    }
}

謝謝。 親愛的我已經應用了此代碼。 為了使用foreach查找控件,我應用了10-12個循環,這比給我的控件要多。

private void Refresh()
{
    foreach (Control c in Page.Controls)
    {
        foreach (Control childc in c.Controls)
        {
            foreach (Control childe in childc.Controls)
            {
                if (childe is DevExpress.Web.ASPxPopupControl.ASPxPopupControl)
                {
                    foreach (Control childf in childe.Controls)
                    {
                        foreach (Control childg in childf.Controls)
                        {
                            foreach (Control childh in childg.Controls)
                            {
                                foreach (Control childi in childh.Controls)
                                {
                                    foreach (Control childj in childi.Controls)
                                    {
                                        foreach (Control childk in childj.Controls)
                                        {
                                            foreach (Control childl in childk.Controls)
                                            {
                                                if (childl is TextBox)
                                                {
                                                    ((TextBox)childl).Text = "";
                                                }
                                                else if (childl is                    DevExpress.Web.ASPxEditors.ASPxTextBox)
                                                {
                                                    ((DevExpress.Web.ASPxEditors.ASPxTextBox)childl).Text = "";
                                                }
                                                else { }
                                            }
                                        }
                                    }
                                }
                            }
                        }

                    }
                }
            }
            }
        }
    }

而是寫長代碼

遞歸查找控件

private Control FindControlReqursive(Control parent)
    {
        foreach (Control control in parent.Controls)
        {
            Control result = FindControlReqursive(control);

            if (result != null)
            {
                if (result is TextBox )
                {
                    TextBox myTextBox = (TextBox)result;
                    myTextBox.Text = "";
                }
                return result;
            }
        }
        return parent.FindControl(controlId);
    }

調用

// controlId is id of the control u want to find 
//eg. i m finding label inside page

 TextBox myLabel=FindControlReqursive(Page);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM