目標
要驗證發生而不必實際啓動它。頁面加載後不會導致驗證強制驗證?
代碼
我標記的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=" " ValidationGroup="LHError" runat="server"
ControlToValidate="textBox" />
<asp:RegularExpressionValidator ID="regexValidator" CssClass="errorMessage" Display="None"
EnableClientScript="false" Text=" " ValidationGroup="LHError" runat="server"
ControlToValidate="textBox" />
<asp:CompareValidator ID="compareValidator" CssClass="errorMessage" Display="None"
EnableClientScript="false" Text=" " ValidationGroup="LHError" runat="server"
ControlToValidate="textBox" />
<asp:CustomValidator ID="customValidator" CssClass="errorMessage" Display="None"
EnableClientScript="false" Text=" " ValidationGroup="LHError" runat="server"
ControlToValidate="textBox" />
<asp:RangeValidator ID="rangeValidator" CssClass="errorMessage" Display="None"
EnableClientScript="false" Text=" " 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
定義的,它會在頁面加載後自動運行驗證。
而且非常簡潔,按鈕的點擊事件,甚至無效數據,IsValid
是true
直到我打電話Validate
。
我明白我需要檢查'IsValid'屬性,但是我正在討論必須自己實例化'Validate'方法。我期待ASP.NET爲我完成的工作是我的觀點。 –