c#
  • asp.net
  • 2016-09-19 94 views 0 likes 
    0

    如果輸入的登錄電子郵件地址(用戶名)已存在於數據庫中,我寫了自定義驗證以防止用戶將信息保存在數據庫中。自定義驗證無法正常工作

    <div class="span3"> 
              <asp:TextBox ID="txtLoginEmailAddress" class="SearchTxtBox" runat="server" Text='<%# ds.Tables[0].Rows[0]["LoginEmailAddress"] %>' 
               MaxLength="50"> </asp:TextBox> 
             </div> 
             <div class="span6"> 
              <asp:RequiredFieldValidator ID="valLoginEmailAddressRequired" runat="server" CssClass="Validator" 
               ValidationGroup="save" Display="Dynamic" ControlToValidate="txtLoginEmailAddress" 
               ErrorMessage="<b>User Name is required</b>"></asp:RequiredFieldValidator> 
              <asp:CustomValidator ID="CheckAvailablity" runat="server" ValidationGroup="save" ControlToValidate="txtLoginEmailAddress" 
               OnServerValidate="ValidateUserName"></asp:CustomValidator> 
    
          <asp:Label ID="valDuplicatePassword" runat="server" style="color:red" Visible="False"></asp:Label></td> 
             </div> 
    
         <asp:Button ID="btnSave" runat="server" Text="Save" CssClass="btn btn-small " OnClick="btnSave_Click" 
               ValidationGroup="save" /> 
    
    在CS文件

     protected void ValidateUserName(object sender, ServerValidateEventArgs e) 
        { 
         SystemUserBL bl = new SystemUserBL(SessionContext.SystemUser); 
         ds = new DataSet(); 
         bl.FetchForLoginEmailAddress(ds, txtLoginEmailAddress.Text); 
    
         if (ds.Tables[0].Rows.Count > 0) 
         { 
          e.IsValid = false; 
          valDuplicatePassword.Visible = true; 
          valDuplicatePassword.Text = "<b>This User Name is already in use by another user.</b>"; 
          btnSave.Enabled = false; 
          btnSaveclose.Enabled = false; 
         } 
         else 
         { 
          e.IsValid = true; 
          btnSave.Enabled = true; 
          btnSaveclose.Enabled = true; 
          valDuplicatePassword.Visible = true; 
          valDuplicatePassword.Text = "<b>Congratulations! " + txtLoginEmailAddress.Text + " is available.</b>"; 
         } 
        } 
    
         protected void btnSave_Click(object sender, EventArgs e) 
        { 
         try 
         { 
          if (save()) 
          { 
           SucessMessage(); 
          } 
         } 
         catch (Exception ee) 
         { 
          ErrorMessage(ee.Message); 
         } 
        } 
    

    現在,當我們點擊保存按鈕將其保存在數據庫中後,會顯示確認消息。我希望它不應該保存在數據庫中,並且自定義驗證應該像其他ASP.NET的驗證一樣工作。請幫忙 !!!

    +0

    你如何保存的東西數據庫?它發生在btnSave_Click裏面嗎? – Andrei

    +0

    沒有...在btnsave點擊功能,只有保存功能writtem – Nida

    +0

    請發佈相關代碼 – Andrei

    回答

    1

    在繼續處理之前,您必須明確檢查頁面是否有效。所以你的情況:

    protected void btnSave_Click(object sender, EventArgs e) 
    { 
        try 
        { 
         if (this.IsValid()) //this checks that the page passed validation, including custom validation 
         { 
         if (save()) 
         { 
          SucessMessage(); 
         } 
         } 
         else 
         { 
         //any custom error message (additional to the validators themselves) should go here 
         } 
        } 
        catch (Exception ee) 
        { 
         ErrorMessage(ee.Message); 
        } 
    } 
    

    嚴格來講,適用於所有ASP.NET驗證器,但特別是如果作爲你的情況,你是不是使用客戶端驗證,你就更有可能遇到此問題。即使您的驗證器確實啓用了客戶端,您仍然應該進行此檢查 - 惡意用戶很容易繞過客戶端驗證並提交無效數據。

    看到這個博客的這個主題更詳細的討論:https://www.jardinesoftware.net/2012/03/31/net-validators-dont-forget-page-isvalid/

    相關問題