I have several small div
s which are utilizing jQuery
draggable. These div
s are placed in an UpdatePanel
, and on dragstop I use the _doPostBack()
JavaScript function, where I extract necessary information from the page's form.
My problem is that when I call this function, the whole page is re-loaded, but I only want the update panel to be re-loaded.
How to Raise a Postback from JavaScript? To do this, we need to just call the __doPostBack() function from our javascript code. When the above function is called, it will raise a postback to server.
PostBack is the name given to the process of submitting an ASP.NET page to the server for processing. PostBack is done if certain credentials of the page are to be checked against some sources (such as verification of username and password using database).
IsPostBack is a property of the Asp.Net page that tells whether or not the page is on its initial load or if a user has perform a button on your web page that has caused the page to post back to itself. The value of the Page.
Why use the <Triggers> section? If you omit the Triggers section and just "click" the button, it will post back the entire page. If you specify triggers, ASP.NET will generate JavaScript to "catch" the click and do the update via AJAX rather than a whole page submission.
Here is a complete solution
<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>
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
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 __doPostBackThe __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(); } }
When the form submits / postback occurs:
javascript:__doPostBack('ButtonB','')
, then the button click handler for that button will be run.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
ClientIDMode="Static"
, then you can do something like this: __doPostBack('<%= myclientid.UniqueID %>', '')
. __doPostBack('<%= MYBUTTON.UniqueID %>','')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With