从代码隐藏添加错误消息到验证摘要
我正在网络表单应用程序上执行如下操作;
protected void button_transfer_search_Click(object sender, EventArgs e) {
Page.Validate("val1");
if (!Page.IsValid && int.Parse(txtArrivalDateTrf.Text) + 5 < 10) {
return;
}
另外,我的 aspx 文件中有以下代码;
<div class="search-engine-validation-summary">
<asp:ValidationSummary ValidationGroup="transfer" runat="server" ShowMessageBox="false" />
</div>
我的问题是如何在返回之前向页面添加错误消息,以便验证摘要可以捕获并显示它。我知道我们可以在 mvc 中轻松做到这一点,但我还没有弄清楚如何在 Web 表单中做到这一点。谢谢 !
I am doing something like below on a web forms application;
protected void button_transfer_search_Click(object sender, EventArgs e) {
Page.Validate("val1");
if (!Page.IsValid && int.Parse(txtArrivalDateTrf.Text) + 5 < 10) {
return;
}
also, I have following code on my aspx file;
<div class="search-engine-validation-summary">
<asp:ValidationSummary ValidationGroup="transfer" runat="server" ShowMessageBox="false" />
</div>
my question is how to add an error message to the page before return so that validation summary can grab that and displays it. I know we can do this in mvc easily but I haven't figured out how to do that in web forms. thanks !
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
每当我发现这种情况时,我都会这样做:
在我的 ASPX 代码中,我有一个 ValidationSummary 控件,其中 ValidationGroup 设置为与
vGroup
相同的值。然后,在我通过代码隐藏加载了所需数量的 CustomValidators(或任何其他类型的验证器)后,我只需调用
对
Page.Validate()
的调用即可调用所有代码的 lambda 链接方法 -在插入的验证器后面,如果有任何返回 false,则该页面无效并返回且不执行任何代码。否则,页面返回有效值,并执行有效代码。Whenever I find this situation this is what I do:
And in my ASPX code I have a ValidationSummary control with a ValidationGroup set to the same value as
vGroup
.Then, after I have loaded as many CustomValidators (or any other kind of validators) by codebehind as I want I simply call
The call to
Page.Validate()
calls the lambda-linked method of all code-behind inserted validators and if any returns false the page is invalid and returned with no code executed. Otherwise, the page returns a valid value, and executes the valid code.