不只一次遇到过这样的问题:一个button要执行了两次不同的onclick事件,所谓不同就是一个button的onclick分别挂了两个不同的function,比如经常会有这样的情况:先弹出一个confirm对话框,然后再执行其它的操作。
也许你会说,那很简单,把两个function里的内容拼到一起不就ok了吗?
可是,如果一个function在server端,而另一个在client端,那么想把它们拼凑到一起是一件很困难的事情。
困难,即不是不能实现,下面是percyboy从微软的程序中摘出一段代码写成了一个function AttachFunction(ev, func),解决了上述的难题!
以下是所有测试该问题的代码
AttachJSFunction.aspx 代码:
<%@ Page language="c#" Codebehind="AttachJSFunction.aspx.cs" AutoEventWireup="false" Inherits="site1.AttachJSFunction" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>AttachJSFunction</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
<script language="JavaScript">
<!--
function init()
{
var mybtn = document.all("Button1");
//**************************************
//mybtn.onclick = Button1_click;
/// *--invoke ordinary.
/// *--it is only execute one function.
//**************************************
//****************************************************************
mybtn.onclick = AttachFunction(mybtn.onclick, "Button1_click()");
/// *--warning: [Button1_click] is wrong.
/// *-- ["Button1_click()"] is right.
//****************************************************************
//****************************************************************
//alert(mybtn.onclick);
/// *--alert output:
/// function anonymous() {
///
/// if(!confirm('Are you sure submit?')) return false;
/// Button1_click() //-- have no ';'
/// }
//****************************************************************
}
function Button1_click()
{
var mytextbox = document.all("TextBox1");
mytextbox.value = "you already submit on (" + (new Date()).toLocaleString() + " ).";
}
function AttachFunction(ev, func)
{
if (typeof(ev) == "function" ) {
ev = ev.toString();
ev = ev.substring(ev.indexOf("{") + 1, ev.lastIndexOf("}"));
}
else
{
ev = "";
}
//************************************************************
//return new Function(func + ev);
return new Function(ev + func);
// *-- warning: can be swap the location of the two functions.
//************************************************************
}
//-->
</script>
</HEAD>
<body onload="init()">
<form id="Form1" method="post" runat="server">
<FONT face="MS UI Gothic">
<asp:TextBox id="TextBox1" runat="server" Width="312px"></asp:TextBox>
<asp:Button id="Button1" runat="server" Text="Button"></asp:Button></FONT>
</form>
</body>
</HTML>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>AttachJSFunction</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
<script language="JavaScript">
<!--
function init()
{
var mybtn = document.all("Button1");
//**************************************
//mybtn.onclick = Button1_click;
/// *--invoke ordinary.
/// *--it is only execute one function.
//**************************************
//****************************************************************
mybtn.onclick = AttachFunction(mybtn.onclick, "Button1_click()");
/// *--warning: [Button1_click] is wrong.
/// *-- ["Button1_click()"] is right.
//****************************************************************
//****************************************************************
//alert(mybtn.onclick);
/// *--alert output:
/// function anonymous() {
///
/// if(!confirm('Are you sure submit?')) return false;
/// Button1_click() //-- have no ';'
/// }
//****************************************************************
}
function Button1_click()
{
var mytextbox = document.all("TextBox1");
mytextbox.value = "you already submit on (" + (new Date()).toLocaleString() + " ).";
}
function AttachFunction(ev, func)
{
if (typeof(ev) == "function" ) {
ev = ev.toString();
ev = ev.substring(ev.indexOf("{") + 1, ev.lastIndexOf("}"));
}
else
{
ev = "";
}
//************************************************************
//return new Function(func + ev);
return new Function(ev + func);
// *-- warning: can be swap the location of the two functions.
//************************************************************
}
//-->
</script>
</HEAD>
<body onload="init()">
<form id="Form1" method="post" runat="server">
<FONT face="MS UI Gothic">
<asp:TextBox id="TextBox1" runat="server" Width="312px"></asp:TextBox>
<asp:Button id="Button1" runat="server" Text="Button"></asp:Button></FONT>
</form>
</body>
</HTML>
AttachJSFunction.asp.cs 代码:
public class AttachJSFunction : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox TextBox1;
protected System.Web.UI.WebControls.Button Button1;
private static string jsFormat = "if(!confirm('{0}')) return false;";
private static string Message01 = "Are you sure submit?"; // *--usually from XML file.
private void Page_Load(object sender, System.EventArgs e)
{
if (!IsPostBack)
{
this.Button1.Attributes.Add("onclick", string.Format(jsFormat, Message01));
}
}
}
{
protected System.Web.UI.WebControls.TextBox TextBox1;
protected System.Web.UI.WebControls.Button Button1;
private static string jsFormat = "if(!confirm('{0}')) return false;";
private static string Message01 = "Are you sure submit?"; // *--usually from XML file.
private void Page_Load(object sender, System.EventArgs e)
{
if (!IsPostBack)
{
this.Button1.Attributes.Add("onclick", string.Format(jsFormat, Message01));
}
}
}