Ensure that __doPostBack() function is implemented on the page

 

In this topic I will explain why sometimes this message appears “Microsoft JScript runtime error: Object expected” when using the __doPostBack Javascript function.

 

 

__doPostBack Javascript function:

This function takes two parameters, the first one is the eventTarget which contains the ID of the control that causes the postback, and the second one is  eventArgument which contains any additional data associated with the control:

<input type="hidden" name="__EVENTTARGET" id="__EVENTTARGET" value="" />
<input type="hidden" name="__EVENTARGUMENT" id="__EVENTARGUMENT" value="" />

<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>

So you can call it to make manual  PostBack by using the following example:

__doPostBack('MyTragetID', 'OtherInformation');

 

 

 

__doPostBack and ASP.NET Controls :

In fact only two ASP.NET web server controls causes  postback (Button,Imagebutton). The other controls call __doPostBack Javascript function. take a look how the server controls render as HTML, you'll figure out that the buttons do not call the __doPostBack Javascript function:

For example add DropDownList and change AutoPostBacl property to true. Now, run the page and view the source of the page, you will see the following output:

  <select name="DropDownList1" onchange="javascript:setTimeout('__doPostBack(\'DropDownList1\',\'\')', 0)" id="DropDownList1">

Here the DropDownList will call __doPostBack function when you change the DropDownList selection. Also you will find that with CheckBoxes, DropDownLists, LinkButtons, etc…

But when you add ASP.NET Button you will find the following HTML output:

<input type="submit" name="Button1" value="Button" id="Button1" />

 

Note that the __doPostBack function and  the two hidden fields, “__EVENTTARGET” and “__EVENTARGUMENT,” are automatically declared by adding ScriptManger or server controls that make postback except Buttons and Imagebuttons, Therefore if you have a form with Textboxes and buttons where you want to use  __doPostBack function  then “Microsoft JScript runtime error: Object expected” exception will occur.

 

 

How to solve the problem :

You just need to call ClientScriptManager.GetPostBackEventReference Method in the page load.

 

Practical Example:Which Button caused a PostBack

You can find this example that show which button that caused a PostBack by calling __doPostBack function:

 

Design:

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>

    <script language="javascript" type="text/javascript">
        function DoPostBack(obj) {
            __doPostBack(obj.id, 'OtherInformation');
        } 
    </script>

</head>
<body>
    <form id="form1" runat="server">
    <asp:Label ID="lblMsg" runat="server" Text="Label"></asp:Label><br />
    <asp:Button ID="bntSubmit1" runat="server" Text="Submit1" OnClientClick="DoPostBack(this);" />   
    <asp:Button ID="bntSubmit2" runat="server" Text="Submit2" OnClientClick="DoPostBack(this);" />   
    </form>
</body>
</html>

C# Code:

 protected void Page_Load(object sender, EventArgs e)
        {
            ClientScript.GetPostBackEventReference(this, string.Empty);

            string targetCtrl = Page.Request.Params.Get("__EVENTTARGET");
            if (targetCtrl != null && targetCtrl != string.Empty)
            {
                lblMsg.Text = targetCtrl + " button make Postback";
            }
        }

 

 

 

But in general if you want to determine which control cased the PostBack you can use this common method :

        private string GetPostBackControl()
        {
            string Outupt = "";

            //get the __EVENTTARGET of the Control that cased a PostBack(except Buttons and ImageButtons)
            string targetCtrl = Page.Request.Params.Get("__EVENTTARGET");
            if (targetCtrl != null && targetCtrl != string.Empty)
            {
                Outupt = targetCtrl + " button make Postback";
            }
            else
            {
                //if button is cased a postback
                foreach (string str in Request.Form)
                {
                    Control Ctrl = Page.FindControl(str);
                    if (Ctrl is Button)
                    {
                        Outupt = Ctrl.ID;
                        break;
                    }
                }
            }
            return Outupt;
        }

 

if your form contain ImageButton then you need extra code for that.

 

Hope this helps,

Published Sunday, June 27, 2010 12:15 AM by Yousef_Jadallah
Filed under: ,

Comments

# re: Insure that __doPostBack() function is implemented on the page

Tuesday, September 28, 2010 12:30 PM by Adi

I can keep my hair now!  Thanks!

# re: Ensure that __doPostBack() function is implemented on the page

Thursday, February 2, 2012 7:31 AM by Alberto Garcia

Thanks. It was the solution of my issues..

# re: Ensure that __doPostBack() function is implemented on the page

Friday, June 8, 2012 10:20 AM by Arjmand

Thanx Man... Simply Brilliant and Brilliantly simple article..

# re: Ensure that __doPostBack() function is implemented on the page

Tuesday, June 19, 2012 2:25 AM by Ravi

thanks for your which saved my time.

# re: Ensure that __doPostBack() function is implemented on the page

Sunday, July 29, 2012 5:27 PM by salman

Thanks alot u saved my time .....

Once again thanks

# re: Ensure that __doPostBack() function is implemented on the page

Tuesday, August 6, 2013 7:30 PM by Ivan N

finally I found the correct solution, thank you very much!

# re: Ensure that __doPostBack() function is implemented on the page

Saturday, September 7, 2013 12:01 AM by Alex

Thanks man

# re: Ensure that __doPostBack() function is implemented on the page

Thursday, September 12, 2013 7:13 AM by Imran

Thanks

# re: Ensure that __doPostBack() function is implemented on the page

Monday, December 9, 2013 5:51 AM by SeyoS

Thanks !

# re: Ensure that __doPostBack() function is implemented on the page

Thursday, March 27, 2014 2:03 AM by Yashree

can anyone help me regarding this same issue but using the Linkbutton control.

Leave a Comment

(required) 
(required) 
(optional)
(required)