Trigger custom or manual postback in ASP.NET
Introduction to postback using JavaScript
One of the most important features of ASP.net is the ability of manually triggering the postback event onto the server. Post back is a process by which the request is posted onto the server based on the event generated by user action. For Ex: when the user selects a dropdown, a post back event is triggered.
How postback works in ASP.NET?
For all the server control except Button and ImageButton, postback is generated by the JavaScript function __doPostBack(). Button and imagebutton uses the client browser functionality to submit the content onto the server. For all other controls, Asp.net runtime automatically insert the definition of __doPostBack() function in the HTML output to initiate a postback onto the page.
As an example, drag a simple linkbutton control and view the aspx page on the browser. IF you view the HTML output of this aspx page, you will see the following code.
<form name="form1" method="post" action="Buttons.aspx" id="form2">
<div>
<input type="hidden" name="__EVENTTARGET" id="__EVENTTARGET" value="" />
<input type="hidden" name="__EVENTARGUMENT" id="__EVENTARGUMENT" value="" />
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUKMTQ1OTQ0MTYyOWRkbEhOO740M99GNXoAENzhs8rbwNQ=" />
</div>
<script type="text/javascript">
//<![CDATA[
var theForm = document.forms['form1'];
if (!theForm) {
theForm = document.form1;
}
function __doPostBack(eventTarget, eventArgument) {
if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
theForm.__EVENTTARGET.value = eventTarget;
theForm.__EVENTARGUMENT.value = eventArgument;
theForm.submit();
}
}
//]]>
</script>
</div>
<a id="LinkButton1" href="javascript:__doPostBack('LinkButton1','')">LinkButton</a>
As you see above, two hidden elements __EVENTTARGET and __EVENTARGUMENT are created by ASP.net runtime which are then used by __doPostBack function to trigger postback onto the page.
__EVENTTARGET store the ID of the control that raises the postback while
__EVENTARGUMENT can be used to send some additional input/ information to the server.
On submitting the form, asp.net engine uses these 2 hidden fields to decide what was submitted by the form and then executes the appropriate action.
Third hidden control VIEWSTATE stores in encoded form, the ID of the control plus the method to be called on various user events/actions.
Raising your own postback event in ASP.NET?
Many times there is a need to raise the postback on some user event. Raising our own custom postback is very simple.
Just write a JavaScript code to call __doPostBack() as shown below
<script type="text/javascript">
function Call() {
__doPostBack('ControlId', 'ControlInfo');
}
</script>
Error encountered while raising custom postback
Above code will not work if your aspx page does not have any control that raises a postback to server. In such cases, you will get the error as shown below
Microsoft JScript runtime error: Object expected
This means you must have at least one control in an aspx page like linkbutton that raises a postback event. Declaring postback control and setting it visibility to false will also not work since the javascript function to perform the necessary action is not included.
Additionally, you may need to add the following code in page load of code behind.
ClientScript.GetPostBackEventReference(this, string.Empty);
This function enables the asp.net engine to initiate custom manual postback event.
Now, if you run the code, you may still get an error like
Invalid postback or callback argument. Event validation is enabled using <pages enableEventValidation="true"/> in configuration or <%@ Page EnableEventValidation="true" %> in a page
Asp.net engine has the inbuilt feature of verifying if the postback event is generated from the server control or not. If the postback event is not triggered from server control, it treats it as the hacking attempt or inconsistent behavior and for security purposes, raises the event validation error. IF the postback is valid and successful, asp.net uses the ClientScriptManager.RegisterForEventValidation method to register the postback or callback data for validation.
To bypass this error, you need to explicitly inform the asp.net engine that the event is generating from the trusted source. Adding the below code should do the trick
protected override void Render(System.Web.UI.HtmlTextWriter writer)
{
ClientScript.RegisterForEventValidation("lbDoPostBack", "JavaScript");
base.Render(writer);
}
Alternatively, change the page property EnableEventValidation to false as shown below
<% @ Page Language="C#" AutoEventWireup="true" CodeFile="PostBack.aspx.cs" EnableEventValidation="false" Inherits="PostBack" %>
Postback in Button and ImageButton control0
There are the only two controls that uses browsers submit behavior to cause postback instead of calling javascript __doPostBack function to post the form. However, you can override this default behavior by setting UseSubmitBehavior property of button control to false (Note this is not possible for ImageButton control). UseSubmitBehavior property tells the asp.net engine to use asp.net postback mechanism or client browser's submit behavior.0
To test it out, take two button controls and set the UseSubmitBehavior of one to true and other to false.
<asp:Button ID="Button1" runat="server" Text="Uses browser submit behavior" UseSubmitBehavior="true" />
<asp:Button ID="Button2" runat="server" Text="Uses asp.net postback mechanism" UseSubmitBehavior="false" />
On running this chunk of code, your HTML output would be
<input type="submit" name="Button1" value="Uses browser submit behavior" id="Button1" />
<input type="button" name