Asp.net验证,页面仍在发布 [英] Asp.net Validation, Page Still Posting Back

查看:124
本文介绍了Asp.net验证,页面仍在发布的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,在项目中遇到标准asp.net验证器的问题。



基本上我在表单中添加以下内容:



Hi guys, got a problem with the standard asp.net validators in a project.

Basically I am adding the following to my form:

<asp:TextBox ID="TextBox1" runat="server" ValidationGroup="MyGroup" CausesValidation="true"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" EnableClientScript="true" runat="server" ErrorMessage="RequiredFieldValidator" ControlToValidate="TextBox1" ValidationGroup="MyGroup"></asp:RequiredFieldValidator>
<asp:Button ID="Button1" runat="server" Text="Button" ValidationGroup="MyGroup" CausesValidation="true" onclick="Button1_Click" />





问题是虽然显示验证错误,但页面仍会回发。



在这一点上我会说,如果我把它放在同一个项目的.aspx页面中它可以正常工作。



然而,当我把它放在一个动态呈现的用户控件中时,我遇到了这个问题。



该项目也使用URL重写,但我不确定这是造成它的原因。任何想法?



Problem is although the validation error displays, the page still posts back.

It is at this point i will say that if i put this in an .aspx page in the same project it works as it should.

However when i put it in a user control which is dynamically rendered, i get this problem.

The project does use URL rewriting as well, but im not sure that this is causing it. Any ideas?

推荐答案

事实证明它与URL重写有关。我已经快速实现了MSDN文章中的自定义表单控件,以对抗帖子上的重写。控件的作用是从action属性中删除url,但是它也在提交操作时删除了



因此要解决此问题,需要添加以下内容自定义控件中的RenderAttributes覆盖,任何其他人都应该使用它。





As it turns out it was to do with the URL re-writing. I had Quickly implemented a custom form control off an MSDN article to combat the re-writing on the post back. What the control did was to remove the url from the action attribute, however it also removed the
on submit action.
So to solve the problem the following needs to be added to the RenderAttributes override in the custom control, should anyone else be using it.


if(this.Page.Validators != null && this.Page.Validators.Count >=1)
    writer.WriteAttribute("onsubmit", "javascript:return WebForm_OnSubmit();");


这篇关于Asp.net验证,页面仍在发布的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆