2013-06-19 64 views
0

目標

要驗證發生而不必實際啓動它。頁面加載後不會導致驗證強制驗證?

代碼

我標記的input元素與該用戶控件:

<vbp:DataRowTextBox ID="SecurityAnswer" runat="server" IsRequired="true" 
    RequiredErrorMessage="Please enter an answer for your security question." MaxLength="50" 
    Label="Your Answer" /> 

誰是內標記是這樣的:

<input type="text" id="textBox" runat="server" /> 
<asp:RequiredFieldValidator ID="requiredFieldValidator" CssClass="errorMessage" Display="None" 
    EnableClientScript="false" Text="&nbsp;" ValidationGroup="LHError" runat="server" 
    ControlToValidate="textBox" /> 
<asp:RegularExpressionValidator ID="regexValidator" CssClass="errorMessage" Display="None" 
    EnableClientScript="false" Text="&nbsp;" ValidationGroup="LHError" runat="server" 
    ControlToValidate="textBox" /> 
<asp:CompareValidator ID="compareValidator" CssClass="errorMessage" Display="None" 
    EnableClientScript="false" Text="&nbsp;" ValidationGroup="LHError" runat="server" 
    ControlToValidate="textBox" /> 
<asp:CustomValidator ID="customValidator" CssClass="errorMessage" Display="None" 
    EnableClientScript="false" Text="&nbsp;" ValidationGroup="LHError" runat="server" 
    ControlToValidate="textBox" /> 
<asp:RangeValidator ID="rangeValidator" CssClass="errorMessage" Display="None" 
    EnableClientScript="false" Text="&nbsp;" ValidationGroup="LHError" runat="server" 
    ControlToValidate="textBox" /> 

誰的車削驗證的和關閉代碼在用戶控件的Page_Load中看起來像這樣:

protected void Page_Load(object sender, EventArgs e) 
{ 
    if (this.ClientIDMode == ClientIDMode.Static) 
    { 
     this.textBox.ID = this.ID; 
    } 

    this.label.Attributes.Add("for", this.textBox.ClientID); 
    this.label.InnerHtml = string.Format("{0}:{1}", this.Label, EmitRequiredSup()); 

    this.requiredFieldValidator.Visible = this.IsRequired; 

    this.regexValidator.Visible = (this.Regexes != null); 
    if (this.regexValidator.Visible) 
    { 
     var regexes = string.Join("|", this.Regexes); 
     this.regexValidator.ValidationExpression = regexes; 
    } 

    this.compareValidator.Visible = !string.IsNullOrEmpty(this.ControlToCompare); 
    this.rangeValidator.Visible = !string.IsNullOrEmpty(this.RangeMinimumValue); 

    this.requiredFieldValidator.ControlToValidate = this.textBox.ID; 
    this.regexValidator.ControlToValidate = this.textBox.ID; 
    this.compareValidator.ControlToValidate = this.ID; 
    this.customValidator.ControlToValidate = this.textBox.ID; 
    this.rangeValidator.ControlToValidate = this.textBox.ID; 
} 

因此,在此示例中,我將所需的驗證器打開,因爲我的IsRequired屬性設置爲true。然而,當我標記一個LinkButton這樣的:

<asp:LinkButton CssClass="actionButton" ID="Submit" runat="server" OnClick="Submit_Click" ValidationGroup="LHError">Change Security Question</asp:LinkButton> 

當前成果

我不得不打電話Validate去發生的驗證。我究竟做錯了什麼?我認爲CausesValidation設置爲true(這是LinkButton的默認值)和ValidateGroup定義的,它會在頁面加載後自動運行驗證。

而且非常簡潔,按鈕的點擊事件,甚至無效數據IsValidtrue直到我打電話Validate

回答

0

這個問題的答案其實很簡單。我用ASP.NET TextBoxRequiredFieldValidatorLinkButton構建了一個測試應用程序。它按我的預期執行,自動在回發期間執行驗證。所以,在那之後我種知道的問題是 - 我正在做的所有動態控制工作Page_Load,但需要得到它在Init這樣做我推翻OnInit如下圖所示,所有的作品如預期:

protected override void OnInit(EventArgs e) 
{ 
    base.OnInit(e); 

    this.compareValidator.ID = string.Format("{0}_{1}", this.ID, this.compareValidator.ID); 

    if (this.ClientIDMode == ClientIDMode.Static) 
    { 
     this.textBox.ID = this.ID; 
    } 

    this.label.Attributes.Add("for", this.textBox.ClientID); 
    this.label.InnerHtml = string.Format("{0}:{1}", this.Label, EmitRequiredSup()); 

    this.requiredFieldValidator.Visible = this.IsRequired; 

    this.regexValidator.Visible = (this.Regexes != null); 
    if (this.regexValidator.Visible) 
    { 
     var regexes = string.Join("|", this.Regexes); 
     this.regexValidator.ValidationExpression = regexes; 
    } 

    this.compareValidator.Visible = !string.IsNullOrEmpty(this.ControlToCompare); 
    this.rangeValidator.Visible = !string.IsNullOrEmpty(this.RangeMinimumValue); 

    this.requiredFieldValidator.ControlToValidate = this.textBox.ID; 
    this.regexValidator.ControlToValidate = this.textBox.ID; 
    this.compareValidator.ControlToValidate = this.ID; 
    this.customValidator.ControlToValidate = this.textBox.ID; 
    this.rangeValidator.ControlToValidate = this.textBox.ID; 
} 
1

不,原因驗證嚴格用於客戶端驗證;服務器端驗證必須通過Page.IsValid屬性進行檢查。

+0

我明白我需要檢查'IsValid'屬性,但是我正在討論必須自己實例化'Validate'方法。我期待ASP.NET爲我完成的工作是我的觀點。 –