我有一个ASP.Net RadioButtonList控件,AutoPostBack设置为true。我还有一个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>
在上面的代码示例中,当用户从默认选择(例如,“Master's Degree”)切换到不同选项(例如,“Doctorate”)时,则触发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控件将仅在我选择当用户到达页面时默认选择 not 的ListItem时回发。如果用户随后选择原始默认选择,则不会发生回发。
为了澄清,如果我回发而不将适用的第二个控件放在ASP.Net AJAX UpdatePanel中,那么无论用户选择哪个RadioButtonList ListItem,SelectedIndexChanged都能正常工作。另一方面,在将控件放在UpdatePanel中以应用AsyncPostBackTrigger之后,仅对非默认ListItem的选择进行回发。
可能相关的更多信息是我使用Microsoft Visual Studio 2008进行编码,而我正在使用的UpdatePanel控件是Microsoft AJAX Libary(不是ASP.NET AJAX Control Toolkit)的一部分。
我很感激有关如何使ASP.Net AJAX工作的任何见解,以便当用户选择RadioButtonList中的默认或非默认ListItem时,我可以触发SelectedIndexChanged事件。为了记录,我尝试在page_load事件中设置RadioButtonList默认选择,但它没有帮助。
提前感谢您提供的任何帮助!
答案 0 :(得分:4)
我遇到了同样的问题,发现生成的HTML在默认选择中没有点击处理程序。列表中的其他单选按钮都有点击处理程序来调用__doPostBack。所以我添加了一些jQuery:
$("#rbl input[checked]").click(function (e)
{
var id = e.target.id.replace('_', '$');
setTimeout(function () { __doPostBack(id, ""); }, 0);
});
但是,即使ajax调用现在按预期工作,服务器端代码仍然没有执行selectedIndexChanged处理程序。
认为这可能是因为服务器知道默认选择是什么,并且看到发布的选择是相同的,因此认为所选索引没有改变,因此没有执行处理程序。
然后我认为当选择其中一个无线电时,服务器应该记住通过视图状态。这让我记得我在radiobuttonlist的容器上有viewstatemode="Disabled"
。所以我在radiobuttonlist上设置viewstatemode="Enabled"
,它现在按预期工作。
答案 1 :(得分:2)
微软表示这是设计原因。它们提供了2种解决方法,但这两种解决方法都存在可能阻碍实际解决方案的问题。
http://connect.microsoft.com/visualstudio/feedback/details/508710/radiobuttonlist
答案 2 :(得分:0)
我有这个问题,所以它解决了这个问题,你必须在C#代码(加载页面)和html标签中设置AutoPostBack和SelectedIndexChange事件处理程序。例如:
protected void Page_Load(object sender, EventArgs e)
{
rblHighestDegree.AutoPostBack = true;
rblHighestDegree.SelectedIndexChanged +=
new EventHandler (rblHighestDegree_SelectedIndexChanged);
}
在Html中:
<asp:RadioButtonList ID="rblHighestDegree" runat="server" AutoPostBack="true"
onselectedindexchanged="rblHighestDegree_SelectedIndexChanged">
答案 3 :(得分:0)
我在后面的代码中构建了单选按钮列表,并且离开了SelectedIndex = 0,然后在onload中我使用JavaScript调用了第一个项目的click()。这是对我有用的最简单的事情。
答案 4 :(得分:0)
我知道这是一个老问题,但正如Jeff Shepler所说,默认选项没有点击处理程序,部分回发不会创建一个。
我通过将第一个radiobuttonlist包装在UpdateModel =“Always”(这是默认值)的新更新面板中来解决这个问题,因此它可以通过任何回发进行更新。
由于特定的表单布局,我无法使用将两个radiobuttonlists放在同一个更新面板中的解决方案。