How to Use __dopostback() in JavaScript

Anika Tabassum Era Feb 02, 2024
How to Use __dopostback() in JavaScript

In general cases, the __dopostback() function is associated with ASP.NET. But in the following example, we will not be including any corresponding .NET code snippet; rather, we will explain the working principle.

The __dopostback() function of JavaScript takes two parameters, EventTarget and EventArgument. These arguments allow identifying the event that will cause the post method to get fired.

Later, with the second argument, we will explain any necessary message or action referred to after a valid post.

Let’s check up on the code fences below to get a rough idea of how the function works. To better understand an example with aspx files, you may count on this explicit thread.

Use the __dopostback() Function in JavaScript

Here, the instance will have some input fields hidden with the __EVENTTARGET and __EVENTARGUMENT id to grab the value that could have been set. Regardless of these inputs, the main function gets triggered when the action button is pressed.

The later button bares the function __dopostback( target, argument) where the target is the action event, and the argument is set to an aspx file. Whatever the conditions or roles are to be played is decided based on the argument.

Usually, when the form action is not set, it refers to posting the form data and reloads a new instance of that same page. And if the action is set, the post will run for the current page and then route to the destined web page.

What if we had a drop-down box for input? In that case, the very first instance of the web page would consider dealing with the server in the GET method.

And for each next reload, the options list gets added again, and the stack goes like this. So, if we set a condition for the one (initial) GET request, the list will not expand later for the POST methods.

You can clearly understand this explanation.

However, the __dopostback() function was to ensure an apt server post regarding the web contents, which could have been altered later.

Code Snippet:

<body>
    <form name="form1" method="post" action="" id="form1">
<div>
<input type="hidden" name="__EVENTTARGET" id="__EVENTTARGET" value="" />
<input type="hidden" name="__EVENTARGUMENT" id="__EVENTARGUMENT" value="" />
</div>
<script type="text/javascript">
var theForm = document.forms['form1'];
if (!theForm) {
    theForm = document.form1;
}
function __doPostBack(eventTarget, eventArgument) {
    if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
        theForm.__EVENTTARGET.value = eventTarget;
        theForm.__EVENTARGUMENT.value = eventArgument;
        theForm.submit();
    }
}
</script>
    </div>
    <button id="LinkButton1" onsubmit="javascript:__doPostBack('LinkButton1','')">Submit</a>
    </form>
</body>
</html>

Output:

javascript dopostback

Anika Tabassum Era avatar Anika Tabassum Era avatar

Era is an observer who loves cracking the ambiguos barriers. An AI enthusiast to help others with the drive and develop a stronger community.

LinkedIn Facebook

Related Article - JavaScript Function