从代码中添加 javascript 会破坏页面上的验证!怎么处理呢?
我所拥有的是按钮,仅当文本框长度为 8 时才应打开另一个页面。必须从代码中添加 JavaScript,因为它不简单地调用 bla.aspx,它更像是 bla.aspx?id=4&code=234 等...
我在服务器端
button.Attributes.Add("onclick","javascript:window.open(bla.aspx)");
和客户端都有这段代码,
<asp:TextBox ID="policyNumberTxt" runat="server" MaxLength="8" CausesValidation="true"></asp:TextBox>
<asp:RegularExpressionValidator ID="policyNumberTxtRev" runat="server"
ControlToValidate="policyNumberTxt" ErrorMessage="Length must be 8."
ValidationExpression="{.{8}.}" ValidationGroup="bla" Display="Dynamic"></asp:RegularExpressionValidator>
<asp:Button ID="printBtn" CssClass="button" Text="Print" runat="server" CausesValidation="true" ValidationGroup="bla" />
发生的情况是,当我单击按钮页面 bla.aspx 打开时,甚至会触发回发。验证程序正在显示消息,但页面已打开并触发回发。
这要怎么处理呢?验证已损坏...
谢谢..
What I have is button that should open another page only if textbox length is 8. Javascript must be added from code because it does not simply calls the bla.aspx, it's more like bla.aspx?id=4&code=234 etc etc...
I have this code on server side
button.Attributes.Add("onclick","javascript:window.open(bla.aspx)");
on client side i have
<asp:TextBox ID="policyNumberTxt" runat="server" MaxLength="8" CausesValidation="true"></asp:TextBox>
<asp:RegularExpressionValidator ID="policyNumberTxtRev" runat="server"
ControlToValidate="policyNumberTxt" ErrorMessage="Length must be 8."
ValidationExpression="{.{8}.}" ValidationGroup="bla" Display="Dynamic"></asp:RegularExpressionValidator>
<asp:Button ID="printBtn" CssClass="button" Text="Print" runat="server" CausesValidation="true" ValidationGroup="bla" />
What happens is that when I click the button page bla.aspx opens, and even postback is trigered. Validator is showing the message, but page is opened and postback trigered.
How to handle this? Validation is broken...
Thanks..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
为了防止提交表单,请添加“return false;”
但这也会阻止表单进行回发!
请注意,传递的 url 应该用引号引起来。
而且你不需要“javascript:”前缀,因为“Pointy”说
我认为验证正则表达式应该更严格,只允许这样的字母和数字
To prevent submitting the form add "return false;"
but that would also prevent the form from doing postback at all!
note that the passed url should be enclosed in quotes..
And you don't need the "javascript:" prefix as 'Pointy' said
I think the validation regular expression should be more strict to allow only letters and numbers like this
尝试将客户端点击逻辑放在页面表单的 onsubmit 中?
Try putting the client click logic in the onsubmit of the page's form instead?
找到答案此处
Found answer here