Click here to Skip to main content
15,911,891 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I'm having a RadioButtonList with two items: "Administrator" with value '1' and "User" with value '2'. I want to get the radiobutton list item to be selected on page load based on the value stored in the database. Value of the RadioButtonList selected item is stored in the database, and it is there in the int variable 'ug'. I've tried the following codes:

SQL
if (ug == '1')
                //rbl1.SelectedItem.Text = "Administrator";
                //rbl1.SelectedItem.Value = "1";
                rbl1.SelectedIndex = 0;
                //rbl1.Items[0].Selected = true;
            else
                //rbl1.SelectedItem.Text = "User";
                //rbl1.SelectedItem.Value = "2";
                rbl1.SelectedIndex = 1;
                //rbl1.Items[1].Selected = true;


But, it is not working for me. Anyone please help me....

Thank you
Posted

XML
<asp:RadioButtonList ID="rb" runat="server">
       <asp:ListItem Text="Administrator" Value="1"></asp:ListItem>
       <asp:ListItem Text="User" Value="2"></asp:ListItem>
   </asp:RadioButtonList>



C#
string ug = "1";
        if (ug == "1")
            rb.SelectedValue = ug;
        else
            rb.SelectedValue = ug;
 
Share this answer
 
C#
if(ug == "1")
{
    rbl1.selectedvalue = ug;
}
else
{
    rbl1.selectedvalue = ug;
}
 
Share this answer
 
v2
Hi,
I've tried:
C#
rbl1.SelectedValue = ug.ToString();

It's working fine.

Thank You.
 
Share this answer
 
v2

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