2016-07-12 58 views
0

我從Page.IsValid始終處於Java腳本

http://www.aspsnippets.com/Articles/Enable-Disable-ASPNet-Validator-Client-Side-Validation-using-JavaScript-or-jQuery.aspx

樣品做工精細採取客戶端,但在後面的代碼,他不檢查頁面驗證樣品返回false即使我禁用驗證他只設置

protected void Button1_Click(object sender, EventArgs e) 
    { 

      ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('PostBack occured.');", true); 

    } 

當我更新的代碼背後來驗證的頁面一樣,

protected void Button1_Click(object sender, EventArgs e) 
    { 
     Page.Validate("Group1"); 
     if (Page.IsValid) 
     { 
      ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('PostBack occured.');", true); 
     } 
    } 

的Page.IsValid始終處於Java腳本在下面的返回false,即使我禁用了驗證的的RequiredFieldValidator與ID = 「ValName的」 爲:

<form id="form1" runat="server"> 
    <u>JavaScript</u> 
    <hr /> 
    UserName: 
<asp:TextBox ID="txtName" runat="server"></asp:TextBox> 
    <asp:RequiredFieldValidator ID="valName" ControlToValidate="txtName" runat="server" 
     ErrorMessage="*Required" ForeColor="Red" ValidationGroup="Group1" /> 
    <br /> 
    Enable Validation: 
<input type="checkbox" id="CheckBox1" onclick="ToggleValidator(this);" checked="checked" /> 
    <br /> 
    <asp:Button Text="Submit" runat="server" OnClick="Button1_Click" ValidationGroup="Group1" /> 
    <script type="text/javascript"> 
     function ToggleValidator(chk) { 
      var valName = document.getElementById("<%=valName.ClientID%>"); 
     ValidatorEnable(valName, chk.checked); 
    } 
    </script> 
    <br /> 
    <br /> 

</form> 

請針對此問題的任何幫助

回答

0

Page.IsValid是經過計算的服務器端,並且不知道您是否將客戶端的JavaScript驗證切換到了客戶端。 Serverside集團可以檢查wheter Checkbox1檢查忽略驗證即RUNAT =「服務器」添加到複選框:

<input type="checkbox" id="CheckBox1" onclick="ToggleValidator(this);" checked="checked" runat="server" /> 

,並確保你的服務器端的條款火災,如果頁面驗證或複選框未選中:

protected void Button1_Click(object sender, EventArgs e) 
{ 
    Page.Validate("Group1"); 
    if (Page.IsValid || !CheckBox1.Checked) 
    { 
     ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('PostBack occured.');", true); 
    } 
}