Click here to Skip to main content
16,013,747 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
WebForm1.aspx

<body>
    <form id="form1" runat="server">>
        <div style="width:180px;height:200px;overflow:scroll;">
            <asp:RadioButtonList ID="rdlCountries" runat="server" AutoPostBack="t 
  rue"              OnSelectedIndexChanged="rdlCountries_SelectedIndexChan&</asp:RadioButtonList>
        </div>
        <br>
        <asp:Label ID="lblSelectedRadioItem" runat="server">></asp:Label>
    </form>


C#
protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                bindCountries();
            }
        }

        private void bindCountries()
        {
            rdlCountries.Items.Add("India");
            rdlCountries.Items.Add("USA");
            rdlCountries.Items.Add("UK");
            rdlCountries.Items.Add("Brazil");
            rdlCountries.Items.Add("England");
            rdlCountries.Items.Add("Telangana");
            rdlCountries.Items.Add("AP");
            rdlCountries.Items.Add("MP");
            rdlCountries.Items.Add("UP");
            rdlCountries.Items.Add("HP");
            rdlCountries.Items.Add("Tail"); 
            rdlCountries.Items.Add("Fra");
            rdlCountries.Items.Add("Par"); 
            rdlCountries.Items.Add("gni");
            rdlCountries.Items.Add("Pd");
            rdlCountries.Items.Add("sinka"); 
            rdlCountries.Items.Add("Aulia");
            rdlCountries.Items.Add("WestIndies");
            rdlCountries.Items.Add("France");
            rdlCountries.Items.Add("Par");
            rdlCountries.Items.Add("geri"); 
            rdlCountries.Items.Add("Poland");
            rdlCountries.Items.Add("srinka");
            rdlCountries.Items.Add("Austlia");
            rdlCountries.Items.Add("Eland");
            rdlCountries.Items.Add("Westdies"); 
            rdlCountries.Items.Add("Frce");
            rdlCountries.Items.Add("Pais"); 
            rdlCountries.Items.Add("germi"); 
            rdlCountries.Items.Add("Pond");
            rdlCountries.Items.Add("srinka"); 
            rdlCountries.Items.Add("Austria");
        }
        protected void rdlCountries_SelectedIndexChanged(object sender, EventArgs e)
        {
            lblSelectedRadioItem.Text = "Selected RadioButtonListItem Name is " + rdlCountries.SelectedItem.ToString() + "";
        }


What I have tried:

I am beginner to Coding and .net,
When I select last listItem,after reloading the page its showing first radiobuttonlist items...
but, I want to show the listitem what I selected in the list.
Plz help me in this scenario.
Thanks in Advance
Posted
Updated 10-Aug-16 7:59am
Comments
Karthik_Mahalingam 10-Aug-16 13:56pm    
try using javascript/jquery, since the size of the container is small when the post back happens, automatically the focus will be moved to the first item. tried with update panel also, no luck..

1 solution

try this

JavaScript
<script>
       function funChange() {
           var list = document.getElementById('<%= rdlCountries.ClientID %>');
           var inputs = list.getElementsByTagName("input");
           var selectedItem;
           for (var i = 0; i < inputs.length; i++) {
               if (inputs[i].checked) {
                   selectedItem = inputs[i];
                   break;
               }
           }
           document.getElementById('<%= lblSelectedRadioItem.ClientID %>').textContent = selectedItem.value;
       }
   </script>


ASP.NET
<asp:radiobuttonlist id="rdlCountries" runat="server" onchange="funChange()"  ></asp:radiobuttonlist>
 
Share this answer
 
v2
Comments
VeenGreen 10-Aug-16 22:43pm    
When I select last listItem,after reloading the radiobuttonlist items going top
but, I want to show the listitem what I selected in the list,Not in the Label.I tried Your Suggested code.but its not highlighting the selected Item(means coming in front of user eye)
Karthik_Mahalingam 10-Aug-16 23:37pm    
coz the post back is still happening.

use exactly this
<asp:radiobuttonlist id="rdlCountries" runat="server" onchange="funChange()" >
VeenGreen 11-Aug-16 13:13pm    
Thanks for your suggestion...it's working..
Karthik_Mahalingam 11-Aug-16 13:14pm    
cool
VeenGreen 11-Aug-16 23:27pm    
Thank you again..

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