ViewState Handling for Radiobuttonlists in ASP.NET
Used the past hour to figure this out:
When you dynamically generate a RadioButtonList with
System.Web.UI.WebControls.ListItem options handling of viewstate is not done by
each ListItems ID property, but based on "Name" and "Value".
Given that the name of the radiobuttonlist object is Default1 the ListItems
are generated by ASP.NET like this:
<input id="Default1_0"
type="radio" name="Default1" value="1" />
<input id="Default1_1"
type="radio" name="Default1" value="2" />
In my application the name and value are provided by a user, and should of
course not be equal. Nevertheless one would assume that ASP.NET handeled the
postback and assigned the SelectedItem/SelectedIndex based on the ID field of
the selected option (when it does go out of the way in adding the "_NUMBER"
after the name).
However, when you have several options with the same [name] and [value]
values the selected index of the radiobuttonlist will always be the first one in
the list, not related to it's ID. So make sure your generated html doesnt look
like this:
<input id="Default1_0"
type="radio" name="Default1" value="1" />
<input id="Default1_1"
type="radio" name="Default1" value="1" />