Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

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
5
Medium Priority
?
281 Views
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))
                        {
                              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?
0
Comment
Question by:kristenhazard
  • 2
  • 2
5 Comments
 

Author Comment

by:kristenhazard
ID: 12589548
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
 
LVL 3

Accepted Solution

by:
sajay earned 2000 total points
ID: 12591064
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
 
LVL 10

Expert Comment

by:EBatista
ID: 12592418
hi, instead of:
currentValue=_cyclelist.SelectedValue;

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

regards
0
 
LVL 10

Expert Comment

by:EBatista
ID: 12592548
of course currentValue should be object type, otherwise you should do something like this:

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

Author Comment

by:kristenhazard
ID: 12595041
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

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Integration Management Part 2
Suggested Courses

578 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