Reputation: 11
I am new to ASP.NET. I am trying to display the text of a radio button in a text box when the radio button is selected. I have tried two different ways of doing it without success:
Attempt 1:
Code in my aspx file:
<asp:RadioButtonList ID="radioList" runat="server">
<asp:ListItem Value="selection1" Text ="One"></asp:ListItem>
<asp:ListItem Value="selection2" Text="Two"></asp:ListItem>
</asp:RadioButtonList>
And then my .cs code:
public void displayText(object sender, EventArgs e)
{
var result = radioList.SelectedValue;
output.Text = result.Text; /* Have also tried result.ToString() */
}
Attempt 2:
Same aspx as above
.cs code:
public void displayText(object sender, EventArgs e)
{
if (selection1.Checked)
{
output.Text = "One";
}
}
The first attempt doesn't give me any errors, but doesn't display the text. The second attempt gives me Error CS0103 The name 'selection1' does not exist in the current context 1_ASPTEST.aspx
I'm sure it's something simple that I'm just overlooking but I'm stumped. Thanks!
Upvotes: 1
Views: 1662
Reputation: 805
You may use this too.
if (radioList.SelectedItem != null)
{
output.Text = radioList.SelectedItem.Text;
}
Hope!. It will help you
Upvotes: 1