asp.net-ajax - ASP.Net AJAX UpdatePanel 无法触发 SelectedIndexChanged 事件

标签 asp.net-ajax updatepanel radiobuttonlist selectedindexchanged

我有一个 AutoPostBack 设置为 true 的 ASP.Net RadioButtonList 控件。我还有一个 OnSelectedIndexChanged 函数,每当用户更改选择时都会调用该函数。最后,要求用户提供这个控件请求的信息,所以当用户到达页面时我有一个默认选择——

<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>

在上面的代码示例中,当用户从默认选择(例如“硕士学位”)切换到不同的选项(例如“博士”)时,会触发 SelectedIndexChanged 事件。然后,如果用户改变主意,随后选择了原来的默认选项,则再次触发 SelectedIndexChanged 事件。这完全按预期和预期工作。

我有第二个控件可以启用或禁用,具体取决于上面的选择,在代码隐藏中...
<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>

只有在我将第二个控件放置在 ASP.Net AJAX UpdatePanel 中以启用异步回发(如上所示)后,才会出现我遇到的问题。执行此操作后,原始 RadioButtonList 控件只会在我选择用户到达页面时默认未选择的 ListItem 时回发。如果用户随后选择原始默认选择,则不会发生回发。

澄清一下,如果我没有在 ASP.Net AJAX UpdatePanel 中放置适用的第二个控件的情况下回发,则无论用户选择哪个 RadioButtonList ListItem,SelectedIndexChanged 都有效。另一方面,在我将控件放置在 UpdatePanel 中以应用 AsyncPostBackTrigger 之后,回发仅发生在非默认 ListItems 的选择上。

可能相关的更多信息是我正在使用 Microsoft Visual Studio 2008 进行编码,并且我使用的 UpdatePanel 控件是 Microsoft AJAX Libary(不是 ASP.NET AJAX 控件工具包)的一部分。

我很感激有关如何让 ASP.Net AJAX 工作的任何见解,以便我可以在用户选择 RadioButtonList 中的默认或非默认 ListItems 时触发 SelectedIndexChanged 事件。作为记录,我尝试在 page_load 事件中设置 RadioButtonList 默认选择,但没有帮助。

在此先感谢您的任何帮助!

最佳答案

我遇到了同样的问题,发现生成的 HTML 在默认选择上没有点击处理程序。列表中的其他单选按钮都具有调用 __doPostBack 的单击处理程序。所以我添加了一点 jQuery:

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

然而,即使 ajax 调用现在按预期工作,服务器端代码仍然没有执行 selectedIndexChanged 处理程序。

认为这可能是因为服务器知道默认选择是什么(是)并且看到发布的选择是相同的,因此认为所选索引没有更改,因此没有执行处理程序。

Then I thought that when one of the other radios was selected, the server should have remembered that through the viewstate.这让我想起我有 viewstatemode="Disabled"在我的单选按钮列表的容器上。所以我设置了viewstatemode="Enabled"在单选按钮列表上,它现在按预期工作。

关于asp.net-ajax - ASP.Net AJAX UpdatePanel 无法触发 SelectedIndexChanged 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12830135/

相关文章:

Asp.net 动态验证器在 Chrome 或 Safari 中不起作用

javascript - 我如何覆盖 ScriptResource.axd javascript 函数中内置的 asp.net

c# - 编辑记录时 RadGrid 中的 "Cannot unregister UpdatePanel with ID ' xxx ' since it was not registered with the ScriptManager... "

asp.net - 为什么 ajax 加载器图像总是使用 jquery 和 asp.net 显示

wpf - WPF 中是否需要基于 `RadioButtonList` 的自定义 `ListBox`?

ios - Swift 单选按钮不可点击

c# - 如何使用 AJAX 创建/删除带有表单控件的表行?

c# - 在 ASP.NET 的类中有 Response.Redirect 的坏习惯?

jquery - 使用 jQuery .load() 函数渲染部分 View

c# - 如何根据随机顺序将对象添加到 RadioButtonList 中?