Posting back an ASP.NET page from Javascript

A Postback is another name for HTTP POST. The contents of a form are sent to the server for processing some information. Afterward, the server sends a new page back to the browser.
In some scenarios you’d like to postback the page or part of the page if you’re using AJAX. All the ASP.NET server controls except the ImageButton and Button will call a JavaScript method called __doPostBack(eventTarget, eventArgument). This method is inserted to the page HTML output by the ASP.NET runtime engine. The method accepts 2 arguments

  • __EVENTTARGET: holds the ID of the control that’s raising the postback event
  • __EVENTARGUMENT: holds additional information that are required by the control on the server

//The __doPostBack method inserted by the ASP.Net runtime
function __doPostBack(eventTarget, eventArgument) {
if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
theForm.__EVENTTARGET.value = eventTarget;
theForm.__EVENTARGUMENT.value = eventArgument;

In order to do a postback from JavaScript code all you need is call the __doPostBack method, but this isn’t the best and safest way to perform the action. If the ASP.NET guys changed the name of the method your code will break. To prevent this you’ll need to use some server side code.
Create a method called DoPostBackJS().
function DoPostBackJS() {
<%= Page.ClientScript.GetPostBackEventReference(this, "posting back from js") %>

The line of code in the preceding method at runtime will be evaluated to
__doPostBack('__Page','posting back from js')


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: