2013-10-22 118 views
0

功能C#ASP.net站點中間的Ajax面板更新。功能中間的Ajax面板更新

這是按鈕點擊。我希望在函數的其餘部分繼續之前將LbError.Text更新爲「」 。這是我現在的代碼。

protected void BUpload_Click(object sender, EventArgs e) 
     { 
      LbError.Text = ""; 

      UpdatePanel1.Update(); //// need it to update here before it moves on 
            but it waits till the end to update the lablel 
     Exicute functions..... 
     ....... 
     ....... 
     } 





<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional"> 
<contenttemplate> 
<asp:Label ID="LbError" runat="server" CssClass="failureNotification" Text=""></asp:Label> 
<br /><br /> 
<br /><br /> 
    <asp:TextBox ID="NewData" runat="server"></asp:TextBox><br /> 
    then click 
    <asp:Button ID="BUpload" runat="server" Text="Upload New Data" 
     onclick="BUpload_Click"/><br /> 

我試過的東西包括有另一個UpdatePanel只是和相同的結果。任何幫助將不勝感激。

回答

1

您可以使用PageRequestManager來完成這項工作。這裏是工作示例。 標記:

<asp:ScriptManager ID="sm" runat="server" /> 
     <script type="text/javascript"> 
      var prm = Sys.WebForms.PageRequestManager.getInstance(); 
      prm.add_initializeRequest(InitializeRequest); 
      function InitializeRequest(sender, args) { 
       $get('LbError').textContent = ''; 
      } 
     </script> 
     <asp:Label ID="LbError" runat="server" CssClass="failureNotification" Text="This is a label"></asp:Label> 
     <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional"> 
      <ContentTemplate> 
       <br /> 
       then click 
       <asp:Button ID="BUpload" runat="server" Text="Upload New Data" 
        OnClick="BUpload_Click" /><br /> 

       <asp:Label ID="Label1" runat="server" CssClass="failureNotification" Text=""></asp:Label> 
      </ContentTemplate> 
     </asp:UpdatePanel> 

和代碼背後的C#:

protected void BUpload_Click(object sender, EventArgs e) 
{ 
    UpdatePanel1.Update(); 
    System.Threading.Thread.Sleep(5000); 
    Label1.Text = "Done"; 
}