2014-07-11 47 views
0

我的驗證器都不起作用。請幫忙!在此先感謝...ASP.NET必填字段驗證器和比較驗證器不工作

if (rfvEmail.IsValid && rfvLoginName.IsValid && rfvNewPassword.IsValid 
       && rfvConfirmPassword.IsValid && cvComparePasswords.IsValid) 
      { 
       DataSet ds = new DataSet(); 
       myDal.ClearParams(); 
       myDal.AddParam("@EmailAddress", txtEmail.Text); 
       myDal.AddParam("@LoginName", txtLoginName.Text); 
       myDal.AddParam("@NewLoginPassword", txtNewPassword.Text); 
       ds = myDal.ExecuteProcedure("spResetPassword"); 

       lblPasswordMessage.Text = ds.Tables[0].Rows[0]["result"].ToString();     
      } 

源代碼如下所示: 我也設置的CausesValidation爲true按鈕

<asp:RequiredFieldValidator 
        ID="rfvConfirmPassword" 
        runat="server" 
        ErrorMessage="Password Confirmation is required!" 
        ControlToValidate="txtConfirmPassword" 
        EnableClientScript="False" 
        ForeColor="Red"></asp:RequiredFieldValidator>     

       <asp:CompareValidator 
        ID="cvComparePasswords" 
        runat="server" 
        ControlToCompare="txtNewPassword" 
        ControlToValidate="txtConfirmPassword" 
        EnableClientScript="false" 
        ForeColor="Red" 
        ErrorMessage="Passwords entered by you do not match!"> 
       </asp:CompareValidator> 

我試圖驗證在保存按鈕的控件:

protected void btnSavePassword_Click(object sender, EventArgs e) 
    { 

     rfvEmail.Validate(); 
     rfvLoginName.Validate(); 
     rfvNewPassword.Validate(); 
     cvComparePasswords.Validate(); 
     resetPassword(); 
     } 
+0

哪些是對驗證的定義,即HTML部分? – entropic

+0

你是否在任何地方調用過'Page.Validate()'? – Dai

+0

我試圖在保存按鈕中調用Page.Validate,但它沒有幫助。 – Nupur

回答

3

乍一看,它似乎是你正在驗證錯誤的方式。如果沒有進一步的信息,很難診斷確切的問題,所以我會提供一些一般性建議。您需要熟悉APS.net Page Life Cycle。在提供的鏈接中,請注意加載,驗證 & 回發事件處理上演。 驗證階段自動發生,並驗證頁面上的每個驗證控件,並將Page.IsValid設置爲truefalse。這意味着你不需要驗證每個控制個體。

還需要注意的是驗證負載回發事件處理之前。通過爲控件分配默認值,可能會混淆PageLoad事件中的驗證。由於這個原因,!Page.IsPosback通常用於僅在初始頁面加載時設置控件,而不是在後續回發時設置。在回發之前,請勿在完成控制之前重置控件。在這個例子中你的Click處理程序。

這裏是我會怎麼做:

//Page Load event 
protected void Page_Load(object sender, EventArgs e) 
{ 
    if(!Page.IsPostback) 
    { 
     //Set Control inital page load values etc in here 
    } 
} 


private void resetPassword() 
{ 
    //You should investigate try/catch blocks to handle database errors better 
    DataSet ds = new DataSet(); 
    myDal.ClearParams(); 
    myDal.AddParam("@EmailAddress", txtEmail.Text); 
    myDal.AddParam("@LoginName", txtLoginName.Text); 
    myDal.AddParam("@NewLoginPassword", txtNewPassword.Text); 
    ds = myDal.ExecuteProcedure("spResetPassword"); 
    lblPasswordMessage.Text = ds.Tables[0].Rows[0]["result"].ToString();   
} 

protected void btnSavePassword_Click(object sender, EventArgs e) 
{ 
    if(Page.IsValid) //The controls have already been validated now 
    { 
     resetPassword(); 

     //If you need to empty/reset fields on button click 
     //do it here. 
    } 

     //Unless you want to reset them regarless of the validity 
     //of the page. Then do it here. 
} 
+0

感謝它幫助!我是C#的新手,仍然在學習。感謝您抽出時間幫助我。對此,我真的非常感激。 – Nupur

+0

很高興幫助。我們都必須從某個地方開始。 ASP.net頁面生命週期可能需要一些習慣。這些東西通常會讓那些入門者感到尷尬。 –