• 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));
                }
            }        
        }
  • 相关阅读:
    最小二乘法拟合圆
    OpenCV solve() 解线性方程
    Halcon、OpenCV、C++ 实现最小二乘法拟合直线
    Halcon 字符串与HTuple互转,double与HTuple互转,Mat与HObject互转
    Halcon的HWindowControl控件在WinForm程序中的使用介绍(重点解决图片缩放的问题)
    将Halcon HObject类型转为Qt QImage类型
    Halcon HImage 与 Qt QImage 的相互转换
    Qt 加载DLL的几种方式小结
    Halcon17无法加载"hdevenginecpp":找不到指定的模块
    Halcon 安装提示 could not write updated path to HKLM
  • 原文地址:https://www.cnblogs.com/publicbill/p/517356.html
Copyright © 2020-2023  润新知