What is the difference between client browser's submit mechanism and the ASP.NET postback mechanism?

风流意气都作罢 提交于 2019-12-30 10:35:30

问题


Button.UseSubmitBehavior property is used to gets or sets a value indicating whether the Button control uses the client browser's submit mechanism or the ASP.NET postback mechanism.

So, What is the difference between client browser's submit mechanism and the ASP.NET postback mechanism?


回答1:


If you set use submit behavior to false, ASP.NET will generate script to handle submit by calling "__doPostBack" method like the following code. The method will add value to event target for telling server which element fire current event.

<script type="text/javascript">
//<![CDATA[
var theForm = document.forms['ctl00'];
if (!theForm) {
    theForm = document.ctl00;
}
function __doPostBack(eventTarget, eventArgument) {
    if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
        theForm.__EVENTTARGET.value = eventTarget;
        theForm.__EVENTARGUMENT.value = eventArgument;
        theForm.submit();
    }
}
//]]>
</script>

<input type="button" name="Button1" value="Submit" onclick="javascript:__doPostBack('Button1','')" id="Button1">      

In the other hand, if you set use submit behavior to true, ASP.NET will generate button as input type submit instead of type button. When use click this button, the form will be normally submited.

<input type="submit" name="Button1" value="Submit" id="Button1">

Both ways are not difference at the server-side. But if you set use submit behavior to true, it will generate a bit cleaner XHTML.




回答2:


Based on the docs you referenced it seems that the default behavior submits a form using the standard submit button in a form, while setting it to false will instead do something like:

<input type=button onclick="submitForm()" />

The default form behavior is:

<form><input type=submit /></form>


来源:https://stackoverflow.com/questions/4635855/what-is-the-difference-between-client-browsers-submit-mechanism-and-the-asp-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!