click 那个链接不能postback,但是click按钮是可以的。代码如下:
cal.aspx
-----------
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="cal.aspx.cs" Inherits="cal" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server" >
<input type="submit" value="click to submit" /> </form>
<a href="javascript:void(0)" onclick="validate()">click to submit</a>
<script language="javascript">
function validate()
{
var oform1 = window.document.getElementById( "form1" );
oform1.submit();
}
</script></body>
</html>cal.aspx.cs
---------------
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;public partial class cal : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
Response.Write("not back");
}
else
{
Response.Write("we are in back now!!!");
}
}
}
cal.aspx
-----------
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="cal.aspx.cs" Inherits="cal" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server" >
<input type="submit" value="click to submit" /> </form>
<a href="javascript:void(0)" onclick="validate()">click to submit</a>
<script language="javascript">
function validate()
{
var oform1 = window.document.getElementById( "form1" );
oform1.submit();
}
</script></body>
</html>cal.aspx.cs
---------------
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;public partial class cal : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
Response.Write("not back");
}
else
{
Response.Write("we are in back now!!!");
}
}
}
1.添加form标签中的属性 method="post"
2.将超链接改为<a href="#" onclick="validate()">click to submit</a>
如下:
<form id="form1" runat="server" method="post">
<input type="submit" value="click to submit" /> </form>
<a href="#" onclick="validate()">click to submit</a>
<script language="javascript">
function validate()
{
var oform1 = window.document.getElementById( "form1" );
oform1.submit();
}
</script>
还是写成这样好。问一下思归老大,为什么要加一个void在外面啊?有什么特别的讲究吗?
<script language="javascript">
function validate()
{
window.open("abc.html");
//var oform1 = window.document.getElementById( "form1" );
//oform1.submit();
return true;
}
</script>
通过click <a>来submit form,用
<a href="javascript:validate()"> 或者 <a href="javascript:void(validate())">通过click <a>来执行一共客户端操作,用
<a href="javascript:void(validate())">================================================
另外我觉得应该避免使用
<a href="#">,不太规范