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#

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))
                        {
                              conn.Open();
                              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";
                              //_cyclelist.UniqueID="DueDate";
                              _cyclelist.DataBind();
                        }

-- 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:

currentValue=_cyclelist.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?
kristenhazardAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

kristenhazardAuthor Commented:
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.
0
sajayCommented:
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

if(!Page.IsPostBack)
{
    //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)
{

}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
EBatistaCommented:
hi, instead of:
currentValue=_cyclelist.SelectedValue;

you shold use:
currentValue=_cyclelist.SelectedItem.Value;

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

int currentValue=Convert.ToInt32(_cyclelist.SelectedItem.Value);
0
kristenhazardAuthor Commented:
sajay,

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.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.