LCJ*_*LCJ 35 asp.net validation
我有以下代码RequiredFieldValidator
.该EnableClientScript
属性在验证控件中设置为"false".我也在浏览器中禁用了脚本.
我没有Page.IsValid
在后面的代码中使用.不过,当我在文本框中提交没有任何价值时,我会得到error message
.
根据@Dai的评论,我发现这可能是一个问题,如果有任何代码Page_Load
在a中执行postback
.不会抛出任何验证错误.
(但是,对于按钮单击处理程序,无需检查Page.IsValid
)
if (Page.IsPostBack)
{
string value = txtEmpName.Text;
txtEmpName.Text = value + "Appended";
}
Run Code Online (Sandbox Code Playgroud)
题
Page_Load
?Page.IsValid
?Page.IsValid
;但是说明了什么是强制性使用场景Page.IsValid
更新1
Page.IsValid
只有在之后Page.Validate()
隐式调用的run方法之后才能访问Page_Load
.如果您将所有逻辑保存在Page_Load事件处理程序中(非常不鼓励!),请Page.Validate()
在检查之前调用Page.IsValid
.
注意:建议不要保留所有逻辑Page_Load
.如果按钮单击事件发生了某些事情,请将其移动到按钮单击事件处理程序.如果在下拉事件中发生某些事情,请将其移至下拉选定的项目更改事件处理程序.
更新2
看起来,我们需要添加If(Page.IsValid)
的button click
,如果我们使用的是也Custom Validator
与服务器端验证.请参阅CustomValidator不能正常工作.
注意:此处存在客户端验证问题:是否使用Page_IsValid或Page_ClientValidate()(对于客户端事件)
MARKUP
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript">
alert('haiii');
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ValidationSummary runat="server" ID="vsumAll" DisplayMode="BulletList" CssClass="validationsummary" ValidationGroup="ButtonClick" />
<asp:TextBox ID="txtEmpName" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="valEmpName" runat="server" ControlToValidate="txtEmpName"
EnableClientScript="false" ErrorMessage="RequiredFieldValidator" Text="*" Display="Dynamic"
ValidationGroup="ButtonClick"></asp:RequiredFieldValidator>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" ValidationGroup="ButtonClick" />
</div>
</form>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
代码背后
protected void Button1_Click(object sender, EventArgs e)
{
string value = txtEmpName.Text;
SubmitEmployee(value);
}
Run Code Online (Sandbox Code Playgroud)
参考文献:
Jai*_*res 35
验证发生Page_Load
在事件处理程序之后(参见http://msdn.microsoft.com/en-us/library/ms178472(v=VS.100).aspx).
如果您的按钮未导致验证,则必须手动触发 Page.Validate.
您可能不会Page.IsValid
在(1)您打电话Page.Validate
或(2)控件之后询问,导致验证是回发的来源/包含在回发中.
如果您需要在事件处理程序触发之前进行验证,您可以使用:
if (Page.IsPostback)
{
Page.Validate( /*Control Validation Group Name Optional*/ );
if (Page.IsValid)
{
//Do some cool stuff
}
}
Run Code Online (Sandbox Code Playgroud)
您可能还需要考虑重新设计,因此您无需这样做.
在处理导致验证的控件的事件处理程序中,Page.IsValid
保证可用.在所有其他情况下,重新请求验证通常更安全.用于处理具有验证器的表单上的提交的一种模型:
void btnSubmit_Click(object sender, EventArgs e)
{
this.UpdateGUIWithSubmitRequest();
if (Page.IsValid)
{
this.ProcessSuccessfulSubmission();
}
else
{
this.ProcessInvalidSubmission();
}
}
Run Code Online (Sandbox Code Playgroud)
如果您使用的CustomValidator
是具有非常昂贵的验证步骤,则可以考虑将结果缓存在中,HttpResponse.Cache
这样您就不必重新验证是否发生了对Page.Validate的多次调用.
void CustomValidator_ServerValidate(object source, ServerValidateEventArgs args)
{
CustomValidator self = (CustomValidator)source;
string validatorResultKey = self.ClientID;
bool? validatorResult = Context.Items[validatorResultKey] as bool?;
if (validatorResult.HasValue)
{
args.IsValid = validatorResult.Value;
return;
}
bool isValid = this.DoSomethingVeryTimeConsumingOrExpensive();
Context.Items[validatorResultKey] = isValid;
args.IsValid = isValid;
}
Run Code Online (Sandbox Code Playgroud)
当然,这取决于您的体系结构,以及您是否能够假设在初始验证期间通过/失败的验证在同一页面生命周期的后续验证期间仍然通过/失败.
归档时间: |
|
查看次数: |
60001 次 |
最近记录: |