Rectangle 27 1

c how to use __doPostBack function in asp.net?


<asp:Button ID="btnNew"
    runat="server"
    CausesValidation="False"
    CommandName="New"
    OnClick="DoPostback"
    CommandArgument="MyArgument"
    Text="Create" />
<asp:Button ID="btnNew" 
    runat="server" 
    CausesValidation="False" 
    CommandName="New" 
    OnClientClick="DoPostBack();" 
    Text="Create" />
Page_Load
Request["__EVENTARGUMENT"]
Request["__EVENTTARGET"]
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
    }
}
protected void Page_Load(object sender, EventArgs e)
{
    if (Page.IsPostBack)
    {
        if (Request["__EVENTTARGET"] == "btnNew" && 
            Request["__EVENTARGUMENT"] == "MyArgument")
        {
            //do something
        }
    }
}

If you don't necessarily want to use JavaScript

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):

Then, add the following method in the code behind:

Note
Rectangle 27 1

c how to use __doPostBack function in asp.net?


<input type="button" value="Click Me" onclick="handleDoPostBack();" />
<script>
handleDoPostBack();
</script>
Dim csm As ClientScriptManager = Page.ClientScript
Dim sb As New StringBuilder
sb.AppendLine("function handleDoPostBack() {")
sb.AppendLine(csm.GetPostBackEventReference(MyButton, ""))
sb.AppendLine("}")
csm.RegisterClientScriptBlock(Me.Page.GetType, "js_code__create_functions", sb.ToString, True)

ASP.Net provides a way for you to build a call to perform a PostBack from javascript.

Below is an example (in VB.Net). In the code-behind, I'm creating my own javascript function. The ASP.Net code will insert javascript code in my function to perform the PostBack for the "MyButton" control. When the page is built, this javascript function will be available. When javascript code makes a call to this javascript function, it will then perform a PostBack, which will mimic the action that occurs when the "MyButton" button is clicked.

From reading some of your comments in the other answers, the issue you're trying to resolve seems to have to do with the AJAX panels you're using. I'm not familar with those, so I'm not sure why it's not working for you, and I don't know if triggering the PostBack programatically is going to solve your issue.

Here's an example of how you might call the javascript function that was created for you on the server side.

Hope that's what you're looking for.

However, if you do need to programatically trigger a PostBack, this is the code you could use. I use it in cases where I don't want a button to be on the page, but I want an action to take place. When something else happens on my page, then I run some javascript code that will act as though a button were clicked - and I can handle that button click on the server side. In my ASP.Net page, I do include the button, but then I use CSS to hide the user from seeing the button. So the user doesn't see the button, but my javascript code can call my javacript function which will mimic a click of that button.

I just made a regular HTML button (instead of using an ASP.Net button), because I didn't want to get the concept of a button PostBack confused with it. This is simply an HTML button that does nothing other than what you program it to do. Since I've added the javascript function call to the onclick, then when you press the button, it will perform the PostBack. When this PostBack occurs, it will act as though the "MyButton" button was clicked; that would be an ASP.Net button that you have somewhere on the page. Since that button is an ASP.Net button, it would already be doing a PostBack on its own, which is why I think there's some confusion about why you're looking to trigger a PostBack programatically.

I just re-read your question to figure out how you would implement this code and caught the part about doing this on Page Load. So when the page loads, you want it to automatically do a PostBack? Is that correct? If so, you'd have to be careful with that, b/c I'd think that could get you into an infinite loop; if of loading the page, posting back, loading the page again, posting back again, etc. What is your goal in trying to force the PostBack? Your comment above mentions PostBack on button click, so that's why I was confused.

No problem - just added an update to my answer. Not sure if it will help you, but I try to give an example of how you would call the javascript function that I created in the server side code.

This will occur as soon as the code encounters it. Or, if you put it in an onload event, then it would be called when the page was finished loading. But like I mentioned in my comment, you'd have to be careful with that because you may end up with an inifinite loop.

You can also call the javascript function directly in code, as such...

i want to try your approach but not sure how to follow it and try it. here is my question, where is the javascript and where do i need to put this code on button click method or page load that is why i am confused as how to try it and see it if it works. i am new to this so please excuse my lack of skills in this area. thanks.

thanks Rick. i can't seem to follow the code, appreciate if u can elaborate with example. all what i am trying to do another postback when button is clicked.

Note
Rectangle 27 1

c how to use __doPostBack function in asp.net?


string eventTarget = this.Request.Params.Get("__EVENTTARGET");
string eventArgument = this.Request.Params.Get("__EVENTARGUMENT");

@moe, not necessarily. As far as I know request object is available through all page life cycle.

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:

do i need to put this code in the page load method?

Note
Rectangle 27 0

c how to use __doPostBack function in asp.net?


<asp:Button ID="dummybtnPivotGridupdate" runat="server" Style="display: none;" ClientIDMode="Static" />
ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "DoPostBack", "__doPostBack('dummybtnPivotGridupdate', '')", true);

below code works for me todo a postback using C#. Note i am not using the 'MyArgument' parameter and leaving it empty

Note