Click here to Skip to main content
12,622,942 members (29,646 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C#
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 17-Jan-13 7:11am
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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:

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

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";
            }
        }
  Permalink  
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 = "";
}
richcb 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.
richcb 18-Jan-13 12:29pm
   
You are welcome. Please accept this solution as your answer.
adriancs 18-Jan-13 11:44am
   
Hi Chiklu Soumya, I didn't mention RadioButtonList in my answer.
Your code is almost there. Its just has a little bug.

It should be this right?
<pre>TextBox1.Text = RadioButton1.Text;</pre>
not this
<pre>RadioButton1.Text = TextBox1.Text;</pre>
richcb 18-Jan-13 10:35am
   
See my updated solution. This works just fine.
Marcus Kramer 18-Jan-13 11:44am
   
+5 because it will work, but I just want to throw it out there that I think this is a waste of a complete postback to the server. A small handler like this is much more efficiently handled through javascript on the client. This is just the ramblings of a developer who strives to minimize postbacks... Cheers.
adriancs 18-Jan-13 11:46am
   
by using update panel?
adriancs 18-Jan-13 11:49am
   
I see others use formated code block in their comments. Do you know how to do that?
Example:
<pre lang="cs">string a = "hello";</pre>
see, the html is not rendered.
adriancs 18-Jan-13 11:59am
   
Oh, I finally got it.
to use formatted code block in comments, use
<code></code>
richcb 18-Jan-13 11:49am
   
Thank you for that input. I am new myself and am always appreciative of suggestions from veterans. I strive to improve all my abilities and knowledge.
adriancs 18-Jan-13 11:55am
   
Yes, as richcb code presents
this is not needed:

else
{
TextBox1.Text = "";
}
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

First change both radio button's properties of AutoPostBack = true;
Next is as described in Solution 2.
  Permalink  
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.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 4

Hi,

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

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

protected void RadioButton1_CheckedChanged(object sender, EventArgs e)
        {
            if (RadioButton1.Checked)
 
                TextBox1.Text = RadioButton1.Text;
        }
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

First group of both check box should be same.
Next is as described in Solution 1.
  Permalink  

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.161128.1 | Last Updated 19 Oct 2015
Copyright © CodeProject, 1999-2016
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