Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET Forms
i have click the radio button1 the disable the text box. the radio button2 click
 
the text box is enable but first i have click radio button2 and then i have click radio button1 not disable the text box How to solve the problem
public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
               txtrb.Enabled = true;
            }
        }
 
        protected void RadioButton1_CheckedChanged(object sender, EventArgs e)
        {
            {
                if (RadioButton1.Enabled == true )
                {
                    txtrb.Enabled = false ;
                }
            }
        }
        protected void RadioButton2_CheckedChanged(object sender, EventArgs e)
        {
            if (RadioButton2.Enabled == true)
            {
                txtrb.Enabled = true ;
            }
 
        }
    }
}
Posted 5-May-11 19:23pm
Edited 5-May-11 19:33pm
v5
Comments
SAKryukov at 6-May-11 0:25am
   
Tag properly! (I've done it for you.) It's important go catch attention of right people.
--SA
SAKryukov at 6-May-11 0:34am
   
Also, write a title which means at least something.
--SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

If you radio button are in the same group, checking one automatically un-check others. In this way, you need the handler for only one radio button. The second one could be redundant or destroy result of the first one. Remove it.
 
Besides, what are you doing?!
 
Who compare a Boolean with true?! The result is exactly same very Boolean! Who assign another Boolean under if? Please, think a bit before coding, just to understand how ridiculous this is. Do it this way:
 
txtrb.Enabled = !RadioButton1.Enabled;
 
Simple, isn't it?
 
—SA
  Permalink  
Comments
Abhinav S at 6-May-11 0:33am
   
Good answer. My 5.
SAKryukov at 6-May-11 0:34am
   
Thank you, Abhinav.
--SA
vimal22 2 at 6-May-11 0:35am
   
i am beginner can u post the code
SAKryukov at 6-May-11 0:38am
   
I just did! This is all you need. Replace the code in your handler and remove one of them.
--SA
Monjurul Habib at 6-May-11 0:44am
   
efficient one. my 5.
SAKryukov at 6-May-11 1:11am
   
Thank you Monjurul.
I don't think efficiency is very difficult, it's just hard too look at ridiculous code, such as "a = 5; a = 6;" :-) If boolean == true is of the same kind.
--SA
Monjurul Habib at 6-May-11 2:38am
   
i do agree.
Rubaba at 7-May-11 10:03am
   
5 for good link.
Monjurul Habib at 7-May-11 12:03pm
   
good link!! where is that??
SAKryukov at 7-May-11 20:02pm
   
No links, just a solution.
--Sa
Monjurul Habib at 9-May-11 2:30am
   
yes.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

use radiobuttonlist and make autopostback property true;
  Permalink  
Comments
Programmergirl90 at 10-Apr-14 10:36am
   
I've searched high and low through JavaScript that only partially works. This worked perfectly and my form is acting exactly how I want it to!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

SA already posted a nice answer. You can also try this:
 
<asp:RadioButton ID="RadioButton1" runat="server" OnCheckedChanged="RadioButton1_CheckedChanged"
           AutoPostBack="true" GroupName="RadioButton" Text="Enable Textbox" /><br />
       <asp:RadioButton ID="RadioButton2" runat="server" OnCheckedChanged="RadioButton2_CheckedChanged"
           AutoPostBack="true" GroupName="RadioButton" Text="Disable Textbox" /><br />
           <asp:TextBox ID="txtrb" runat="server"></asp:TextBox>
 

        protected void RadioButton1_CheckedChanged(object sender, EventArgs e)
        {
            txtrb.Enabled = false ;
                
        }
        protected void RadioButton2_CheckedChanged(object sender, EventArgs e)
        {
                txtrb.Enabled = true ;
           
        }
 

 
I hope the above information will be helpful. If you have more concerns, please let me know.
  Permalink  
v3
Comments
nane aa at 6-May-11 1:07am
   
this answer is correct.
 
other wise if any radiobutton Selected="True"
in the radiobutton was default taken.
Monjurul Habib at 6-May-11 2:40am
   
i just show the basic thing.The implementation may change according to business logic.
Rubaba at 7-May-11 10:03am
   
indeed
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

why don't u use radiobuttonlist. It aotmaticaly uncheck the other one on selection changed
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 319
1 Kornfeld Eliyahu Peter 265
2 Maciej Los 249
3 BillWoodruff 205
4 Sergey Alexandrovich Kryukov 175
0 OriginalGriff 5,107
1 DamithSL 4,157
2 Maciej Los 3,595
3 Kornfeld Eliyahu Peter 3,450
4 Sergey Alexandrovich Kryukov 2,741


Advertise | Privacy | Mobile
Web01 | 2.8.141216.1 | Last Updated 6 May 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100