views:

190

answers:

3

I have this code:

<asp:RadioButtonList ID="rblExpDate" runat="server" >
    <asp:ListItem Selected="True" Text="No expiration date"></asp:ListItem>
    <asp:ListItem Text="Expires on:"></asp:ListItem>
</asp:RadioButtonList>

that I would like to be always, on page load, to mark the first option ("no expiration date"). However, if the user marks the second option and reloads, the second option is selected, even though I do this on page load:

rblExpDate.Items[0].Selected = true;
rblExpDate.SelectedIndex = 0;

Appreciate your help!

A: 

Are you sure that your code is not within a postback check?

        if (!Page.IsPostBack)
        {
            rblExpDate.Items[0].Selected = true;
            rblExpDate.SelectedIndex = 0;
        }

More to the point, this sounds like a strange requirement that doesn't sound very useable. Why have an option available if you are always going to override it?

Daniel Dyson
Daniel, the thing is that if the second option is checked, I enable a datepicker - and the requirement is that the datepicker will be disabled if the first option is selected.
Nir
The code is not within a postback check.
Nir
In this case, enable or disable the datepicker in the PreRender event. Then make any changes you like to the state of the rbl
Daniel Dyson
I can disable the datepicker but it still won't let me change the state of the rbl... I need a way to set its selected index.
Nir
A: 

What most probably happens is that the code executed on Page_Load runs first, setting the element you want. After that the SelectedIndexChanged event of the RadioButtonList fires and gets the selected item from the ViewState, overriding what the code did. You have to debug this, though.

Slavo
ViewState is checked during page load so this is unlikely to be the issue here. Nir is setting the values OnLoad, which is after the page has loaded and ViewState has been checked
Daniel Dyson
A: 

The solution I chose is in Javascript (mootools):

window.addEvent('domready', function() {
         $("rblExpDate_0").checked = true;
});

(I gave up on the .net side)

thanks for your help @Slavo and @Daniel Dyson.

Nir