使用的UpdatePanel和RadioButtonLists时,当你改变这些值回发不存在的,我们有一些“奇怪”的问题时,不会触发。RadioButtonList的预载设置导致的UpdatePanel触发重新设置为初始值
我已经成功在于:当在RadioButtonList的值在页面的预载设置,并且您重新选择这个初始值出现时追查问题。
我有ASP页面的完整代码和下面的代码隐藏。
ASPX:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="RBLPostBackIssue._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<div>
<asp:Label ID="Label1" runat="server"></asp:Label>
<asp:RadioButtonList ID="RadioButtonList1" AutoPostBack="true" OnSelectedIndexChanged="RadioButtonList1_SelectedIndexChanged" RepeatDirection="Horizontal" runat="server">
<asp:ListItem>Yes</asp:ListItem>
<asp:ListItem>No</asp:ListItem>
</asp:RadioButtonList>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:PlaceHolder ID="PlaceHolder1" runat="server">
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</asp:PlaceHolder>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="RadioButtonList1" EventName="SelectedIndexChanged" />
</Triggers>
</asp:UpdatePanel>
</div>
</form>
</body>
</html>
代码隐藏:
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace RBLPostBackIssue
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_PreLoad(object sender, EventArgs e)
{
if (!IsPostBack)
{
switch (new Random().Next(2))
{
case 0:
Label1.Text = "Initial value set to Yes";
RadioButtonList1.SelectedValue = "Yes";
break;
case 1:
Label1.Text = "Initial value set to No";
RadioButtonList1.SelectedValue = "No";
break;
}
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ShowHideLogic();
}
}
private void ShowHideLogic()
{
PlaceHolder1.Visible = RadioButtonList1.SelectedValue.ToLower().Contains("yes");
}
protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
{
ShowHideLogic();
}
}
}
出现以下情况。
- 在RadioButtonList随机设置为是第一负载或号
- 你改变在RadioButtonList和文本框的值将显示/隐藏相应。
- 您将该值更改回它的原始值,并且根本不发生回发。
- 无论您多久更改一次值,只有在您不选择初始值时才会发生回传。
PreLoad中初始值的设置来自我们不希望更改的遗留代码。任何改变都需要大量的测试,并重新部署到大量的其他项目中。
我发现的唯一的解决方案是不使用的RadioButtonList的触发器,但把它在UpdatePanel内。 Whislt在某些情况下这是一个解决方案,但它并不理想,因为UI不会允许这样做。
我想我的问题是,有其他人有这个问题,你知道另一种解决方案呢,还是我只是愚蠢和错过了一些东西明显?
我不敢相信我没有想到只是将RadioButtonList放在它自己的更新面板中,而不是将它封装在我正在使用的版本中。谢谢!! – 2009-06-03 12:43:33