简体   繁体   English

.Net将“__doPostBack”添加到javaScript onClick事件中......为什么?

[英].Net adds “__doPostBack” to javaScript onClick event… why?

Typically.NET handles the post back event on a button by making the button a type="submit": 通常,.NET通过使按钮为type =“submit”来处理按钮上的回发事件:

<input type="submit" Text="Delete" />

However, I've recently bumped into a situation where .NET is handling the post back by making the input a type="button" and then adding a javaScript onClick event to do the "__doPostBack()" - so you end up with this: 但是,我最近碰到了一种情况,即.NET通过输入一个type =“button”来处理回发,然后添加一个javaScript onClick事件来执行“__doPostBack()” - 所以你最终得到这个:

<input type="button" onClick="__doPostBack(...);" />

The problem with this is trying to add confirmation messages to the click. 这样做的问题是尝试向点击添加确认消息。 So with the 2nd scenario, I end up with this: 所以在第二个场景中,我最终得到了这个:

<input type="button" onClick="return confirm('Are you sure?'); __doPostBack(...);" Text="Delete" />

Of course, in the above scenario the postback will never happen. 当然,在上面的场景中,回发永远不会发生。

What I'm wondering is why .net chooses one scenario over there other and is there a way to prevent the 2nd? 我想知道的是为什么.net在那里选择一个场景,有没有办法阻止第二个?

Thanks 谢谢

You need to have output like: 您需要输出如下:

<input type="button" onClick="if(!confirm('Are you sure?')){return false};__doPostBack(...);" Text="Delete" />

By having it return if the confirm is true or false, you will not execute the postback. 如果确认为真或假,则返回它,您将不会执行回发。 However if you only return on false, you should be fine. 但是,如果你只是假的,你应该没事。

The __doPostBack enables .net to add extra functionality such as client side validation checks. __doPostBack使.net能够添加额外的功能,例如客户端验证检查。 If it needs to do it that way, it will do. 如果它需要这样做,它会做。

In regard to your implementation, you could use the OnClientClick property of the button to set any additional scripts. 关于您的实现,您可以使用按钮的OnClientClick属性来设置任何其他脚本。

The answer to this is: 答案是:

.Net uses __doPostBack(...) in the onClick event when working in an ajax scenario. 在ajax场景中工作时,.Net在onClick事件中使用__doPostBack(...)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM