2015-02-05 150 views
1

我有UpdatePanel(ASP.Net WebForms,.Net 4.0)的问题。这里是代码:UpdatePanel触发器没有触发

 <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”设置为此列表框,它会重新加载页面(这正是我试图避免的)。

总结 - 当我点击ListBox里面的项目UpdatePanel时,没有任何反应(事件没有被触发)。我想在选定索引更改时避免页面重新加载。我试图解决方案(客户端ID,AsyncPostBack,十几“常规”回发的触发器,我想我错过了一个简单的细节和它的驾驶我疯狂

谁能帮

编辑 - ?作为@mason指出,问题是在含有\r\n字符引起回发的问题overidden message.ToString()方法。

回答

0

您将浏览器的控制台收到一个JavaScript错误。

未捕获Sys.WebForms.PageRequestManagerServerErrorExce ption: Sys.WebForms.PageRequestManagerServerErrorException:无效回传 或回调参数。事件验证在页面中使用配置或EnableEventValidation =“true”%>启用。为了安全起见, 此功能验证回发或回调事件参数 的参数源自最初呈现它们的服务器控件。如果 数据有效且预期,请使用 ClientScriptManager.RegisterForEventValidation方法,以便 注册回发或回调数据以进行验证。 MsAjaxJs V = c42ygB2U07n37m_Sfa8ZbLGVu4Rr2gsBo7MvUEnJeZ81:1未捕获 Sys.WebForms.PageRequestManagerServerErrorException: Sys.WebForms.PageRequestManagerServerErrorException:无效回发 或回调参数。事件验证在页面中使用配置或EnableEventValidation =“true”%>启用。为了安全起见, 此功能验证回发或回调事件参数 的参数源自最初呈现它们的服务器控件。如果 数据有效且预期,请使用 ClientScriptManager.RegisterForEventValidation方法,以便 注册回发或回调数据以进行验证。

你可以看到一个简单的版本,如果你使用:

msgList.DataSource = new List<string>(){"A\r\n","B\r\n","C\r\n"}; 

当您在浏览器标签看它,你会看到向服务器发送POST请求,但在服务器端的Page_Load方法根本不会被击中。

该修复方法是要么不在用于列表框的数据中使用\r\n字符,要么按照指示在ClientScriptManager.RegisterForEventValidation on MSDN处注册它。

+0

令人难以置信。当我添加msgList时它工作得很好。DataSource = new List (){“A”,“B”,“C”};' 问题是在'Message'类中覆盖'.ToString()'。我把'\ r \ n'放在方法中,而不是那样。当我删除它时,其他所有工作都很好。 问题解决! – nighthawk 2015-02-05 23:55:09

+1

@nighthawk我更新了解决方案。无论如何,我建议不要使用UpdatePanel。相反,切换到使用[ASP.NET Web API](http://www.asp.net/web-api)以及使AJAX变得轻松的客户端脚本框架(如jQuery)。 – mason 2015-02-06 00:14:06

+0

我可能会将它切换到WebAPI,因为UpdatePanel会导致很多问题(比如这个),并且会减慢页面的速度。 – nighthawk 2015-02-06 00:19:43