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,

10 Comments

Comments have been disabled for this content.