Solved

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

Posted on 2009-05-06
3
397 Views
Last Modified: 2012-05-06
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

0
Comment
Question by:brgdotnet
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 15

Accepted Solution

by:
mohan_sekar earned 500 total points
ID: 24317179
because, you've added Selected="True" to the 1st list item. Remove it
also, to get the selected item,  use "RadioButtonList1.SelectedItem.Value"
0
 
LVL 2

Author Comment

by:brgdotnet
ID: 24317716
Awesome Mohan.

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

Author Closing Comment

by:brgdotnet
ID: 31578568
RadioButtonList1.SelectedItem.Value
is the best way to get the value!
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

632 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question