How the PostBack Occurs?
All the webcontrols except Button and
ImageButton control will call a javascript function called __doPostBack() to
post the form to server. Button and ImageButton control will use the browsers
ability and submit the form to the server. ASP.Net runtime will automatically
insert the definition of __doPostBack() function in the HTML output when there
is a control that can initiate a postback in the page.
To know this, drag a LinkButton control
into the page and execute the page. You can see the __doPostBack() function will
get automatically added to the page output. Refer below,
<body>
<form name="form1" method="post"
action="Buttons.aspx" id="form1">
<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>
</form>
</body>
</html>
In the above code, you can see the
definition of __doPostBack() method(bolded) automatically inserted.
What does __doPostBack() method
do?
It is very clear from the above method
definition that the method accepts 2 arguments, eventTarget and
eventArgument. These arguments values are in turn populated to hidden fields,
__EVENTTARGET and __EVENTARGUMENT.
__EVENTTARGET
This field is populated with the ID of
the control that raises the postback in order to identify it by the
server.
__EVENTARGUMENT
This field can be used by the control
to send some additional information to the server. Later sections of this
article will discuss more on this field and its usages.
These 2 hidden fields are again
inserted by the ASP.Net runtime when it inserts __doPostBack method.
Moving to our subject now, if we want
to raise a postback manually without an event then it is as simple as calling
the __doPostback() method using javascript.
How to Raise a Postback from
JavaScript?
To do this, we need to just call the
__doPostBack() function from our javascript code.
Refer below,
<script
type="text/javascript">
function CallServer() {
__doPostBack('','');
}
</script>
When the above function is called, it will raise a postback to
server.
Note
Sometimes, when the above code is
executed we may get the below error,
Microsoft JScript runtime error: Object
expected
As i said earlier, ASP.Net runtime will
insert the definition of __doPostBack() automatically only when there is a
control that can initiate a postback in the page. In this case, your page might
not have a control that can raise the postback. ASP.Net is brilliant enough so
that it outputs the __doPostBack() function only if there is a control that can
raise postback.
We will move to the next section and
understand how we can take care of these scenarios.
Emitting __doPostBack() postback
method manually
The ClientScriptManager class released
in .NetFramework has method called GetPostBackEventReference()
which can emit the definition of __doPostBack method into our page. Include the
below code in your page load,
protected void Page_Load(object sender,
EventArgs e)
{
ClientScript.GetPostBackEventReference(this, "");
}
The above code will insert the
__doPostBack() method into your webpage.
Raising a Control Event
Sometimes, you may have a scenario
where you may need to raise a control’s event from a javascript based on some
business logic. For example, if you want to raise a DropDownList Selected Index
changed event or a LinkButton click event manually, then you can call
__doPostBack() method by passing its ID. Refer the below code.
<script
type="text/javascript">
function CallServer() {
__doPostBack('lbDoPostBack','');
}
</script>
You should pass the UniqueID of the control when you have associated master page in the __doPostBack() method above.
When you call __doPostBack() method
without any argument (as in section Raising Postback from Javascript)
then it will call only the Page_Load event.
|