views:

136

answers:

1

What I am trying to Do is accessing Page Controls at Page_Load, and make a database query, and make controls visible or not visible.

Here is the Code

foreach (Control thiscontrol in ContentPlaceHolderBody.Controls) {

            try
            {

                if (thiscontrol.ID.Contains("TextBox") || thiscontrol.ID.Contains("Label"))
                {
                    string dummy = thiscontrol.ID;
                    bool IsValid = db.Roles.Any(a => a.controlName == dummy);
                    if (IsValid == false)
                        thiscontrol.Visible = false;
                }

                else if (thiscontrol.ID.Contains("UpdatePanel"))
                {

                    foreach (Control UPcontrols in ((UpdatePanel)thiscontrol).ContentTemplateContainer.Controls)
                    {


                            if (UPcontrols.ID.Contains("TextBox") || UPcontrols.ID.Contains("DropDownList"))
                            {
                                bool UPIsValid = db.Roles.Any(a => a.controlName == UPcontrols.ID);
                                if (UPIsValid == false)
                                    UPcontrols.Visible = false;
                            }

                    }

                }


            }
            catch { }



        }

My Problem is with the UPcontrols ! IT should retrieve the controls within the UpdatePanel, but the thing is it doesnt do its Job except in the Debug Mode !!! ... When I add a breakpoint everything is OK ! ... but when I run the web application it doesnt find any components within the UpdatePanel ....

PLease help me ... It is a very important case...

Thanks !

A: 

This seems like a very bizarre design. That is, using control IDs for such purposes is rather unusual.

Nevertheless, you need a recursive method here to do a deep walk of every control on the page. Your method will not work if the UpdatePanel is contained within another control.

Eilon