简体   繁体   English

如何使用网页对象查找控件?

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

Here is code 这是代码

private void Get_Controls() 
{

     _Default def = new _Default();

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

     }

}

I have controls in Default.aspx but Count gives me 0. 我在Default.aspx中具有控件,但是Count给我0。

Try this: 尝试这个:

  foreach (var control in this.Controls)
  {
  }

try below one. 尝试以下一项。

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

Thanks. 谢谢。 Dear i have already applied this code. 亲爱的我已经应用了此代码。 To find controls using foreach I apply 10-12 loops, than it gives me controls. 为了使用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 { }
                                            }
                                        }
                                    }
                                }
                            }
                        }

                    }
                }
            }
            }
        }
    }

Instead writing long Code 而是写长代码

Finding Controls Recursively 递归查找控件

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);
    }

Calling 调用

// 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