Solved

Disabling all form controls (C#)

Posted on 2004-08-25
3
590 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

821 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