Disable all form controls

E

Evan M.

Hello

I'm trying to disable all the WebControls in my form under condition
(i.e. data is missing). So I'm trying the following lines:

foreach (System.Web.UI.WebControls.WebControl c in
EditingForm.Controls)
c.Enabled = false;

However, when I try and hit the page, I get this exception:
System.InvalidCastException: Specified cast is not valid.

So if this refuses to work, who can tell me how I can disable all the
web controls on my page without having to explicitly hit each one?
 
H

Hibri

Hi,
Check if the control is a webcontrol before you disable it
like;

foreach (System.Web.UI.Control c in
EditingForm.Controls){
if(c.getType()== typeof(System.Web.UI.WebControls.WebControl){
c.Enabled = false;
}
}

Not all controls in the control collection will be of the type WebControl.
HTH
 
E

Evan M.

Thanks. Discovered there were LiteralControls intermixed with the
WebControls for some reason :p

Works perfectly now.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top