Click here to Skip to main content
15,885,032 members
Articles / Web Development / ASP.NET

__doPostBack function

Rate me:
Please Sign up or sign in to vote.
4.57/5 (19 votes)
12 Oct 2013CPOL2 min read 262.7K   19   15
Hi everyone. Today I am going to talk about  __doPostBack function, because there is some confusion in using _dopostback.You can see this

This articles was originally at wiki.asp.net but has now been given a new home on CodeProject. Editing rights for this article has been set at Bronze or above, so please go in and edit and update this article to keep it fresh and relevant.

Hi everyone.

Today I am going to talk about the __doPostBack function, because there is some confusion with using this function.
You can see this __doPostBack function in your ASP.NET generated HTML code.

The function takes the following two arguments: 

eventTarget  - This contains the ID of the control that caused the post back.
eventArgument
- This contains any additional data associated with the control.

In any ASP.NET page the two hidden fields: __EVENTTARGET and __EVENTARGUMENT are automatically declared. 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 posted back and what is the event that has to be handled.

The value of the parameters 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 with the two parameters passed to the function. After this, the page is submitted back to the server. 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.

ASP.NET
<a id="LinkButton1" href="javascript:__doPostBack( 'LButton3','' )">LinkButton</a>

You can see the function call __doPostBack('LButton3','') in the href and the argument passed for eventTarget is "LButton3" which is the id of the link button control (EventSource)

Example

  1. Add two hidden fields inside the form.
    ASP.NET
    <input type =hidden name ="__EVENTTARGET" value ="">
    <input type =hidden name ="__EVENTARGUMENT" value =""> 
  2. Add javascript under the Head tag.
    ASP.NET
    <script>
    function __doPostBack( eventTarget, eventArgument )
    {
        document.Form1.__EVENTTARGET.value = eventTarget;
        document.Form1.__EVENTARGUMENT.value = eventArgument;
        document.Form1.submit();
    }
    </script>  
  3. Add two controls.
    ASP.NET
    <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.
    C#
    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 add.
    C#
    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:
    Java
    __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

This article was originally posted at http://wiki.asp.net/page.aspx/1082/dopostback-function

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
The ASP.NET Wiki was started by Scott Hanselman in February of 2008. The idea is that folks spend a lot of time trolling the blogs, googlinglive-searching for answers to common "How To" questions. There's piles of fantastic community-created and MSFT-created content out there, but if it's not found by a search engine and the right combination of keywords, it's often lost.

The ASP.NET Wiki articles moved to CodeProject in October 2013 and will live on, loved, protected and updated by the community.
This is a Collaborative Group

754 members

Comments and Discussions

 
Question__doPostback Pin
Member 1486605118-Aug-21 5:02
Member 1486605118-Aug-21 5:02 
QuestionWhat is the way to call it from C# ? Pin
Gluups7-Sep-18 0:42
Gluups7-Sep-18 0:42 
AnswerRe: What is the way to call it from C# ? Pin
Gluups7-Sep-18 1:55
Gluups7-Sep-18 1:55 
QuestionHow Event name is determined Pin
Ehsan Sajjad15-May-16 23:45
professionalEhsan Sajjad15-May-16 23:45 
Question__EVENTTARGET is name, not ID!! Pin
gqqnbig1-Mar-16 12:56
gqqnbig1-Mar-16 12:56 
QuestionGood! Pin
jamuro7716-Aug-15 9:58
jamuro7716-Aug-15 9:58 
SuggestionVery good Pin
freedeveloper24-May-15 12:55
professionalfreedeveloper24-May-15 12:55 
GeneralRe: Very good Pin
Ehsan Sajjad15-May-16 23:46
professionalEhsan Sajjad15-May-16 23:46 
GeneralRe: Very good Pin
freedeveloper11-Jun-16 12:20
professionalfreedeveloper11-Jun-16 12:20 
GeneralRe: Very good Pin
Ehsan Sajjad13-Jun-16 4:14
professionalEhsan Sajjad13-Jun-16 4:14 
GeneralХорошая статья Pin
TataSAF13-Jan-15 19:52
TataSAF13-Jan-15 19:52 
GeneralHelpful Article Pin
Shafiqur Rahman, Banladesh25-Jun-14 22:37
Shafiqur Rahman, Banladesh25-Jun-14 22:37 
GeneralGood Artical Pin
ShriMhaske29-May-14 1:17
ShriMhaske29-May-14 1:17 
GeneralMy vote of 1 Pin
KSGRao9-Apr-14 2:08
KSGRao9-Apr-14 2:08 
GeneralGood Article Pin
Alireza_13629-Dec-13 19:56
Alireza_13629-Dec-13 19:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.