.Net adds "__doPostBack" to the javaScript onClick event ... why?

Typically, .NET handles a post back event on a button, creating a type = "submit" button:

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

However, I recently came across a situation where .NET processes the message back by typing type = "button" and then adding a javaScript onClick event to execute "__doPostBack ()", so you end up with this:

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

The problem with this is an attempt to add confirmation messages to the client. So, with the second scenario, I get the following:

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

Of course, in the above script, postback will never happen.

What I wonder is why .net selects one script there another and is there a way to prevent the second?

thanks

+4
source share
3 answers

The answer to this question is:

.Net uses __doPostBack (...) in the onClick event when running in ajax script.

0
source

You should have the conclusion:

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

If it is returned, if the confirmation is true or false, you will not postback. However, if you only return to false, you should be fine.

+3
source

__doPostBack allows .net to add additional features, such as client-side validation checks. If it needs to be done this way, it will be done.

As for your implementation, you can use the OnClientClick property of the button to install additional scripts.

0
source

Source: https://habr.com/ru/post/1307784/


All Articles