views:

34

answers:

2

Hi,

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"&gt;
<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<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...

A: 

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

Danil
A: 

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

mahmoud