Disable all controls in...

E

Edwin Knoppert

I have a div which holds controls.
I'm looking for a way to disable all controls but *without* setting each
control enable state.
Same to style>display i'm looking for a disable method.

Do i need a container control for this?
 
A

Andrew Robinson

You can use a container control or make the div a server side control:

<body>
<form id="form1" runat="server">

<div id="div1" runat="server">

<asp:TextBox ID="TextBox1" runat="server" />

<asp:Button ID="Button1" runat="server" />

</div>

</form>

</body>

div1.Visible = false;
 
A

Andrew Robinson

Edwin,

Only ASP.NET server controls contain the Enabled property. If you are using
all server side controls, you can do the following:

<body>
<form id="form1" runat="server">
<div>
<asp:placeHolder ID="PlaceHolder1" runat="server">
<asp:TextBox ID="TextBox1" runat="server" />
<asp:Button ID="Button1" runat="server" Text="Button" />
</asp:placeHolder>
</div>
</form>
</body>

in code:

foreach (WebControl control in PlaceHolder1.Controls)
{
control.Enabled = false;
}

If you attempt to use this with an HTML control such as an <IMG> or <INPUT>
tag, you will throw an InvalidCastException.

Hope this helps.
 
Joined
Dec 4, 2007
Messages
1
Reaction score
0
Enable Disable, all controls inside a div / placeholder

ASPX Page:

Code:
 <asp:PlaceHolder ID="placeHolderMyServerControl" runat="server"></asp:PlaceHolder>

Code Behind:

Call Function:
Code:
private bool condition;

protected override void CreateChildControls()
{
   base.CreateChildControls();
   MyServerControl serverControl = new MyServerControl();
   placeHolderMyServerControl.Controls.Add(serverControl);
}

protected override void OnPreRender(EventArgs e)
{
  base.OnPreRender(e);
  MyCondition();
  if(condition == true)
  {
     EnableControl(true, placeHolderMyServerControl.Controls);
  }
  else
  {
     EnableControl(false, placeHolderMyServerControl.Controls);
  }
}
Create Function:
Code:
/// <summary>
/// Enable Disable Control
/// </summary>
/// <param name="enabled">true or false</param>
/// <param name="controls">Controls Collection</param>
 private void EnableControl(bool enabled, ControlCollection controls)
        {
           foreach (Control childControl in control)
           {
               try
               {
                   WebControl webChildControl = (WebControl) childControl;
                   webChildControl.Enabled = enabled;
               }
               catch
               {
                   
               }
               finally
               {
                   EnableControl(enabled, childControl.Controls);
               }
           }
        }


//Your Condition
private void MyCondition()
{
//Put your own if condition
  if()
  {
   condition = true;
  }
  else
  {
   condition = false;
   }
}
=============

:driver:
I have more posts like these on rudebox.editboard.com <- My forum ;)
 
Last edited:

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top