方法一:
在JS中使用button按钮的click方法
<script type&#61;"text/Javascript">
function JSFun2()
{
document.getElementById("<%&#61; btnTest4.ClientID %>").click();
}
</script>
function JSFun2()
{
document.getElementById("<%&#61; btnTest4.ClientID %>").click();
}
</script>
方法二:
ClientScript.GetPostBackEventReference(btnTest2, "")
上面这行代码的返回值是一个string,"__doPostBack(&#39;btnTest2&#39;,&#39;&#39;)".
然后使用RegisterClientScriptBlock注册一下脚本.
string jscript &#61; "function JSFun1(){" &#43; ClientScript.GetPostBackEventReference(btnTest2, "") &#43; "};";
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "", jscript, true);
HTML
<%&#64; Page Language&#61;"C#" AutoEventWireup&#61;"true" CodeBehind&#61;"Default.aspx.cs" Inherits&#61;"GetPostBackEventReference_Test._Default" %>
DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns&#61;"http://www.w3.org/1999/xhtml" >
<head runat&#61;"server">
<title>Untitled Pagetitle>
<script type&#61;"text/Javascript">
function JSFun2()
{
document.getElementById("<%&#61; btnTest4.ClientID %>").click();
}
script>
head>
<body>
<form id&#61;"form1" runat&#61;"server">
<asp:Button ID&#61;"btnTest1" runat&#61;"server" Text&#61;"Test1" />
<asp:Button ID&#61;"btnTest2" runat&#61;"server" Text&#61;"Test2" OnClick&#61;"btnTest2_Click" />
<br />
<asp:Button ID&#61;"btnTest3" runat&#61;"server" Text&#61;"Test3" />
<asp:Button ID&#61;"btnTest4" runat&#61;"server" Text&#61;"Test4" OnClick&#61;"btnTest2_Click" />
form>
body>
html>
<%&#64; Page Language&#61;"C#" AutoEventWireup&#61;"true" CodeBehind&#61;"Default.aspx.cs" Inherits&#61;"GetPostBackEventReference_Test._Default" %>
DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns&#61;"http://www.w3.org/1999/xhtml" >
<head runat&#61;"server">
<title>Untitled Pagetitle>
<script type&#61;"text/Javascript">
function JSFun2()
{
document.getElementById("<%&#61; btnTest4.ClientID %>").click();
}
script>
head>
<body>
<form id&#61;"form1" runat&#61;"server">
<asp:Button ID&#61;"btnTest1" runat&#61;"server" Text&#61;"Test1" />
<asp:Button ID&#61;"btnTest2" runat&#61;"server" Text&#61;"Test2" OnClick&#61;"btnTest2_Click" />
<br />
<asp:Button ID&#61;"btnTest3" runat&#61;"server" Text&#61;"Test3" />
<asp:Button ID&#61;"btnTest4" runat&#61;"server" Text&#61;"Test4" OnClick&#61;"btnTest2_Click" />
form>
body>
html>
C#
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
namespace GetPostBackEventReference_Test
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
this.btnTest1.OnClientClick &#61; "Javascript:JSFun1();return false;";
this.btnTest3.OnClientClick &#61; "Javascript:JSFun2();return false;";
}
string jscript &#61; "function JSFun1(){" &#43; ClientScript.GetPostBackEventReference(btnTest2, "") &#43; "};";
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "", jscript, true);
}
protected void btnTest2_Click(object sender, EventArgs e)
{
Response.Write(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss fffffff"));
}
}
}
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
namespace GetPostBackEventReference_Test
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
this.btnTest1.OnClientClick &#61; "Javascript:JSFun1();return false;";
this.btnTest3.OnClientClick &#61; "Javascript:JSFun2();return false;";
}
string jscript &#61; "function JSFun1(){" &#43; ClientScript.GetPostBackEventReference(btnTest2, "") &#43; "};";
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "", jscript, true);
}
protected void btnTest2_Click(object sender, EventArgs e)
{
Response.Write(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss fffffff"));
}
}
}