AttachJSFunction(一个button同时挂两个onclick事件)

不只一次遇到过这样的问题:一个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 > &nbsp;&nbsp;&nbsp;
                
< 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));
            }
        }        
    }

转载于:https://www.cnblogs.com/publicbill/archive/2006/09/28/517356.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值