Link to home
Start Free TrialLog in
Avatar of brgdotnet
brgdotnetFlag for United States of America

asked on

The first item in my radio button list is always selected ?

500 points for this one as I am rushed to get the product fixed and into production.
Hello, a few weeks ago, I added to my web form two radio buttons. and a regular asp.net button.
The two radio buttons are labeled
-View Statistics
-View Charts
When the regular asp.net button is pressed, I then check to see which item from the radio button list was selected.
Now for some reason the code is broken and no matter which radio button I select, if I trace into the code, it shows that the very first radio button was selected(View Statistics). Even though I selected the second one(View Charts). Below is my C# code and asp.net code. Can anyone tell me what is going wrong here? Why the code behind shows that the first item is always selected?
Below is my C# code and asp.net code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        foreach (ListItem item in RadioButtonList1.Items)
        {
            if (item.Value == "View Statistics")
            {
                 break;
            }
            else
            {
                break;
            }
        }
    }
}
//-----------------------------------------------------------------------
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
                <asp:RadioButtonList ID="RadioButtonList1" runat="server" Width="225px">
                <asp:ListItem Selected="True">View Statistics</asp:ListItem>
                <asp:ListItem>View Charts</asp:ListItem>
            </asp:RadioButtonList>
            <br />
            <asp:Button ID="Button1" runat="server" Text="View Report" Width="110px" 
                onclick="Button1_Click" />
            <div >
    </div>
    </form>
</body>
</html>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of mohan_sekar
mohan_sekar
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of brgdotnet

ASKER

Awesome Mohan.

RadioButtonList1.SelectedItem.Value
is the best way to get the value. Have a great day!
RadioButtonList1.SelectedItem.Value
is the best way to get the value!