0

I have this method on my codebehind .cs file (.NET.Framework 4.0):

protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
    {
        if (CheckBox1.Checked == true)
        {

            this.nombre.Enabled = false;

        } 
    }

So, with this I can disable that nombre TextBox in my aspx, everytime when I click on the checkbox.

Here's the code in aspx file:

        <asp:CheckBox ID="CheckBox1" Checked="false" runat="server" OnCheckedChanged="CheckBox1_CheckedChanged" AutoPostBack="true"/>     

Now, I want to know, a way to simplify this routine, I mean, I have plenty of textboxes, radiobuttons, etc...

So how can I achieve this using a loop in asp.net?

Thanks in advance!

6
  • why don't you use javascript or jquery for that? If you willing to use jquery then it will be very simple with that? Commented Oct 15, 2013 at 4:33
  • what is the logic of enable/ disable textbox with multiple checkboxes ? Commented Oct 15, 2013 at 4:37
  • Nono, i just need one checkbox, the one i show you in my post, then disable all textboxes, radiobuttons etc, with just one checkbox. Commented Oct 15, 2013 at 4:39
  • @nrsharma do you know where i can find examples of this circumstance on javascript? Commented Oct 15, 2013 at 4:40
  • @KristianKoci it's not a matter of finding examples, it's a matter of logic with some different approach. If you know javascript or jquery then you can implement logic in there with very simple code. Commented Oct 15, 2013 at 4:45

2 Answers 2

1

Please try following.

protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
    {
        if (CheckBox1.Checked == true)
        {    
           DisableControlsInPage(this.Page,false);    
        } 
    }

    protected void DisableControlsInPage(Control parent, bool isEnable) {
        foreach(Control c in parent.Controls) {
            if (c is TextBox) {
                ((TextBox)(c)).Enabled = isEnable;
            }
            if (c is RadioButton) {
                ((RadioButton)(c)).Enabled = isEnable;
            }    
            DisableControlsInPage(c, isEnable);
        }
    }
Sign up to request clarification or add additional context in comments.

3 Comments

Headshottt! lol thank you very much @mit works perfectly now!!
Btw, if uncheck the checkbox they are still disabled?
In CheckBox1_CheckedChanged event write else condition with DisableControlsInPage(this.Page,true);
1

you can try to get the controls and then check if it's a textbox

foreach(Control cont in this.Controls)
{
   if(cont.GetType() == typeof(Textbox))
   {
      (cont as Textbox).Enabled = false;
   }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.