Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use __doPostBack function in asp.net

i am trying to use __doPostBack function so i can force my page to do post pack on the page load but i am having some difficulties understanding.

when i was looking at the examples online. On button click, i want to do the post back but not sure how to complete the code in the code behind.

Here is what i have so far:

<script type="text/javascript">
        function DoPostBack() {
            __doPostBack('btnNew', 'MyArgument');
        }
    </script>

Here is my button

 <asp:Button ID="btnNew" runat="server" CausesValidation="False" CommandName="New" OnClick="DoPostBack()" Text="Create" />

I don't seem to understand to use "MyArgument" in the code behind. What do i need to do in the code behind so it does post back on the page load? thanks in advance for the assistance.

like image 800
moe Avatar asked May 03 '13 15:05

moe


People also ask

What is __ Dopostback?

Understanding the JavaScript __doPostBack Function. This method is used to submit (post back) a form to the server and allows ASP.NET framework to call appropriate event handlers attached to the control that raised the post back.

What is __ Eventargument?

The __EVENTARGUMENT is any relevant event arguments regarding the control performing the postback. For most controls, there are no specialized event arguments, and since event arguments are different for every control, null is passed to represent a default argument should be created during the event sequence.

What postback means?

A postback is the exchange of information between servers to report a user's action on a website, network, or app.


2 Answers

Scenario 1

If you want to use your JavaScript function to trigger the postback, you need to replace the OnClick with OnClientClick. Modify the button definition like this (I'm already assuming that it's nested inside an UpdatePanel):

<asp:Button ID="btnNew" 
    runat="server" 
    CausesValidation="False" 
    CommandName="New" 
    OnClientClick="DoPostBack();" 
    Text="Create" />

In the code behind, in the Page_Load method, read the Request["__EVENTTARGET"] and the Request["__EVENTARGUMENT"]:

protected void Page_Load(object sender, EventArgs e)
{
    if (Page.IsPostBack)
    {
        if (Request["__EVENTTARGET"] == "btnNew" && 
            Request["__EVENTARGUMENT"] == "MyArgument")
        {
            //do something
        }
    }
}

Scenario 2

If you don't necessarily want to use JavaScript, modify the button's definition like this:

<asp:Button ID="btnNew"
    runat="server"
    CausesValidation="False"
    CommandName="New"
    OnClick="DoPostback"
    CommandArgument="MyArgument"
    Text="Create" />

Then, add the following method in the code behind:

protected void DoPostback(object sender, EventArgs e)
{
    var target = ((Button)(sender)).ClientID; //"btnNew"
    var argument = ((Button)(sender)).CommandArgument; //"MyArgument"
    if (target == "btnNew" &&
        argument == "MyArgument")
    {
        //do something
    }
}
like image 108
Alex Filipovici Avatar answered Sep 29 '22 05:09

Alex Filipovici


The values that are passed to the function __doPostBack as arguments will be sent to the server as request parameters named __EVENTTARGET and __EVENTARGUMENT. ASP.NET uses these values to determine what control has fired an event and what arguments should be passed as EventArgs object. But you can access them directly using object HttpRequest:

string eventTarget = this.Request.Params.Get("__EVENTTARGET");
string eventArgument = this.Request.Params.Get("__EVENTARGUMENT");
like image 43
Andrei Avatar answered Sep 29 '22 05:09

Andrei