__doPostBack and Object expected error


The best way to understand the working of the __doPostBack function is to dissect the function into small pieces and explore each piece one at a time.  Let us take a look at the function.

<input type=”hidden” name=”__EVENTTARGET” id=”__EVENTTARGET” value=”” />
<input type=”hidden” name=”__EVENTARGUMENT” id=”__EVENTARGUMENT” value=”” />
function __doPostBack(eventTarget, eventArgument) {
if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
theForm.__EVENTTARGET.value = eventTarget;
theForm.__EVENTARGUMENT.value = eventArgument;
theForm.submit();
}
}

The __doPostBack function takes two arguments, eventTarget and eventArgument.  The eventTarget contains the ID of the control that causes the postback and the eventArgument contains any additional data associated with the control.  Note that the two hidden fields, “__EVENTTARGET” and “__EVENTARGUMENT,” are automatically declared.  The value of the eventTarget and eventArgument are stored in the hidden fields.  The two hidden variables can be accessed from the code behind using the forms/params collection.

Let me clarify a common problem that we miss out here.

__doPostBack() is a script that gets written into your html, by ASP .Net, during any postback event and not any load event. Hence it would be meaningful to use this method in postback events.

Now let me go through some of the possible causes for this error:

  • Case is not right: Check the spelling of __doPostBack() – there are two underscores followed by ‘doPostBack’
  • __EVENTTARGET is not Id, instead its the name of the object. For ex. if a button id is ‘ctl00_cpMain_btnSubmit’, its name will be ‘ctl00$cpMain$btnSubmit’. This happens normally when you are on a child page or usercontrol inheriting from a master page. Best option would be to view the source of the page and get the button Name (I would recommend you to use IE developer toolbar to view source).
  • __EVENTTARGET argument not right: The object itself is not present on the page.
  • You must be calling this method, on a html button. In this case, there would be no __doPostBack() method in the page itself.

By the way its browser independent. These are some quick notes that i had faced, in my projects. Hope it was useful. Feel free to add on more.

2 thoughts on “__doPostBack and Object expected error

  1. @DotNetDevster: A good point you have bought out here.

    In Akamai you need to uncheck the Enable Edge Server Identification checkbox in the Edge Services – General section to ensure that the “__doPostBack”,”__EVENTTARGET” and “__EVENTARGUMENT” are not being deleted from the page source.

Feel free to leave a reply here...

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s