Home All Groups Group Topic Archive Search About

Trouble with radio button list in Asp.net 1.1

Author
12 Dec 2005 2:48 PM
Larry Beall
I have a radio button list that has to contain the same value for 2
items.  When this is posted back and I attempt to grab the selected
index it is always using the first instance of that value item.  Is this
a bug or intended design and does anyone have any suggestions for
getting around this limitation?  Below is some code that I have used to
reproduce the problem.  Any help would be much appreciated.

aspx file
<%@ Page language="c#" Codebehind="default.aspx.cs"
AutoEventWireup="false" Inherits="troubleshooting._default" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
    <HEAD>
        <title>default</title>
        <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
        <meta name="CODE_LANGUAGE" Content="C#">
        <meta name="vs_defaultClientScript" content="JavaScript">
        <meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
    </HEAD>
    <body MS_POSITIONING="GridLayout">
        <form id="Form1" method="post" runat="server">
            <asp:RadioButtonList id="RadioButtonList1" style="Z-INDEX: 101; LEFT:
24px; POSITION: absolute; TOP: 16px"
                runat="server">
                <asp:ListItem Value="1">Yes</asp:ListItem>
                <asp:ListItem Value="0">No</asp:ListItem>
                <asp:ListItem Value="1">NA</asp:ListItem>
            </asp:RadioButtonList>
            <asp:Button id="Button1" style="Z-INDEX: 102; LEFT: 16px; POSITION:
absolute; TOP: 104px" runat="server"
                Text="Button"></asp:Button>
        </form>
    </body>
</HTML>


cs file
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace troubleshooting
{
    /// <summary>
    /// Summary description for _default.
    /// </summary>
    public class _default : System.Web.UI.Page
    {
        protected System.Web.UI.WebControls.RadioButtonList RadioButtonList1;
        protected System.Web.UI.WebControls.Button Button1;

        private void Page_Load(object sender, System.EventArgs e)
        {
            // Put user code to initialize the page here
        }

        #region Web Form Designer generated code
        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN: This call is required by the ASP.NET Web Form Designer.
            //
            InitializeComponent();
            base.OnInit(e);
        }

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.Button1.Click += new System.EventHandler(this.Button1_Click);
            this.Load += new System.EventHandler(this.Page_Load);

        }
        #endregion

        private void Button1_Click(object sender, System.EventArgs e)
        {
            Response.Write(this.RadioButtonList1.SelectedIndex.ToString());
        }
    }
}

Author
12 Dec 2005 6:26 PM
Phillip Williams
Hi Larry,

It is by design that the selectedIndex gets or sets the lowest ordinal index
of the selected items in the list (in your case where there is a duplicate
value, it picks up the first).

To get a round it (or rather right into it) assign a unique value for each
listitem.
Show quoteHide quote
"Larry Beall" wrote:

> I have a radio button list that has to contain the same value for 2
> items.  When this is posted back and I attempt to grab the selected
> index it is always using the first instance of that value item.  Is this
> a bug or intended design and does anyone have any suggestions for
> getting around this limitation?  Below is some code that I have used to
> reproduce the problem.  Any help would be much appreciated.
>
> aspx file
> <%@ Page language="c#" Codebehind="default.aspx.cs"
> AutoEventWireup="false" Inherits="troubleshooting._default" %>
> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
> <HTML>
>     <HEAD>
>         <title>default</title>
>         <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
>         <meta name="CODE_LANGUAGE" Content="C#">
>         <meta name="vs_defaultClientScript" content="JavaScript">
>         <meta name="vs_targetSchema"
> content="http://schemas.microsoft.com/intellisense/ie5">
>     </HEAD>
>     <body MS_POSITIONING="GridLayout">
>         <form id="Form1" method="post" runat="server">
>             <asp:RadioButtonList id="RadioButtonList1" style="Z-INDEX: 101; LEFT:
> 24px; POSITION: absolute; TOP: 16px"
>                 runat="server">
>                 <asp:ListItem Value="1">Yes</asp:ListItem>
>                 <asp:ListItem Value="0">No</asp:ListItem>
>                 <asp:ListItem Value="1">NA</asp:ListItem>
>             </asp:RadioButtonList>
>             <asp:Button id="Button1" style="Z-INDEX: 102; LEFT: 16px; POSITION:
> absolute; TOP: 104px" runat="server"
>                 Text="Button"></asp:Button>
>         </form>
>     </body>
> </HTML>
>
>
> cs file
> using System;
> using System.Collections;
> using System.ComponentModel;
> using System.Data;
> using System.Drawing;
> using System.Web;
> using System.Web.SessionState;
> using System.Web.UI;
> using System.Web.UI.WebControls;
> using System.Web.UI.HtmlControls;
>
> namespace troubleshooting
> {
>     /// <summary>
>     /// Summary description for _default.
>     /// </summary>
>     public class _default : System.Web.UI.Page
>     {
>         protected System.Web.UI.WebControls.RadioButtonList RadioButtonList1;
>         protected System.Web.UI.WebControls.Button Button1;
>    
>         private void Page_Load(object sender, System.EventArgs e)
>         {
>             // Put user code to initialize the page here
>         }
>
>         #region Web Form Designer generated code
>         override protected void OnInit(EventArgs e)
>         {
>             //
>             // CODEGEN: This call is required by the ASP.NET Web Form Designer.
>             //
>             InitializeComponent();
>             base.OnInit(e);
>         }
>        
>         /// <summary>
>         /// Required method for Designer support - do not modify
>         /// the contents of this method with the code editor.
>         /// </summary>
>         private void InitializeComponent()
>         {
>             this.Button1.Click += new System.EventHandler(this.Button1_Click);
>             this.Load += new System.EventHandler(this.Page_Load);
>
>         }
>         #endregion
>
>         private void Button1_Click(object sender, System.EventArgs e)
>         {
>             Response.Write(this.RadioButtonList1.SelectedIndex.ToString());
>         }
>     }
> }
>
Author
12 Dec 2005 7:22 PM
Larry Beall
I was afraid you were going to say that.  Unfortunately this is an
application that has been in production a while and will require a code
work around in order to apply different values.  Thank you for your reply.

Phillip Williams wrote:
Show quoteHide quote
> Hi Larry,
>
> It is by design that the selectedIndex gets or sets the lowest ordinal index
> of the selected items in the list (in your case where there is a duplicate
> value, it picks up the first).
>
> To get a round it (or rather right into it) assign a unique value for each
> listitem.