C# .NET - how to use radiobutton list code in c#.net?

Asked By deepi singh on 16-Dec-11 04:29 AM
i have to keep 3 radio buttons in asp.net and also i have to write code in selected index changed event?

can anyone let me know how to use Radio Button List for the above requiremment?
Jitendra Faye replied to deepi singh on 16-Dec-11 04:36 AM
Implement SelectedIndexChanged event like this-

protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)

{


//write your code here

}

Try this and let me know.

Manish Mishra replied to deepi singh on 16-Dec-11 04:48 AM
Hi,
you can use the below code
--------------------------------------------------------------------------------------
public RadioButtonList()
[ValidationPropertyAttribute("SelectedItem")]
public class RadioButtonList : ListControl,
IRepeatInfoUser, INamingContainer, IPostBackDataHandler
--------------------------------------------------------------------------------------

for details of the properties, methods, events etc. follow the below post
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.radiobuttonlist.radiobuttonlist.aspx
kalpana aparnathi replied to deepi singh on 16-Dec-11 04:48 AM
hi,

Try this code:

<%@ Page Language="C#" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<script runat="server">
  protected void RadioButtonList1_SelectedIndexChanged(object sender, System.EventArgs e)
  {
    Label1.Text = "Your favorite: " + RadioButtonList1.SelectedItem.Text;
  }
</script>
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
  <title>How to use OnSelectedIndexChanged event in RadioButtonList</title>
</head>
<body>
  <form id="form1" runat="server">
  <div>
    <h2 style="color:Maroon">RadioButtonList: OnSelectedIndexChanged</h2>
    <asp:Label
       ID="Label1"
       runat="server"
       Font-Bold="true"
       ForeColor="Teal"
       Font-Size="Large"
       >
    </asp:Label>
    <br /><br />
    <asp:Label
       ID="Label2"
       runat="server"
       Font-Bold="true"
       ForeColor="DodgerBlue"
       Text="Color List"
       >
    </asp:Label>
    <asp:RadioButtonList
       ID="RadioButtonList1"
       runat="server"
       AutoPostBack="true"
       OnSelectedIndexChanged="RadioButtonList1_SelectedIndexChanged"
       BackColor="DodgerBlue"
       ForeColor="AliceBlue"
       >
       <asp:ListItem>Cyan</asp:ListItem>
       <asp:ListItem>DarkBlue</asp:ListItem>
       <asp:ListItem>DarkGreen</asp:ListItem>
       <asp:ListItem>DarkSalmon</asp:ListItem>
       <asp:ListItem>Crimson</asp:ListItem>
    </asp:RadioButtonList>
  </div>
  </form>
</body>
</html>