问题
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...
回答1:
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 :)
回答2:
__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.
回答3:
_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.
来源:https://stackoverflow.com/questions/4049670/encountering-error-when-doing-post-back-using-dopostback