views:

33

answers:

3

Anyone know of a way to read out in a list all of the UpdatePanel Client ID's I have on a page? Basically I need to loop through all controls in the page with a type of UpdatePanel, and display the ClientID for each..

I have four update panels on this page and I am using this

        private string LoopUpdatePanel(ControlCollection controlCollection)
    {
        StringBuilder sb = new StringBuilder();
        foreach (Control control in controlCollection)
        {
            if (control is UpdatePanel)
            {
                sb.Append(((UpdatePanel)control).ClientID + ", ");
            }

            if (control.Controls != null)
            {
                LoopUpdatePanel(control.Controls);
            }
        }
        return sb.ToString();
    }

It returns an empty string??

A: 

Sounds like a proper logic, maybe you might be searching through a different set of controls?

Have you tried to step through using debug and check the IDs?


 protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            string result = "result = ";

            foreach (Control tmpControl in Page.Controls)
            {
                Type tmpType = tmpControl.GetType();

                if (tmpControl is SiteMaster)
                {
                    foreach (Control SiteMasterControlItem in tmpControl.Controls)
                    {
                        if (SiteMasterControlItem is System.Web.UI.HtmlControls.HtmlForm)
                        {
                            int i = 0;

                            for( i =0;i < SiteMasterControlItem.Controls.Count; i++)
                            {
                                Type tmpType2 = SiteMasterControlItem.Controls[i].GetType();
                            }
                        }


                    }
                }
            }

            Response.Write(result);
        }

        catch(Exception ex)
        {
            Response.Write("error = " + ex.StackTrace);
        }
    }

The last loop contains the contentplace holder. You might have to go deeper. This was tried on VS2010 with c#4.0

HTH

rlee923
Yes and it doesn't seem to find any of the UpdatePanels? See above
leen3o
I can't get the code to understand what 'SiteMaster' is? I get the red underline...
leen3o
skip that bit if you do not have master page.
rlee923
I do but the code doesn't understand what SiteMaster is?
leen3o
It is a control that contains master page contents I thinkCould be new to c# 4.0Just Ignore it and use inner loop.Just go through all the controls you have at the moment and print it all out.
rlee923
+2  A: 

Not sure what language you're using or what you've tried...Please give us some code so we know what you have tried maybe we'll catch the error.

but as an example here's how you'd loop through controls. Just use the properties you want instead of text (in your case Id value and update panels). I believe the important part of acquiring controls is to go through the entire control hierarchy so you don't miss any children, grand children etc.

    private void Page_Load(object sender, System.EventArgs e)
{
    LoopTextboxes(Page.Controls);
}

private void LoopTextboxes(ControlCollection controlCollection)
{
    foreach(Control control in controlCollection)
    {
        if(control is TextBox)
        {
            ((TextBox)control).Text = "I am a textbox";
        }

        if(control.Controls != null)
        {
            LoopTextboxes(control.Controls);
        }
    }
}
Ehsan
I have added code above based on yours and it still returns an empty string - I have run in Debug and it never finds a control of type UpdatePanel? even though there are four on the page!
leen3o
A: 

It is a control that contains master page contents I think Could be new to c# 4.0 Just Ignore it and use inner loop. Just go through all the controls you have at the moment and print it all out.

Also the method you got up there is not returning values correctly, you are called the method without return values, not the signature but when you call the method like LoopUpdatePanel(control.Controls);

rlee923