当页面上有自定义验证器时,为什么 .NET 会为按钮呈现 javascript?
我有两个问题 - 首先,当同一页面上有自定义验证器时,为什么 .net 会为 asp 按钮呈现 javascript onclick 事件;其次,如何摆脱 javascript?
当javascript关闭时它工作正常,所以我不知道它的意义是什么。这是一个小例子:
<form id="form1" runat="server">
<asp:Button ID="Button1" OnClick="Button1_Click" Text="test" runat="server" />
<asp:CustomValidator ID="CustomValidator1" runat="server"
OnServerValidate="Stuff_Validate" EnableClientScript="false">
</asp:CustomValidator>
</form>
这将为按钮生成以下 html:
<input type="submit" name="Button1" value="test" onclick="javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("Button1", "", true, "", "", false, false))" id="Button1" />
如果没有自定义验证器,它是:
<input type="submit" name="Button1" value="test" id="Button1" />
有什么想法吗?
谢谢,
安妮莉
I've got two questions - first of all, why does .net render a javascript onclick event for asp buttons when there's a custom validator on the same page, and secondly, how can I get rid of the javascript?
It works fine when javascript is turned off, so I don't know what the point of it is. Here's a mini example:
<form id="form1" runat="server">
<asp:Button ID="Button1" OnClick="Button1_Click" Text="test" runat="server" />
<asp:CustomValidator ID="CustomValidator1" runat="server"
OnServerValidate="Stuff_Validate" EnableClientScript="false">
</asp:CustomValidator>
</form>
This will generate the following html for the button:
<input type="submit" name="Button1" value="test" onclick="javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("Button1", "", true, "", "", false, false))" id="Button1" />
Without the custom validator, it's:
<input type="submit" name="Button1" value="test" id="Button1" />
Any ideas?
Thanks,
Annelie
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您查看
System.Web.UI.WebControls.Button
的作用,您就会明白原因:在
GetPostBackOptions()
中,如果页面上有任何验证器(在当前验证组),然后它将生成用于回发的脚本。然后在
AddAttributesToRender()
中,如果存在任何脚本,它们将在 onclick 事件中呈现。如果您需要解决这个问题,那么您将必须构建一个自定义按钮
If you look at what
System.Web.UI.WebControls.Button
does you can see why:In
GetPostBackOptions()
, if you have any validators on the page (in the current validation group), then it will generate a script for the postback.Then in
AddAttributesToRender()
, if any scripts are present, they are rendered out in the onclick event.If you need to get around this, then you are going to have to build a custom Button
添加 javascript 是因为自定义验证是通过 javascript 完成的。它本来就应该在那里。您认为验证是如何完成的?如果你去掉了 javascript,验证将无法工作。
The javascript is added because the custom validation is done via javascript. It is meant to be there. How do you think the validation is done? If you got rid of the javascript the validation would not work.