views:

412

answers:

2

hey all,

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 form 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());
    }
+1  A: 

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.

Noon Silk
+2  A: 

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.

Joel Etherton
the thing is, i have a similar piece of code on a prototype project iv been working on it works fine there, another thing that is weird is that i can select the thrid and forth items in the list fine. only when i select the 2nd item it sets the value back to the first item for some reason !! any ideas ??
c11ada
@c11ada: Yah, I helped a co-worker out with this exact behavior yesterday on a rbl. He had to dig deep into some of his code but he found a line where he was setting the 2nd option on the list in the code. What this ended up doing is causing the JS postback code not to recognize the change.
Joel Etherton
@Joel Etherton rookie mistake on my end, after looking into my code in more depth and looking at the data which was being dumped into the radiobuttonlist i found out that the first two items had the same text (12 and 12) ... changed the 2nd one to 13 and this solved the problem. i didnt know that the text has an effect on the selected index !! thanks for the help !!
c11ada