我遇到了UpdatePanel(ASP.Net WebForms,.Net 4.0)的问题。这是代码:
<div class="container-fluid">
<form id="form1" runat="server">
<h2>Poruke</h2>
<div class="row">
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePartialRendering="true" />
<asp:UpdatePanel ID="msgListUpdatePanel" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="False">
<ContentTemplate>
<div class="col-md-4">
<asp:ListBox ID="msgList" runat="server" OnSelectedIndexChanged="msgList_SelectedIndexChanged" AutoPostBack="true" ClientIDMode="AutoID"></asp:ListBox>
</div>
<div class="col-md-8">
<asp:ListBox ID="conversationList" runat="server" ClientIDMode="AutoID"></asp:ListBox>
<br class="divider" />
<p>
Odgovor: <span>
<asp:TextBox ID="replyTxtbox" runat="server"></asp:TextBox></span>
</p>
<asp:Button ID="sendBtn" runat="server" Text="Pošalji" OnClick="sendBtn_Click" EnableViewState="false" ClientIDMode="AutoID" />
</div>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="msgList" EventName="SelectedIndexChanged"/>
</Triggers>
</asp:UpdatePanel>
</div>
</form>
</div>
这是代码隐藏......
int userIdCookie = 0;
message selected = new message();
protected void Page_Load(object sender, EventArgs e)
{
if (!this.Page.User.Identity.IsAuthenticated)
{
FormsAuthentication.RedirectToLoginPage();
}
if (!Page.IsPostBack)
{
if (Int32.TryParse(HttpContext.Current.User.Identity.Name, out userIdCookie))
{
message msg = new message();
var allMsg = msg.allMessagesFormatted().Distinct().ToList();
msgList.DataSource = allMsg;
msgList.DataBind();
}
}
else
{
// test only!
replyTxtbox.Text = msgList.SelectedIndex.ToString();
msgListUpdatePanel.Update();
}
}
protected void msgList_SelectedIndexChanged(object sender, EventArgs e)
{
message msg = new message();
var allMsg = msg.allMessagesFormatted().Distinct().ToList();
msgList.DataSource = allMsg;
IList<message> boundList = (IList<message>)msgList.DataSource;
selected = boundList[msgList.SelectedIndex];
var conversation = msg.allMessagesFormatted().FindAll(x => x.conversationGuid == selected.conversationGuid);
conversationList.DataSource = conversation;
conversationList.DataBind();
}
protected void sendBtn_Click(object sender, EventArgs e)
{
if(selected.recipientId != 0)
{
message newmsg = new message();
newmsg.senderId = userIdCookie;
newmsg.recipientId = selected.recipientId;
newmsg.subject = selected.subject;
newmsg.messageTxt = replyTxtbox.Text;
newmsg.conversationGuid = selected.conversationGuid;
newmsg.time = DateTime.Now;
newmsg.Add();
}
}
msgList填充得很好但是当我改变选择时,没有任何反应 - 它的SelectedIndex事件永远不会触发。如果我将AutoPostBack =“true”设置为此列表框,则会重新加载页面(这正是我要避免的)。
总结一下 - 当我点击UpdatePanel里面的ListBox中的项目时,没有任何反应(事件未被触发)。我想在选择的索引更改时避免页面重新加载。我已经尝试了十几个解决方案(ClientID,AsyncPostBack,“常规”PostBack触发器,我想我错过了一个简单的细节,这让我很生气。
有人可以帮忙吗?
编辑 - 正如@mason指出的那样,问题出现在包含message.ToString()
字符的重叠\r\n
方法中,这会导致回发问题。
答案 0 :(得分:0)
您将在浏览器的控制台中收到JavaScript错误。
未捕获的Sys.WebForms.PageRequestManagerServerErrorException: Sys.WebForms.PageRequestManagerServerErrorException:无效的回发 或回调参数。使用配置或&lt;%@ Page启用事件验证 EnableEventValidation =“true”%&gt;在一个页面中。出于安全考虑, 此功能验证回发或回调事件的参数 源自最初呈现它们的服务器控件。如果 数据有效且预期,使用 ClientScriptManager.RegisterForEventValidation方法 注册回发或回调数据以进行验证。 MsAjaxJs?v = c42ygB2U07n37m_Sfa8ZbLGVu4Rr2gsBo7MvUEnJeZ81:1 Uncaught Sys.WebForms.PageRequestManagerServerErrorException: Sys.WebForms.PageRequestManagerServerErrorException:无效的回发 或回调参数。使用配置或&lt;%@ Page启用事件验证 EnableEventValidation =“true”%&gt;在一个页面中。出于安全考虑, 此功能验证回发或回调事件的参数 源自最初呈现它们的服务器控件。如果 数据有效且预期,使用 ClientScriptManager.RegisterForEventValidation方法 注册回发或回调数据以进行验证。
如果使用以下内容,您可以看到更简单的版本:
msgList.DataSource = new List<string>(){"A\r\n","B\r\n","C\r\n"};
当您在浏览器选项卡中看到它时,您会看到发送到服务器的POST请求,但在服务器端,Page_Load
方法根本不会被命中。
修复是要么不在用于ListBox的数据中使用\r\n
个字符,要么按照指示在ClientScriptManager.RegisterForEventValidation on MSDN注册它。