Home / ASP.NET Wiki / Javascript / __doPostBack function

__doPostBack function

 Rate It (20)

Hi everyone. Today I am going to talk about  __doPostBack function, because there is some confusion in using _dopostback.

You can see this __doPostBack function in your asp.net generated HTML code.
The function takes two arguments:

1) eventTarget

2) eventArgument

1) EventTarget contains the ID of the control that causes the postback

eventArgument contains any additional data associated with the control.


When a page is posted back to the server ASP .NET inspects __EVENTTARGET and __EVENTARGUMENT values and this way it can decide which of the controls caused the page to be postedback and what is the event that has to be handled.
In any asp.net page the two hidden fields, “__EVENTTARGET” and “__EVENTARGUMENT,” are automatically declared. The value of the eventTarget and eventArgument are stored in the hidden fields. The two hidden variables can be accessed from the code behind using the forms or params collection.

If we inspect the code of the __doPostBack function, we can see that it first sets the values of two hidden fields created by ASP .NET named __EVENTTARGET and __EVENTARGUMENT with the two parameters passed to the function. After this, the page is submitted back to the server. When a page is posted back to the server ASP .NET inspects __EVENTTARGET and __EVENTARGUMENT values and this way it can decide which of the controls caused the page to be postedback and what is the event that has to be handled. The ID of the control which causes the postback is stored in the __EVENTTARGET hidden field, so you can find the control which caused the postback.


<a id="LinkButton1" href="javascript:__doPostBack('LButton3','')">LinkButton</a>
You can see the fucntion call "__doPostBack('LButton3','')" in href and the argument passed for eventTarget is "LButton3" which is the id of the linkbutton control (EventSource)

Example

I am going to talk aamirhasan.aspx Page.

in aamirhasan.aspx Page

1) Add two hidden fields inside the form as given below

<input type =hidden name ="__EVENTTARGET" value ="">
<input type =hidden name ="__EVENTARGUMENT" value ="">

2) Add javascript under the Head tag as given below

<script>
                       function __doPostBack(eventTarget, eventArgument) {
                           document.Form1.__EVENTTARGET.value = eventTarget;
                           document.Form1.__EVENTARGUMENT.value = eventArgument;
                           document.Form1.submit();
                       }
    </script>

 3) add two control given below

 <a id="LButton3" href="javascript:__doPostBack('Button2','')">LinkButton</a>

<asp:Button ID="Button2" runat="server" onclick="Button2_Click" Text="Button" />

 4) add function in your cs page

protected void Button2_Click(object sender, EventArgs e)
{
    Response.Write("Welcome to  Student Academic Blog");
}

5) You also need some code in the code behind to capture the postback and fire the event:

In the PageLoad method:

if (Request.Form["__EVENTTARGET"] == "Button2")
{
    //fire event
    Button2_Click(this, new EventArgs());
}

This will capture the posted variable "__EVENTTARGET" and cause it to fire the event "Button2_Click". You can also pass an event argument along with the target in case you need to pass something to your code behind:

javascript:__doPostBack("Button2', '<event argument here>')

This would be captured in the code behind as Request.Form["__EVENTARGUEMENT"]

 So this is how you can use __doPostBack

Enjoy it

Revision number 6, Thursday, December 08, 2011 8:56:57 PM by zac.hall

Comments

Good

Thank you very much. It was helpful and ultimately i understood y we cannot call code behind function with a runat="server">/a> tag.

Added information: _doPostBack() is not generated for Buttons and ImageButtons.

Hello, I tried this code, the Page is doing the PostBack but it is not entering the code behind function... can you help me?

on chrome???

You forgot the code on how to get the event to fire... on PageLoad should be: if(Request.Form["__EVENTTARGET"] == "Button2") { //fire event - may not be exactly correct ;) Button2_Click(this, new EventArgs()); }

Suppose I have a textbox with textchanged event on server side but auto post back for textbox is set to false. Now the label is clicked and with the help of __EVENTTARGET the click event of label is fired but the interesting thing to notice here is text changed event for text box would also be fired. The question is how ?

Is there any difference between _doPostBack & __doPostBack?

_doPostBack is not available in JS.

Related Articles

Capturing Key Strokes and Doing a Post back

Capturing Key Strokes and Doing a Post back With the invention of Web 2.0 customers are becoming more and more demanding when it comes to interactive Web UI.With application being moved from Desktops to web, end users demand that the web application provide

How to Postback Asynchronously from inside a GridView

Concept With the rise of ASP.NET 2.0 AJAX Extensions 1.0, developers were fascinated by the great AJAX Server controls, but they missed that some of them might be harmful if they didn't really know how to usethem ina proper way. One example is when there

Shortcuts

Table of Contents

Top Wiki Contributors

(last 30 days)

  1. abiruban (1)