2015-02-05 119 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