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.
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.
Share Improve this question edited May 3, 2013 at 15:36 Liam 29.7k28 gold badges137 silver badges200 bronze badges asked May 3, 2013 at 15:00 moemoe 5,24940 gold badges135 silver badges202 bronze badges 5- 1 just don't understand why you use __doPostBack in JavaScript if button does postback anyway? – Alexan Commented May 3, 2013 at 15:11
- What is the reason behind writing your own postback script for a button control (which is posting as anyway as Alex has stated)? – Win Commented May 3, 2013 at 15:15
- Do you want to do a PostBack for button OnClick? That is fired authomatically. You don't need to add extra code for it. – Kremena Lalova Commented May 3, 2013 at 15:15
- i am using ajax tab container and it is not doing the full post back for some reason, i am sure my issue will be resolved if i do some kind of post back. i would appreciate if u could help of what i am asking... thanks – moe Commented May 3, 2013 at 15:15
- i am using ajax tooltip in a DetailView but on page load the tooltip does not show any text but when i do some kind of post back in the detailview then the tooltip shows the text or the description – moe Commented May 3, 2013 at 15:19
4 Answers
Reset to default 3Scenario 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
}
}
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");
ASP.Net provides a way for you to build a call to perform a PostBack from javascript.
The ClientScriptManager.GetPostBackEventReference Method will build the code you need to use in javascript to perform a PostBack from a particular control http://msdn.microsoft.com/en-us/library/system.web.ui.clientscriptmanager.getpostbackeventreference.aspx
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.
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)
Hope that's what you're looking for.
UPDATE
Here's an example of how you might call the javascript function that was created for you on the server side.
<input type="button" value="Click Me" onclick="handleDoPostBack();" />
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.
You can also call the javascript function directly in code, as such...
<script>
handleDoPostBack();
</script>
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.
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.
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.
below code works for me todo a postback using C#. Note i am not using the 'MyArgument' parameter and leaving it empty
ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "DoPostBack", "__doPostBack('dummybtnPivotGridupdate', '')", true);
where
<asp:Button ID="dummybtnPivotGridupdate" runat="server" Style="display: none;" ClientIDMode="Static" />
Hope this helps!