Click here to Skip to main content
15,848,157 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi ,
I am creating a web application in ASP.NET and C#.
I have 4 textboxes for Choice1, Choice 2, Choice3 and Choice4.
And, Radiobuttonlist with 4 items, A,B,C,D
I want if user select option A in radiobuttonlist, then its value should be equal to choice 1 textbox, if user select Option B in radiobuttonlist, then its value should be equal to Choice 2 textbox and so on..
i tried if else loop for this, but whatever option i select, radiobuttonlist selected index always remains 0.
How to get selected index of radiobuttonlist??
Posted
Comments
Please post the code which you have tried.

Hi,


Code in design page...

ASP.NET
<asp:radiobuttonlist id="RadioButtonList1" runat="server" autopostback="True" xmlns:asp="#unknown">
            onselectedindexchanged="RadioButtonList1_SelectedIndexChanged">
        <asp:listitem>Choice1</asp:listitem>
        <asp:listitem>choice2</asp:listitem>
        <asp:listitem>choice3</asp:listitem>
        <asp:listitem>choice4</asp:listitem>
        </asp:radiobuttonlist>


I raise server event on index changed by user.

event code for retrieving selectedindex from raised event

C#
protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
{
    int i = RadioButtonList1.SelectedIndex;
}


If you changed index this event'll be raise thn check for index values.
Just now I worked on it.it works fine for me.

If any doubts just post with your code it can helps to solve your problem

I hope you understand what I said.


All the best.
 
Share this answer
 
Hi akkanshagupta,
I am providing Broad Explanation,if still any doubt send me a query.
Design:
<table align="center" class="style1">
            <tr>
                <td class="style2">
                    <asp:radiobuttonlist id="RadioButtonList1" runat="server" autopostback="True" xmlns:asp="#unknown">
                        onselectedindexchanged="RadioButtonList1_SelectedIndexChanged" 
                        RepeatDirection="Horizontal">
                        <asp:listitem>India
                        <asp:listitem>Australia
                        <asp:listitem>U.S.A
                        <asp:listitem>Germany
                    
                </td>
                <td>
                     </td>
            </tr>
            <tr>
                <td class="style2">
                    Srinivas From:</td>
                <td>
                    <asp:textbox id="TextBox1" runat="server" xmlns:asp="#unknown">
                </td>
            </tr>
            <tr>
                <td class="style2">
                    Jhon From:</td>
                <td>
                    <asp:textbox id="TextBox2" runat="server" xmlns:asp="#unknown">
                </td>
            </tr>
            <tr>
                <td class="style2">
                    Obama From</td>
                <td>
                    <asp:textbox id="TextBox3" runat="server" xmlns:asp="#unknown">
                </td>
            </tr>
            <tr>
                <td class="style2">
                    Hitlor From:</td>
                <td>
                    <asp:textbox id="TextBox4" runat="server" xmlns:asp="#unknown">
                </td>
            </tr>
        </table>


Code Behind:
protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
  {
      TextBox1.Text = "";
      TextBox2.Text = "";
      TextBox3.Text = "";
      TextBox4.Text = "";
      if (RadioButtonList1.SelectedIndex == 0)
      {
          TextBox1.Text = RadioButtonList1.SelectedItem.Text;
      }
      else if (RadioButtonList1.SelectedIndex == 1)
      {
          TextBox2.Text = RadioButtonList1.SelectedItem.Text;
      }
      else if (RadioButtonList1.SelectedIndex == 2)
      {
          TextBox3.Text = RadioButtonList1.SelectedItem.Text;
      }
      else
          TextBox4.Text = RadioButtonList1.SelectedItem.Text;
  }
 
Share this answer
 
v2
Comments
akkanshagupta 17-Sep-11 6:47am    
I already tried this...but it always set RadioButtonList selected index to 0.
naren programmers 17-Sep-11 14:43pm    
try this

design
<div>
<asp:RadioButtonList ID="radio" runat="server" AutoPostBack="True"
onselectedindexchanged="radio_SelectedIndexChanged">
<asp:ListItem>Usa
<asp:ListItem>Uk


<asp:TextBox ID="TextBox1" runat="server"><asp:TextBox ID="TextBox2"
runat="server">
</div>

code behind


protected void radio_SelectedIndexChanged(object sender, EventArgs e)
{
TextBox1.Text = "";
TextBox2.Text = "";
switch (radio.SelectedIndex) {
case 0:
TextBox1.Text = radio.Items[0].Text;
break;
case 1:
TextBox2.Text = radio.Items[1].Text;
break;
default:
break;
}
srinivas vadepally 17-Sep-11 6:56am    
I have done and given code for you dear!!!
Just Verify your code.
It seems that you're populating the RadioButtonList on the page load -
If so - make sure you surround your population of the RadioButtonList with an
If/Then/Postback block:
if not Page.IsPostBack then
' populate your RBL
end if

eg:

if (!IsPostBack)
{
loadradiobuttonlist();
}
 
Share this answer
 
Comments
CHill60 3-Jan-14 14:51pm    
The question is over 2 years old

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