Steve Kinsey.Com

subscribe to the RSS Feed

Thursday, March 11, 2010

ASP.Net Ajax SelectedIndexChanged Bug With RadioButtonList and AJAX Update Panels

Selected Index Changed Bug With ASP.Net Ajax
Today I discovered a little bug when using AJAX update panels with radio button lists to update the panel. Here is my scenario:

I had a gridview inside an AJAX update panel and I had a radio button list on my page with two options (Active and Inactive). My goal was to update the content of the gridview depending on the option selected from the radio button list. Here is the code for my radio button list:

<asp:RadioButtonList ID="rblStatus" runat="server" AutoPostBack="True" RepeatDirection="Horizontal">
            <asp:ListItem>Active</asp:ListItem>
            <asp:ListItem Selected="True">Inactive</asp:ListItem>
            </asp:RadioButtonList>

And here is how my update panel looked:

        <asp:UpdatePanel ID="updMainGrid" runat="server" UpdateMode="Conditional">
        <ContentTemplate>
        <asp:GridView ID="gridView1" runat="server" Width="100%" AutoGenerateColumns="False" >
            <Gridview setup.....................>
        </asp:GridView>
        </ContentTemplate>
        <Triggers>
            <asp:AsyncPostBackTrigger ControlID="rblStatus" EventName="SelectedIndexChanged" />
        </Triggers>
        </asp:UpdatePanel>

The code behind for the SelectedIndexChanged event of the radio button list simply rebound the gridview depending on the option selected at the radio button. To my surprise this didn’t work, the first time I click “Active” it would work but after that it would not run the SelectedIndexChanged code again. However if I took the AJAX panel away it worked perfectly. Eventually I traced the problem down to the “Selected=”True”” option in the radio button. As soon as you have this on any of the options the AJAX panel breaks.

So now that I have the bug, how do I make sure that a default option is selected when the page first loads? I tried setting the property in the code behind and this still causes the same problem. Eventually I found a little piece of javascript that worked. Here is the script I used:

<script language="JavaScript" type="text/javascript">
document.getElementById('<Control Name>').checked = true; </script>

To use this simply change “Control Name”to the name of your radio button list control.

Bookmark and Share

Enjoy This Post?


  • Web Hosting Jack said,

    oh god good explanation…
    Cheers !!!

  • iBlack said,

    Yep , that’s really a nasty bug. I’m using the same thing like yours’ a little different.

    got 1 radiobuttonlist and only one list item inserted manually (like default choice, and of course i need it to be selected on page load) and the other items are loaded from a sequel database source, that defines some options for filtering my gridview.

    Ajaxmanager takes the radiobuttonlist actions for notify grid to be updated.

    BUT …. as you said … the adding of attribut SELECTED=”true” on my default listitem makes it unable to fire the indexchanged event.

    without ajax it works.

    your js script doesn’t help my case.

Add A Comment

CommentLuv Enabled