Solved

Disabling all form controls (C#)

Posted on 2004-08-25
3
587 Views
Last Modified: 2008-01-09
I'm trying to disable all form controls on a particular form.  I've looking at other examples on EE, but they all seem to require that I specify the control type.  Here is my code:

<script language="C#" runat="server">
   private void Page_Load(object sender, System.EventArgs e)
      {
         if(Page.IsPostBack) {
             Control myForm = Page.FindControl("form");
             foreach (Control c in myForm.Controls)
             {
                  c.Attributes.Add("disabled", "true");
             }
         }
      }
</script>

<form runat="server" id="form">
   <asp:TextBox id="text1" runat="server" /><br>
   <asp:DropDownList id="ddl1" runat="server" /><br>
   <textarea runat="server" id="other_software" rows="2" cols="55" />
</form>


This produces the following error:
"'System.Web.UI.Control' does not contain a definition for 'Attributes'"

I've tried the following code, and it works, but it's so messy to specify each control type.  Is there a shorter method where I don't haev to cast the control type?

foreach (Control c in myForm.Controls)
{
   if(c.GetType().ToString().Equals("System.Web.UI.WebControls.TextBox")) {
      ((TextBox)c).Attributes.Add("disabled", "true");
   } else if(c.GetType().ToString().Equals("System.Web.UI.WebControls.DropDownList")) {
      ((DropDownList)c).Attributes.Add("disabled", "true");
   } else if(c.GetType().ToString().Equals("System.Web.UI.HtmlControls.HtmlTextArea")) {
      ((HtmlTextArea)c).Attributes.Add("disabled", "true");
   } else if(c.GetType().ToString().Equals("System.Web.UI.WebControls.CheckBox")) {
      ((CheckBox)c).Attributes.Add("disabled", "true");
   } else if(c.GetType().ToString().Equals("System.Web.UI.WebControls.CheckBoxList")) {
      ((CheckBoxList)c).Attributes.Add("disabled", "true");
   }
}
0
Comment
Question by:hendridm
  • 2
3 Comments
 
LVL 8

Accepted Solution

by:
shovavnik earned 500 total points
ID: 11897125
Try this loop:

foreach (Control c in myForm.Controls)
{
  if( c is WebControl ) ( (WebControl) c ).Enabled = false;
  else if( c is HtmlControl ) ( (HtmlControl) c).Disabled = true;
}
0
 
LVL 8

Author Comment

by:hendridm
ID: 11897271
Disco.  You rock!
0
 
LVL 8

Expert Comment

by:shovavnik
ID: 11897321
Yeah, baby!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Align Buttons next to each other 2 25
File Upload Control on a ASP.NET Overlay Page 1 42
Handle null when using linq in this line 1 26
Error in page 3 45
In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now