Click here to Skip to main content
15,887,416 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I am trying to write code so that selecting a radio button will disable a textbox. The default state of the textbox should be enable. Any ideas how I should code this?
Posted
Updated 5-May-11 1:24am
v4

You could also shorten the code already presented to this single line:

C#
TextBox1.Enabled = (!RadioButton1.Checked);
 
Share this answer
 
v2
Comments
thatraja 5-May-11 22:45pm    
Simple & nice answer
On Page_Load event , write the following code.

C#
protected void Page_Load(object sender, System.EventArgs e)
{
    if (!IsPostBack) {
        TextBox1.Enabled = true;
    }
}


On RadioButton1_CheckedChanged event, write the following code.

C#
protected void RadioButton1_CheckedChanged(object sender, System.EventArgs e)
{
    if (RadioButton1.Checked) {
        TextBox1.Enabled = false;
    } else {
        TextBox1.Enabled = true;
    }
}
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 5-May-11 18:34pm    
Wonde, you too!
How can you write such code?!
Please, "TextBox1.Enabled = !RadioButton1.Checked;" no ifs (and no buts) :-)
--SA
Wonde Tadesse 5-May-11 20:09pm    
Thanks. But this can be another option. And more over, I did this code to demonstrate basic conditional statement for . Is it a crime. kannan 2448 . :(
on Form Load:

Textbox1.Enabled=False;


C#
private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
    if (Textbox1.Enabled==False)
       {Textbox1.Enabled=True;
       }
else
{Textbox1.Enabled=False;}
        }
 
Share this answer
 
Comments
Ashishmau 5-May-11 7:17am    
First, Read Question Carefully
Sergey Alexandrovich Kryukov 5-May-11 18:31pm    
Raji, you need to be careful not to show bad examples of coding.
The fact you're using "if (Textbox1.Enabled==False) ..." instead of "Textbox1.Enabled = !Textbox1.Enabled" (instead of all of your code of the method body) shows that you're quite confused about programming yourself! You also violate naming convention and do not indicate anything about even set-up.
--SA
on page load u can write like

textbox.enabled=true;


then on radiobutton checkchanged event u can write this

C#
protected void RadioButton1_CheckedChanged(object sender, EventArgs e)
   {
           if(RadioButton1.checked==true)
           textbox.enabled=false;
           else
           textbox.enabled=true;
   }
 
Share this answer
 
v3
Comments
Tarun.K.S 5-May-11 7:32am    
You might want to read it again, I think the question has changed.
Ashishmau 5-May-11 7:41am    
corrected
Sergey Alexandrovich Kryukov 5-May-11 18:32pm    
Pretty bad code, just a bit batter than Raji's (that code does not solve the problem). Please see my comment to Raji's answer.
--SA
Hi,
You should write the javascript code, that runs at server. The code should be activated when the radiobutton's OnCheckedChanged event is fired.
Solution can be found at http://forums.asp.net/t/1288462.aspx/1[^].
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900