简体   繁体   中英

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM