在页面加载中我想调用一个自定义验证器来验证我的页面并显示相应的错误消息。 我怎么能这样做?
我在自定义验证程序中使用javascript。
答案 0 :(得分:2)
您可以使用Page.IsValid和Page.Validate()
http://weblogs.asp.net/rajbk/archive/2007/03/15/page-isvalid-and-validate.aspx
编辑后://这是一个工作样本:
JS:
<script type="text/javascript">
function EmpIDClientValidate(ctl, args) {
// the value is a multiple of 5 if the module by 5 is 0
args.IsValid = (args.Value % 5 == 0);
}
</script>
.aspx的:
<table>
<tr>
<td>
ID (multiple of 5):
</td>
<td>
<asp:TextBox runat="server" Width="200px" ID="EmpID" Text="12"/>
<asp:RequiredFieldValidator runat="server" ID="ValidateEmpID" ControlToValidate="EmpID"
ErrorMessage="ID is required" Display="dynamic">*
</asp:RequiredFieldValidator>
<asp:CustomValidator runat="server" ID="ValidateEmpID2" ControlToValidate="EmpID"
ClientValidationFunction="EmpIDClientValidate" ErrorMessage="ID must be a multiple of 5"
Display="dynamic" OnServerValidate="ValidateEmpID2_ServerValidate">*
</asp:CustomValidator>
</td>
</tr>
</table>
<br />
<asp:Button runat="server" Text="Submit" ID="Submit" OnClick="Submit_Click" /><br />
<br />
<asp:CheckBox runat="server" ID="chkEnableValidators" Checked="True" AutoPostBack="True"
Text="Validators enabled" OnCheckedChanged="OptionsChanged" />
<br />
<asp:CheckBox runat="server" ID="chkEnableClientSide" Checked="True" AutoPostBack="True"
Text="Client-side validation enabled" OnCheckedChanged="OptionsChanged" />
<br />
<asp:CheckBox runat="server" ID="chkShowSummary" Checked="True" AutoPostBack="True"
Text="Show summary" OnCheckedChanged="OptionsChanged" />
<br />
<asp:CheckBox runat="server" ID="chkShowMsgBox" Checked="False" AutoPostBack="True"
Text="Show message box" OnCheckedChanged="OptionsChanged" />
<br />
<br />
<asp:ValidationSummary runat="server" ID="Summary" DisplayMode="BulletList" HeaderText="<b>Please review the following errors:</b>"
ShowSummary="true" />
<asp:Label runat="server" ID="Result" ForeColor="magenta" Font-Bold="true" EnableViewState="False" />
代码隐藏:
protected void Page_Load(object sender, EventArgs e)
{
// To run all validators
Page.Validate();
// To run just one validator:
ValidateEmpID2.Validate();
}
protected void Submit_Click(object sender, EventArgs e)
{
if (Page.IsValid)
Result.Text = "Thanks for sending your data";
else
Result.Text = "There are some errors, please correct them and re-send the form.";
}
protected void ValidateEmpID2_ServerValidate(object source, ServerValidateEventArgs args)
{
try
{
args.IsValid = (int.Parse(args.Value) % 5 == 0);
}
catch
{
args.IsValid = false;
}
}
protected void OptionsChanged(object sender, EventArgs e)
{
// Examine all the validators on the back.
foreach (BaseValidator validator in Page.Validators)
{
// Turn the validators on or off, depending on the value
// of the "Validators enabled" check box (chkEnableValidators).
validator.Enabled = chkEnableValidators.Checked;
// Turn client-side validation on or off, depending on the value
// of the "Client-side validation enabled" check box
// (chkEnableClientSide).
validator.EnableClientScript = chkEnableClientSide.Checked;
}
// Configure the validation summary based on the final two check boxes.
Summary.ShowMessageBox = chkShowMsgBox.Checked;
Summary.ShowSummary = chkShowSummary.Checked;
}
答案 1 :(得分:0)
Page.Validate(),假设您使用的是CustomValidator服务器控件
答案 2 :(得分:0)
您可以将该功能称为
<body onload ="Functionname()">
如果javascript可以用作onload =“javascript:functionname();”