ASP.Net Radio Button List Problem
im having some trouble with the radio button list in ASP.net, for some reason it wont let me select the 2nd item in the radio button list ( index of 1) when i select this item the selected item goes back to 0 (the first item). when i debug the code to see what the selected item is, it shows 0 for some reason, even though i press the 2nd item ( the index should be 1)
can any one think of what i might be doing wrong here ??
on the web f开发者_运维技巧orm side i have this
<asp:RadioButtonList ID="RadioButtonList1" runat="server" OnSelectedIndexChanged="RadioButtonList1_SelectedIndexChanged" AutoPostBack="true">
</asp:RadioButtonList>
then on the script side, the radio button list is populated by going through an array like so
for (i = 0; i < answersJArray[i].Length; i++)
{
RadioButtonList1.Items.Add(answersJArray[i].ToString());
}
It would be good if you could show us a little of your codebehind or describe your process and events a little more thoroughly. I'd say offhand that you must be databinding the selection of the radiobuttonlist or initializing its selection by some criteria on page load.
If either of these are correct, you'll want to insert a !IsPostBack conditional to make sure you aren't overriding any potential events by rebinding the control inappropriately or reassigning its selected option programmatically.
Yes, you're re-binding the data before you check it (thus the selection changes).
At what time in the page lifecycle are you checking the value? And also, when you do perform databinding?
It's best if you can bind in the markup (via some sort of datasource, I typically use an ObjectDataSource
, but whatever is fine).
If you don't do this, you need to prevent databinding when the page is in "PostBack" mode, otherwise it'll be too late to check the value.
I had the same problem.
I discovered that it is because the value of the listItem
(the same value of two distinct listItems).
The listItem
is constructed by text
and value
:
- The
text
is used to display in the page. - The
value
is used in for other stuff.
If you add two ListItems with the same value, you will get that behaviour: When click in an item, the first one is selected.
精彩评论