Click here to Skip to main content
15,867,704 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello!! I"m new to asp.net and have a doubt. I want to know how to display radio button values in a textbox in asp.net.
Ex: I have two radio buttons and there values are "Male" and "Female". My query is that whenever i"ll click on "Male" radio button another textBox will show "Male".
Thanks In Advance
Posted

Ok, so on the checkchanged event of the radiobutton, set the text attribute of the textbox to the selected item value of the radiobutton.

Here is what I would do, add this to your html form:

XML
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><asp:RadioButton ID="RadioButton1" runat="server" GroupName="choice" 
             oncheckedchanged="RadioButton1_CheckedChanged" Text="Male" AutoPostBack="True"/>
        <asp:RadioButton ID="RadioButton2" runat="server" GroupName="choice" 
             Text="Female" AutoPostBack="True" 
             oncheckedchanged="RadioButton2_CheckedChanged"/>;


and add this to your code-behind:

C#
protected void RadioButton1_CheckedChanged(object sender, EventArgs e)
        {
            if (RadioButton1.Checked)
            {
                TextBox1.Text = "Male";
            }
        }
        protected void RadioButton2_CheckedChanged(object sender, EventArgs e)
        {
            if (RadioButton2.Checked)
            {
                TextBox1.Text = "Female";
            }
        }
 
Share this answer
 
v7
Comments
Chiklu.Soumya 18-Jan-13 10:22am    
I tried. But didn't worked. Please give me full example with coding.
adriancs 18-Jan-13 10:33am    
Hi, can you paste the codes that you have tried? You can paste the code in your question. Just edit the question.
Chiklu.Soumya 18-Jan-13 11:33am    
Sorry Bro! I haven't tried ur above written code. My question was regarding individual radio button "not radiobuttonlist". My code is as of:

<div>

<asp:RadioButton ID="RadioButton1" runat="server" AutoPostBack="True"
CausesValidation="True" GroupName="radio"
oncheckedchanged="RadioButton1_CheckedChanged" Text="Male" />
<asp:RadioButton ID="RadioButton2" runat="server" AutoPostBack="True"
CausesValidation="True" GroupName="radio"
oncheckedchanged="RadioButton2_CheckedChanged" Text="Female" />
<asp:TextBox ID="TextBox1" runat="server">

</div>




protected void RadioButton1_CheckedChanged(object sender, EventArgs e)
{
if (RadioButton1.Checked == true)
{
RadioButton1.Text = TextBox1.Text;
}
else
{
TextBox1.Text = "";
}
}
protected void RadioButton2_CheckedChanged(object sender, EventArgs e)
{
if (RadioButton2.Checked == true)
{
RadioButton2.Text = TextBox1.Text;
}
else
{
TextBox1.Text = "";
}
Richard C Bishop 18-Jan-13 11:39am    
Ok, I updated it using just radio buttons. Try that.
Chiklu.Soumya 18-Jan-13 12:17pm    
Thanks richcb......Now It's working fine.
First change both radio button's properties of AutoPostBack = true;
Next is as described in Solution 2.
 
Share this answer
 
v3
Comments
Chiklu.Soumya 18-Jan-13 10:21am    
Thanks!!! But It's working only in RadioButtonList Control. Doesn't worked on individual radio button. If you have any coding plz post.
adriancs 18-Jan-13 10:32am    
Hi, can you show me what are the codes that you have written so far? You can paste the code in your question. Just edit the question.
Hi,

First make RadioButton autopostback to true....
Then use as follows....

Under particular radio button event handler use this....

C#
protected void RadioButton1_CheckedChanged(object sender, EventArgs e)
        {
            if (RadioButton1.Checked)

                TextBox1.Text = RadioButton1.Text;
        }
 
Share this answer
 
First group of both check box should be same.
Next is as described in Solution 1.
 
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