Solved

Disabling all form controls (C#)

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

708 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

16 Experts available now in Live!

Get 1:1 Help Now