Solved

Disabling all form controls (C#)

Posted on 2004-08-25
3
591 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

710 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