2010-07-02 152 views
2

這還沒有發生到我面前,但由於某些原因服務器端驗證事件不會被觸發: 我使用下面Сustom服務器端驗證

<asp:CustomValidator runat="server" id="RefId" OnServerValidate="Ref_Id" ControlToValidate="txtRefId" ErrorMessage="Referral ID is Required." ></asp:CustomValidator> 

代碼當我在下面的代碼上修復調試器時,代碼將不會被觸發。也檢查下面的代碼。

Protected Sub Ref_Id(ByVal source As System.Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs) 
    Dim isPresent As String 
    isPresent = MembershipManager.IsReferalApproved(Me.txtRefId.Text) 
    If isPresent <> "" Then 
     addReferralName() 
     args.IsValid = True 
    Else 
     lblRefNotValid.Text = "Referral IDNO does not exist." 
     lblRefNotValid.Visible = True 
     Me.txtRefName.Text = "" 
     args.IsValid = False 
    End If 
End Sub 
+0

你可以用'e.Value',而不是'Me.txtRefId.Text'因爲它們是相同。 'CustomValidator'從'ControlToValidate'獲取'ValidationProperty'屬性值。對於'TexBox'這是'Text'屬性。 – abatishchev 2010-07-02 12:54:34

回答

0

請問您的txtRefId Autopostback和CausesValidation?

+0

我相信TextBox不會做任何(自動)回發。只有CustomValidator會這樣 - 因爲調用代碼隱藏方法 – abatishchev 2010-07-02 13:17:33

+0

您處於錯誤的軌道上。 http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.textbox.autopostback.aspx 驗證器不回發。它是驗證Codebehind當你做一個Page.Validate() – 2010-07-02 13:41:14

1

您的自定義驗證的控制應該具備的使用性能ValidateEmptyText = True或驗證不會在一個空的文本框觸發

+0

我在添加ValidateEmptyText = True,但仍然在服務器端驗證未觸發,只有當我從此asp標記中消除Controltovalidate時驗證觸發。但我的驗證函數每次調用。 – Amol 2010-07-02 13:27:17

+0

加回你的controltovalidate。在文本框中放置一些默認文本。查看驗證是否觸發。 – MrMagoo 2010-07-02 14:24:20