Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Disabling all form controls (C#)

Posted on 2004-08-25
3
Medium Priority
?
593 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 2000 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

688 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