On a radiobuttonlist I'm always getting .SelectedIndex = -1 even though I have selected one of the radio buttons. Change that to dropdownlist and it works. .NET C#

Posted on 2004-11-15
Last Modified: 2006-11-17
This is a good one. I'm seeing different behavior with radiobuttonlist versus drowdownlist even though they supposedly inheret the same properties from listcontrol

I created a radiobuttonlist and bound data to it via my page_load code

-- see data binding in page_load here --
// query db for cyclename and due date
                        string sql = "select distinct CycleName,DueDate " +
                              "from v_store_assignments " +
                              "where Source=2 " +
                              "and ChainName=@p_chainname " +
                              "and DueDate>=@p_duedatebegin and DueDate<= @p_duedateend " +
                              "order by DueDate asc";
                        System.Configuration.AppSettingsReader configurationAppSettings = new System.Configuration.AppSettingsReader();
                        string dsn = ((string)(configurationAppSettings.GetValue("sqlConnection2_mr.ConnectionString", typeof(string))));
                        using (SqlConnection conn = new SqlConnection(dsn))
                        using (SqlCommand cmd = new SqlCommand(sql,conn))
                              cmd.Parameters.Add("@p_chainname", SqlDbType.VarChar,50).Value=_Retailer2.SelectedValue;
                              cmd.Parameters.Add("@p_duedatebegin", SqlDbType.DateTime).Value=dt_beginrange.Text;
                              cmd.Parameters.Add("@p_duedateend", SqlDbType.DateTime).Value=dt_endrange.Text;
                              _cyclelist.DataSource = cmd.ExecuteReader();
                              // I want to eventually display the visit date range along with the CycleName
                              //string displayfield =
                              _cyclelist.DataTextField = "CycleName";
                              _cyclelist.DataValueField = "DueDate";

-- end data binding here--

I then select from the list a single radio button and select another button called view report. That view report button calls code that retrieves the value of the button I selected and sets it as a crystal report parameter.

Now when I try to retrieve the value via the .selectedvalue:


I get null.

When I check for the .SelectedIndex I always get -1.

Then when I change the radiobuttonlist to a dropdownlist all works. Argghhh.

So, does anybody know why the radiobuttonlist would behave differently than the dropdownlist?
Question by:kristenhazard

    Author Comment

    OK, after spending hours on this maddening issue. It's like the value that I select never actually gets posted.  For some reason on the radiobuttonlist selected value is not being recognized. Now when I hardcode my values in another radioubutton list versus getting from DB the it does work properly.
    LVL 3

    Accepted Solution

    check for post back before you load the radio button values. otherwise the selected index is lost as the values are reloaded on each page load

        //Load the drop down list


    //selected index changed event handler if you want to handle on every post back
    protected void RDButton_Channged(Object sender,System.EventArgs e)

    LVL 10

    Expert Comment

    hi, instead of:

    you shold use:

    LVL 10

    Expert Comment

    of course currentValue should be object type, otherwise you should do something like this:

    int currentValue=Convert.ToInt32(_cyclelist.SelectedItem.Value);

    Author Comment


    it looks like a postback issue.

    1st question: why is page_load called when I click a button?  It's actually called before my onclick code. I wouldn't expect that behavior.
    2nd question: I populate the radio buttons on post back then once I select the radio button I don't want to re-populate them.  Any suggestions?  Basically I can't use the simple (!isPostBack) becuase I already use that to generate the radio buttons.

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
    Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    728 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

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now