2015-05-06 54 views
0

我想要做的是在ASP.Net WebForm的服務器往返過程中顯示一條消息。在我的WebForm上,我有兩個按鈕,例如更新和取消按鈕。 如果單擊更新或取消按鈕,則應始終顯示消息。 如果單擊更新按鈕,則應該驗證是否在TextBox中給出了值。 所以這是我的代碼:ASP.Net驗證和JavaScript更新和取消按鈕

<%@ Page Language="VB" %> 
<!DOCTYPE html> 
<script runat="server"> 
    Protected Sub ButtonValidated_Click(sender As Object, e As EventArgs) 
     Label1.Text = "ButtonValidated " & Now.ToLongTimeString & " " & TextBox1.Text 
    End Sub 

    Protected Sub ButtonUnvalidated_Click(sender As Object, e As EventArgs) 
     Label1.Text = "ButtonUnvalidated " & Now.ToLongTimeString & " " & TextBox1.Text 
    End Sub 
</script> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
    <title>Validators and JS</title> 
    <script type="text/javascript"> 
     function ShowValMsg(e, val) { 
      dv = $get("ProgressDiv"); 
      var r = true; 
      if (val != null) { 
       r = Page_ClientValidate(val); 
       if (r) { 
        dv = $get("ProgressDiv"); 
        dv.style.visibility = "visible"; 
       } 
      } else { 
       dv = $get("ProgressDiv"); 
       dv.style.visibility = "visible"; 
      } 
      return r; 
     } 
    </script> 
</head> 
<body> 
    <form id="form1" runat="server"> 
    <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager> 
    <div> 
     <asp:TextBox ID="TextBox1" runat="server" /> 
     <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="RequiredFieldValidator" ControlToValidate="TextBox1" ValidationGroup="ValGrp" /> 
     <br /> 
     <!-- this could be the update button --> 
     <asp:Button ID="ButtonJsValidated" runat="server" Text="JsValidated" OnClick="ButtonValidated_Click" CausesValidation="false" OnClientClick="return ShowValMsg(this, &quot;ValGrp&quot;);" /> 
     <!-- this could be the cancel button --> 
     <asp:Button ID="ButtonJsUnvalidated" runat="server" Text="JsUnvalidated" OnClick="ButtonUnvalidated_Click" CausesValidation="false" OnClientClick="return ShowValMsg(this, null);" /> 
     <br /> 
     <asp:Button ID="ButtonValidated" runat="server" Text="Validated" OnClick="ButtonValidated_Click" CausesValidation="true" ValidationGroup="ValGrp" /> 
     <asp:Button ID="ButtonUnvalidated" runat="server" Text="Unvalidated" OnClick="ButtonUnvalidated_Click" CausesValidation="false" /> 
     <br /> 
     <asp:Label ID="Label1" runat="server" Text="Label" /> 
     <div id="ProgressDiv" style="position:absolute;top:0px;left:300px;height:100px;width:100px;visibility:hidden;opacity: 1; z-index: auto" >Please wait ...</div> 
    </div> 
    </form> 
</body> 
</html> 

如果TextBox1是空的,我點擊ButtonValidated事後對ButtonJsUnvalidated它的工作原理是我想要的。 如果TextBox1爲空,我點擊ButtonJsValidated,然後在ButtonJsUnvalidated上沒有服務器往返。如果我第二次點擊ButtonJsUnvalidated。 所以我認爲,如果我點擊ButtonJsValidated就會出現問題。 我發現的所有討論都是再次點擊一個按鈕並再次驗證或如何重置驗證器(我也試過沒有成功)。

有沒有人知道解決方案?

回答

0

感謝來自ppedv.de的Hannes,我已經得到了解決方案。我的JS代碼,現在看起來是這樣的:

 function ShowValMsg(e, val, msg) { 
 
      if (msg == null) { 
 
       if (val != null) { 
 
        if (Page_ClientValidate(val)) { 
 
         Show(e); 
 
        } 
 
        return; 
 
       } else { 
 
        Show(e); 
 
        return; 
 
       } 
 
      } else { 
 
       if (val != null) { 
 
        if (Page_ClientValidate(val)) { 
 
         if (confirm(msg)) { 
 
          Show(e); 
 
          return; 
 
         } else { 
 
          return false; 
 
         } 
 
        } 
 
       } else { 
 
        if (confirm(msg)) { 
 
         Show(e); 
 
         return; 
 
        } else { 
 
         return false; 
 
        } 
 
       } 
 
      } 
 
     } 
 

 
     function Show(e) { 
 
      $get("ProgressDiv").style.visibility = "visible"; 
 
     }