Click here to Skip to main content
15,888,803 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
C#
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
Updated 5-May-11 18:33pm
v5
Comments
Sergey Alexandrovich Kryukov 6-May-11 0:25am    
Tag properly! (I've done it for you.) It's important go catch attention of right people.
--SA
Sergey Alexandrovich Kryukov 6-May-11 0:34am    
Also, write a title which means at least something.
--SA

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:

C#
txtrb.Enabled = !RadioButton1.Enabled;


Simple, isn't it?

—SA
 
Share this answer
 
Comments
Abhinav S 6-May-11 0:33am    
Good answer. My 5.
Sergey Alexandrovich Kryukov 6-May-11 0:34am    
Thank you, Abhinav.
--SA
vimal22 2 6-May-11 0:35am    
i am beginner can u post the code
Sergey Alexandrovich Kryukov 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 6-May-11 0:44am    
efficient one. my 5.
use radiobuttonlist and make autopostback property true;
 
Share this answer
 
Comments
Programmergirl90 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!
SA already posted a nice answer. You can also try this:

XML
<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>



C#
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.
 
Share this answer
 
v3
Comments
nane aa 6-May-11 1:07am    
this answer is correct.

other wise if any radiobutton Selected="True"
in the radiobutton was default taken.
Monjurul Habib 6-May-11 2:40am    
i just show the basic thing.The implementation may change according to business logic.
Rubaba 7-May-11 10:03am    
indeed
why don't u use radiobuttonlist. It aotmaticaly uncheck the other one on selection changed
 
Share this answer
 

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