AttachJSFunction(一个button同时挂两个onclick事件)
2006-09-28 16:19
399 查看
不只一次遇到过这样的问题:一个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>
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));
}
}
}
也许你会说,那很简单,把两个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>
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));
}
}
}
相关文章推荐
- js中onclick事件同时调用两个方法
- 给服务器控件button添加一个在onclick之前的js事件
- 用JS实现一个表单多个按钮的方法,两个onclick事件处理
- 使用jquery当页面打开时,将一个事件绑定到控件(同时执行两个事件),并修改加载样式类中的样式
- js触发asp.net的Button的Onclick事件
- JavaScript-父子dom同时绑定两个点击事件,一个用捕获,一个用冒泡时执行顺序
- jquery的表单验证方法,一个function能不能同时捕捉点击事件和按键事件?能不能再优化下,有代码。
- JS中onclick事件的一个坑
- 在Android中ListView中添加两个点击事件,一个是OnItemClick,另一个是一个图片的OnClick事件
- js触发asp.net的Button的Onclick事件
- 讨论: 在WebControl中的Button,同时有onClientClick和onclick事件,验证控件实效
- 如何解决按回车键激发Textbox的TextChanged事件的同时,激发Button的Onclick事件
- asp.net js验证为空j时,不让button执行onclick事件
- android 同时点击多个button 避免同时响应,只响应一个点击事件 (是否允许多点触控)
- IOS 开发两个按钮(button)很近的时候同时触发事件解决的方法(setExclusiveTouch)
- js触发asp.net的Button的Onclick事件应用
- IE6中,一个Button同时打开两个下载窗口,并且可以自动关闭
- 关于html控件button的onclick和onserverclick事件不能同时起作用问题
- JavaScript实现父子dom同时绑定两个点击事件,一个用捕获,一个用冒泡时执行顺序的方法
- 点击TButton后的执行OnClick和OnMouseDown两个事件的过程(其实是通过WM_COMMAND执行程序员的代码)