ASP.Net AJAX UpdatePanel Fails to Trigger SelectedIndexChanged Event
Asked Answered
P

5

6

I have an ASP.Net RadioButtonList control with AutoPostBack set to true. I also have an OnSelectedIndexChanged function that is called whenever a user changes the selection. Finally, the user is required to provide the information requested for this control, so I have a default selection when the user arrives on the page--

<asp:RadioButtonList ID="rblHighestDegree" runat="server" AutoPostBack="true"
        onselectedindexchanged="rblHighestDegree_SelectedIndexChanged">
   <asp:listitem Runat="server" Text="Master's Degree" Selected="True" />
   <asp:listitem Runat="server" Text="Doctorate" />
</asp:RadioButtonList>

In the code sample above, when the user switches from the default selection (e.g., "Master's Degree") to a different option (e.g., "Doctorate"), then the SelectedIndexChanged event is triggered. Then, if the user changes his mind, and subsequently selects the original default option, the SelectedIndexChanged event is triggered again. This works precisely as intended and expected.

I have a second control that I enable or disable, depending on the selection above, in the code-behind...

<asp:ScriptManager ID="scriptmanager1" runat="server" />
<asp:UpdatePanel ID="updatepanel1" runat="server">
<ContentTemplate>
<asp:RadioButtonList ID="rdlYearsOfStudy" runat="server" Enabled="false">
<asp:listitem Runat="server" Text="Less than 3 years" />
<asp:listitem Runat="server" Text="3 - 4 years" />
<asp:listitem Runat="server" Text="5 - 6 years" />
 </asp:RadioButtonList>
</ContentTemplate>
<Triggers>
  <asp:AsyncPostBackTrigger ControlID="rblHighestDegree" EventName="SelectedIndexChanged" /> 
</Triggers>
</asp:UpdatePanel>

The problem I'm having only occurs only after I placed this second control within an ASP.Net AJAX UpdatePanel to enable an asynchronous postback (as shown above). Once I did this, the original RadioButtonList control will only postback when I select a ListItem that was not selected by default when the user arrived on the page. If the user subsequently selects the original default selection, the postback does not occur.

To clarify, if I postback without placing the applicable second control in an ASP.Net AJAX UpdatePanel, then SelectedIndexChanged works no matter which RadioButtonList ListItem the user selects. On the other hand, after I place a control inside an UpdatePanel to apply the AsyncPostBackTrigger, then the postback only happens for selections of non-default ListItems.

More information that might be relevant is that I'm coding with Microsoft Visual Studio 2008, and the UpdatePanel control that I'm using is part of the Microsoft AJAX Libary (not the ASP.NET AJAX Control Toolkit).

I would appreciate any insight with how to get ASP.Net AJAX to work such that I can get the SelectedIndexChanged event to fire when a user selects either the default or non-default ListItems in the RadioButtonList. For the record, I tried setting the RadioButtonList default selection in the page_load event, but it didn't help.

Thanks in advance for any and all help!

Pulpwood answered 10/10, 2012 at 23:44 Comment(2)
Note that I also attempted removing the triggers defined on the aspx page and, instead, registered the RadioButtonList control in the page_load event: "scriptmanager1.RegisterPostBackControl(rblHighestDegree);" Unfortunately, this approach resulted in the exact same behavior that I complained about above.Pulpwood
If you look at the generated HTML, you'll see that the default selected radio does not have an onclick set to call __doPostBack.Catawba
C
5

I had the same issue and found that the generated HTML did not have a click handler on the default selection. The other radio buttons in the list all have click handlers to call __doPostBack. So I added this bit of jQuery:

$("#rbl input[checked]").click(function (e)
{
    var id = e.target.id.replace('_', '$');
    setTimeout(function () { __doPostBack(id, ""); }, 0);
});

However, even though the ajax call was now working as expected, the server-side code was still not executing the selectedIndexChanged handler.

Thinking that it might be because the server knows what the default selection is (was) and sees that the posted selection is the same and so thinks the selected index was not changed and so didn't execute the handler.

Then I thought that when one of the other radios was selected, the server should have remembered that through the viewstate. Which caused me to remember that I had viewstatemode="Disabled" on the container of my radiobuttonlist. So I set viewstatemode="Enabled" on the radiobuttonlist and it's now working as expected.

Catawba answered 19/10, 2012 at 16:7 Comment(2)
Thank you for this response. I gave it a shot, but unfortunately, setting viewstatemode="Enabled" on the radiobuttonlist did not change the behavior or resolve my problem.Pulpwood
Simply enabling the viewstate is not enough. Did you attach the client-side click handler to call __doPostBack?Catawba
C
2

Microsoft states that it's by design. They offer 2 workarounds, but both workarounds have issues which may prevent an actual solution.

http://connect.microsoft.com/visualstudio/feedback/details/508710/radiobuttonlist

Cauliflower answered 17/12, 2013 at 16:55 Comment(1)
Your link is broken. Do you remember what the workarounds were?Ascidium
N
0

I have this problem so , it solved this way , you must set AutoPostBack and SelectedIndexChange event handler in C# code (Load Page) and in html tag . for eg :

    protected void Page_Load(object sender, EventArgs e)
    {
            rblHighestDegree.AutoPostBack = true;
            rblHighestDegree.SelectedIndexChanged += 
             new EventHandler  (rblHighestDegree_SelectedIndexChanged);
    }

And in Html :

    <asp:RadioButtonList ID="rblHighestDegree" runat="server" AutoPostBack="true"
    onselectedindexchanged="rblHighestDegree_SelectedIndexChanged">
Neilneila answered 2/7, 2013 at 11:18 Comment(0)
T
0

I built the radio button list in code behind, and left off the SelectedIndex = 0, then in onload I called click() of the first item using JavaScript. This is the simplest thing that worked for me.

Typhon answered 31/7, 2014 at 22:59 Comment(0)
S
0

I know this is an old question, but as Jeff Shepler said, there is no click handler for default option, and partial postback will not create one.

I got round this by wrapping the first radiobuttonlist in an a new update panel with UpdateModel="Always" (which is the default) so it is updated by any postback.

I couldn't use the solution of putting both radiobuttonlists in the same update panel because of the particular form layout.

Sagacity answered 24/8, 2016 at 12:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.