HotTester
HotTester

Reputation: 5768

Encountering Error when doing post-back using __doPostBack()

I am getting an error on javascript when doing post back. The code is as follows:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="test.aspx.cs" Inherits="test" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Untitled Page</title>

    <script language="javascript" type="text/javascript">

function DoPostBack() 
{
    __doPostBack('Button2','My Argument');
}

    </script>

</head>
<body>
    <form id="form1" runat="server">
    <input type="button" id="Button2" value="Press me" onclick="DoPostBack()" />
    </form>
</body>
</html>

I am getting the following error:

Line: 13
Error: Object expected

I can't understand why this error is coming. Kindly help...

Upvotes: 2

Views: 1759

Answers (3)

mas_oz2k1
mas_oz2k1

Reputation: 2901

__doPostBack is not created by default. If the page does not have a control that causes a postback then ASP.NET does not create/generate this method. In your case you can force ASP.NET to generate __doPostBack by adding the following line in you Page_Load event:

ClientScript.GetPostBackEventReference(this, string.Empty);

This line will force the creation of this method.

Upvotes: 1

Mahmoud Farahat
Mahmoud Farahat

Reputation: 5475

you can use a hidden button to do this task

Button1.Attributes.CssAttributes.Add("Display","None");

after hiding the button

you can call its click function from javascript

document.getElementById('<%=Button1.ClientID%>').click();

this will call Button1_Click on server

** remember to set UseSubmitBehaviour=false to make this work on non-IE browsers

hope that helps :)

Upvotes: 2

Danil
Danil

Reputation: 1893

_doPostBack isn't created by default. It appears when you are adding control with autoPostBack=true or adding some grid with buttons in it. So there is no _doPostBack javascript generated in your code. If you add

<asp:DropDownList ID="list" runat="server" AutoPostBack="true">
        <asp:ListItem Text="first"></asp:ListItem>
        <asp:ListItem Text="second"></asp:ListItem>
    </asp:DropDownList>

for instance your code will work. Don't know if it is really useful code :) however.

Upvotes: -1

Related Questions