What is the meaning of __doPostBack function, and when is it used?
simply said, it is used mainly by controls with AutoPostBack property
http://www.dotnetspider.com/resources/189-AutoPostBack-What-How-works.aspx
if you want to implement autopostback for your custom control, then you need to implement IPostBackDataHandler
How to use __doPostback in C#
You could call the .click()
event on your button that would cause the postback you are searching for:
Page.ClientScript.RegisterStartupScript(this.GetType(), "postback", "<script>$(document).ready( function() {$('#" + PrintExcemptionsButton.ClientID + "').click()});</script>", false);
However I don't see the sense of this. You could simply call the OnButtonClick()
function in your Page_Load
function from codebehind.
ASP.NET how to use __doPostBack from Custom JavaScript with Master/Content Pages
are you hard coding the call to __doPostBack?
try using Page.GetPostBackEventReference
to obtain a valid handle in your javascript.
once you write out a valid post back event reference, you could wrap it inside a local function, which you can than call from master page.
if (window.myPostBackfunction) // check if it exists
myPostBackFunction();
Is it OK to use __doPostBack()?
I would advice against it, since it's internal stuff of ASP.NET
and was never meant to be used directly.
Instead, what I'm doing when I need to "manually" trigger PostBack is adding hidden "server side" button with the proper OnClick:
<asp:Button id="btnDummy" runat="server" OnClick="Foo" style="display: none;" />
Then the JS is:
document.getElementById("<%=btnDummy.ClientID%>").click();
This way I don't care how post back happens, I just trigger the natural flow of events.
ASP.NET postback with JavaScript
Here is a complete solution
Entire form tag of the asp.net page
<form id="form1" runat="server">
<asp:LinkButton ID="LinkButton1" runat="server" /> <%-- included to force __doPostBack javascript function to be rendered --%>
<input type="button" id="Button45" name="Button45" onclick="javascript:__doPostBack('ButtonA','')" value="clicking this will run ButtonA.Click Event Handler" /><br /><br />
<input type="button" id="Button46" name="Button46" onclick="javascript:__doPostBack('ButtonB','')" value="clicking this will run ButtonB.Click Event Handler" /><br /><br />
<asp:Button runat="server" ID="ButtonA" ClientIDMode="Static" Text="ButtonA" /><br /><br />
<asp:Button runat="server" ID="ButtonB" ClientIDMode="Static" Text="ButtonB" />
</form>
Entire Contents of the Page's Code-Behind Class
Private Sub ButtonA_Click(sender As Object, e As System.EventArgs) Handles ButtonA.Click
Response.Write("You ran the ButtonA click event")
End Sub
Private Sub ButtonB_Click(sender As Object, e As System.EventArgs) Handles ButtonB.Click
Response.Write("You ran the ButtonB click event")
End Sub
- The LinkButton is included to ensure that the __doPostBack javascript function is rendered to the client. Simply having Button controls will not cause this __doPostBack function to be rendered. This function will be rendered by virtue of having a variety of controls on most ASP.NET pages, so an empty link button is typically not needed
What's going on?
Two input controls are rendered to the client:
<input type="hidden" name="__EVENTTARGET" id="__EVENTTARGET" value="" />
<input type="hidden" name="__EVENTARGUMENT" id="__EVENTARGUMENT" value="" />
__EVENTTARGET
receives argument 1 of __doPostBack__EVENTARGUMENT
receives argument 2 of __doPostBack
The __doPostBack function is rendered out like this:
function __doPostBack(eventTarget, eventArgument) {
if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
theForm.__EVENTTARGET.value = eventTarget;
theForm.__EVENTARGUMENT.value = eventArgument;
theForm.submit();
}
}
- As you can see, it assigns the values to the hidden inputs.
When the form submits / postback occurs:
- If you provided the UniqueID of the Server-Control Button whose button-click-handler you want to run (
javascript:__doPostBack('ButtonB','')
, then the button click handler for that button will be run.
What if I don't want to run a click handler, but want to do something else instead?
You can pass whatever you want as arguments to __doPostBack
You can then analyze the hidden input values and run specific code accordingly:
If Request.Form("__EVENTTARGET") = "DoSomethingElse" Then
Response.Write("Do Something else")
End If
Other Notes
- What if I don't know the ID of the control whose click handler I want to run?
- If it is not acceptable to set
ClientIDMode="Static"
, then you can do something like this:__doPostBack('<%= myclientid.UniqueID %>', '')
. - Or:
__doPostBack('<%= MYBUTTON.UniqueID %>','')
- This will inject the unique id of the control into the javascript, should you wish it
- If it is not acceptable to set
Related Topics
Convert Webpage to Image from Asp.Net
How to Deserialize Xml Document
The Calling Thread Cannot Access This Object Because a Different Thread Owns It
Pass List of Checkboxes into View and Pull Out Ienumerable
Interaction Between Forms - How to Change a Control of a Form from Another Form
Transitioning from Windows Forms to Wpf
Filtering on Include in Ef Core
Ienumerable VS List - What to Use - How Do They Work
How to Ignore a Property in Class If Null, Using Json.Net
Extension Method and Dynamic Object
Encrypt and Decrypt a String in C#
How to Auto-Generate a C# Class File from a Json String
How to Get the Path of the Assembly the Code Is In
Convert Generic List/Enumerable to Datatable
Create a Strongly Typed C# Object from Json Object With Id as the Name