Link to home
Start Free TrialLog in
Avatar of clickclickbang
clickclickbang

asked on

Cast Control To WebControl

I am iterating through controls on a page and need to cast them to a webcontrol to access attribute property.

Can someone give me a quick example of how to cast a control to a web control?

        public void FetchControls(Control c)
        {
            try
            {
                WebControl wc = (WebControl)c;

                DistributeToSets(wc);

                // Run FetchControls again if the current control is a parent and has child controls.
                if (c.HasControls())
                {
                    foreach (Control cc in c.Controls)
                    {
                        FetchControls(cc);
                    }
                }
            }
            catch (Exception ex)
            {
                debug("Failed casting from Control to WebControl.");
                debug(ex.Message);
            }
        }

Executing this gives me an invalid cast exception for ALL the controls.

Any tips will be appreciated!
ASKER CERTIFIED SOLUTION
Avatar of Yurich
Yurich
Flag of New Zealand image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of bsdotnet
bsdotnet

To access each control's attributes and change the value, you can directly use,
      Button1.Style.Add("Height","200");
      TextBox1.Style.Add ("Color","Green");

The syntax is
                 controlname.Style.Add(Attributename, AttributeValue);
Avatar of clickclickbang

ASKER

Thanks much guys!