Cant cycle through controls in an UpdatePanel

Last post 11-15-2006 2:48 PM by mockingbird. 4 replies.

Sort Posts:

  • Cant cycle through controls in an UpdatePanel

    11-14-2006, 1:42 PM

    Hi

    I'm using a Composite pattern for user controls on a project with a custom httphandler. There is one common component that all the controls inherit from including the composite control. The composite is also a user control that simply ties all the others together and takes care of event handling between them. Anyway, some of these controls are inside UpdatePanels in the composite and when I try to recursively traverse down the control tree, I cant get a reference to the user controls that are inside UpdatePanels. I can access them directly from the codebehind of the composite but that's exactly what the recursive function is trying to avoid. I do get a reference to the UpdatePanel themselves but using a FindControl to get at the user control inside returns a null. The updatePanel.Controls.Count is always 0 and I cant find any information about the SingleChildControlCollection to see how that ought to work.

     Any advice?

    Thanks.

     

  • Re: Cant cycle through controls in an UpdatePanel

    11-14-2006, 5:33 PM
    • Loading...
    • jodywbcb
    • Joined on 03-12-2003, 3:52 PM
    • West Seattle,WA
    • Posts 985

    Here is how I handle it.  Sometimes depending on how the controls are used or the properties of the control - when in codebehind its difficult to find a control within a control if it hasn't been rendered...Wizard Control for instance - if it contains custom controls you have to do more than the typical find control.  This is the code I use for those scenarios:

     Place in Base Class...

     public Control GetNestedControl(Control skin, string controlID)
            {
                Control ctlFoundControl = null;
                try
                {
                    ctlFoundControl = base.FindControl(controlID);
                }
                catch(HttpException)
                {
                    ctlFoundControl = null;

                }
               return (ctlFoundControl != null) ? ctlFoundControl : GetNestedControl(skin.Controls, controlID);

            }

            public static Control GetNestedControl(ControlCollection col, string id)
            {
                foreach (Control c in col)
                {
                    Control child = FindNestedControl(c, id);
                    if(child !=null)
                        return child;


                }
                return null;
            }
            private static Control FindNestedControl(Control root, string id)
            {
                if (root.ID != null && root.ID == id)
                {
                    return root;
                }

                foreach (Control child in root.Controls)
                {
                    Control recurse = FindNestedControl(child, id);
                    if (recurse != null)
                    { return recurse; }
                }
                return null;

            }

     

    To use in your  control:

     

    On the init or wherever you start the process:

     

     override protected void InitializeSkin(Control skin)
            {


                    Wizard1 = (Wizard)GetControl(skin, "Wizard1"); <--this is the control we can't use the typical FindControl


    // the following are in the navigation panes use GetNested instead of GetControl

                   chkConfirmAction = (CheckBox)GetNestedControl(skin, "chkConfirmAction"); <-
        

     

    Maybe that will help you in this situation - maybe not :)

     

     

    -- jody
    My Blogs on .Net 2.0 and Ajax
    http://csk.wbcb.com
    http://ArtbyJody.com
  • Re: Cant cycle through controls in an UpdatePanel

    11-14-2006, 6:44 PM
    No, this wouldn't work. I get the updatePanel.Conrols.Count as 0 always. It wont iterate though the controls.
  • Re: Cant cycle through controls in an UpdatePanel

    11-14-2006, 7:14 PM
    • Loading...
    • jodywbcb
    • Joined on 03-12-2003, 3:52 PM
    • West Seattle,WA
    • Posts 985
    Where in the process tree are you trying get the count.  If they have not yet been placed into the control tree yet - then it makes sense that the count would be zero.  Perhaps doing a EnsureChildControls() is called before you attempt.  would do the trick...Or, perhaps if you post some demo code here... we can assist a bit better....
    -- jody
    My Blogs on .Net 2.0 and Ajax
    http://csk.wbcb.com
    http://ArtbyJody.com
  • Re: Cant cycle through controls in an UpdatePanel

    11-15-2006, 2:48 PM

    Hey Jody

    From what you said, I thought about EnsureChildControls(), which I cant use explicity, since the Composite control is also a UserControl. But this seemed to do it..

     On the aspx, this is how it was earlier where i could access all controls inside the composite and inside the usercontrols in the composite except those inside UpdatePanels

    Control ctrl = LoadControl( composite user control );
    ctrl.SetData( some object );
    if( not a postback )
          this.something.Add( ctrl );

    I've read about this somewhere and revered the statements order, like this

    Control ctrl = LoadControl( composite user control );
    if( not a postback )
           this.something.Add( ctrl );
    ctrl.SetData( some object );

     And now i can see the controls inside UpdatePanels. Strange but it works.

    Thanks a lot,
    vik


     

Page 1 of 1 (5 items)
Microsoft Communities
Page view counter