自己动手编写一个ajax extender

该扩展的功能有以下几点:
1、当鼠标移到某个按钮上时,该按钮显示一个Css样式;
2、当鼠标移开该按钮时,该按钮显示另外一个Css样式;
3、当鼠标点击该按钮时,执行一个用户自己定义的javascript函数;


该extender共有以下三个文件:
HoverButtonExtender.cs
HoverButtonDesigner.cs
HoverButtonBehavior.js
其中HoverButtonDesigner.cs和HoverButtonBehavior.js的代码分别如下:

HoverButtonBehavior.js
  1None.gif// (c) Copyright Microsoft Corporation.
  2None.gif// This source is subject to the Microsoft Permissive License.
  3None.gif// See http://www.microsoft.com/resources/sharedsource/licensingbasics/sharedsourcelicenses.mspx.
  4None.gif// All other rights reserved.
  5None.gif
  6None.gif
  7None.gif// README
  8None.gif//
  9None.gif// There are two steps to adding a property:
 10None.gif//
 11None.gif// 1. Create a member variable to store your property
 12None.gif// 2. Add the get_ and set_ accessors for your property.
 13None.gif//
 14None.gif// Remember that both are case sensitive!
 15None.gif//
 16None.gif
 17None.gifType.registerNamespace('HoverButton');
 18None.gif
 19ExpandedBlockStart.gifContractedBlock.gifHoverButton.HoverButtonBehavior = function(element) dot.gif{
 20InBlock.gif
 21InBlock.gif    HoverButton.HoverButtonBehavior.initializeBase(this, [element]);
 22InBlock.gif
 23InBlock.gif    // TODO : (Step 1) Add your property variables here
 24InBlock.gif    //
 25InBlock.gif    this._HoverButtonCssClass = null;
 26InBlock.gif    this._UnHoverButtonCssClass = null;
 27InBlock.gif    this._OnButtonClickScript = null;
 28InBlock.gif    this._clickHandler = null;
 29InBlock.gif    this._hoverHandler = null;
 30InBlock.gif    this._unhoverHandler = null;    
 31InBlock.gif
 32ExpandedBlockEnd.gif}

 33None.gif
 34ExpandedBlockStart.gifContractedBlock.gifHoverButton.HoverButtonBehavior.prototype = dot.gif{
 35InBlock.gif
 36ExpandedSubBlockStart.gifContractedSubBlock.gif    initialize : function() dot.gif{
 37InBlock.gif        HoverButton.HoverButtonBehavior.callBaseMethod(this, 'initialize');
 38InBlock.gif        
 39InBlock.gif        
 40InBlock.gif        this._hoverHandler = Function.createDelegate(thisthis._onTargetHover);       
 41InBlock.gif        this._unhoverHandler = Function.createDelegate(thisthis._onTargetUnhover);    
 42InBlock.gif        if(this.get_element() && this._onButtonClick)
 43ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 44InBlock.gif            this._clickHandler = Function.createDelegate(this,this._onButtonClick);
 45InBlock.gif            $addHandler(this.get_element(),'click',this._clickHandler);
 46ExpandedSubBlockEnd.gif        }

 47ExpandedSubBlockStart.gifContractedSubBlock.gif        this._hoverBehavior = $create(AjaxControlToolkit.HoverBehavior, dot.gif{unhoverDelay:1, hoverElement: null}nullnullthis.get_element());
 48InBlock.gif        this._hoverBehavior.add_hover(this._hoverHandler);
 49InBlock.gif        this._hoverBehavior.add_unhover(this._unhoverHandler);  
 50InBlock.gif        // TODO: add your initalization code here
 51ExpandedSubBlockEnd.gif    }
,
 52InBlock.gif
 53ExpandedSubBlockStart.gifContractedSubBlock.gif    dispose : function() dot.gif{
 54InBlock.gif        // TODO: add your cleanup code here
 55ExpandedSubBlockStart.gifContractedSubBlock.gif         if (this._hoverBehavior) dot.gif{
 56InBlock.gif            this._hoverBehavior.dispose();
 57InBlock.gif            this._hoverBeahvior = null;            
 58ExpandedSubBlockEnd.gif        }
        
 59InBlock.gif        HoverButton.HoverButtonBehavior.callBaseMethod(this, 'dispose');
 60ExpandedSubBlockEnd.gif    }
,
 61InBlock.gif
 62InBlock.gif
 63InBlock.gif    _onTargetHover : function(eventArgs)
 64ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 65InBlock.gif        var e = this.get_element();   
 66InBlock.gif        Sys.UI.DomElement.removeCssClass(e, this._UnHoverButtonCssClass);     
 67InBlock.gif        Sys.UI.DomElement.addCssClass(e, this._HoverButtonCssClass);
 68ExpandedSubBlockEnd.gif    }
,
 69InBlock.gif    
 70InBlock.gif        //onUnHover
 71InBlock.gif    _onTargetUnhover: function(eventArgs)
 72ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 73InBlock.gif        var e = this.get_element();   
 74InBlock.gif        Sys.UI.DomElement.removeCssClass(e, this._HoverButtonCssClass);     
 75InBlock.gif        Sys.UI.DomElement.addCssClass(e, this._UnHoverButtonCssClass);
 76ExpandedSubBlockEnd.gif    }
,
 77InBlock.gif    _onButtonClick: function(eventArgs)
 78ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 79ExpandedSubBlockStart.gifContractedSubBlock.gif         if (this._OnButtonClickScript) dot.gif{
 80InBlock.gif                window.setTimeout(this._OnButtonClickScript, 0);
 81ExpandedSubBlockEnd.gif            }

 82ExpandedSubBlockEnd.gif    }
,
 83InBlock.gif    // TODO: (Step 2) Add your property accessors here
 84InBlock.gif    //
 85ExpandedSubBlockStart.gifContractedSubBlock.gif    get_HoverButtonCssClass : function() dot.gif{
 86InBlock.gif        return this._HoverButtonCssClass;
 87ExpandedSubBlockEnd.gif    }
,
 88InBlock.gif
 89ExpandedSubBlockStart.gifContractedSubBlock.gif    set_HoverButtonCssClass : function(value) dot.gif{
 90InBlock.gif        this._HoverButtonCssClass = value;
 91ExpandedSubBlockEnd.gif    }
,
 92ExpandedSubBlockStart.gifContractedSubBlock.gif     get_UnHoverButtonCssClass : function() dot.gif{
 93InBlock.gif        return this._UnHoverButtonCssClass;
 94ExpandedSubBlockEnd.gif    }
,
 95InBlock.gif
 96ExpandedSubBlockStart.gifContractedSubBlock.gif    set_UnHoverButtonCssClass : function(value) dot.gif{
 97InBlock.gif        this._UnHoverButtonCssClass = value;
 98ExpandedSubBlockEnd.gif    }
,
 99InBlock.gif    
100InBlock.gif    set_OnButtonClickScript: function(value)
101ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
102InBlock.gif        this._OnButtonClickScript = value;
103ExpandedSubBlockEnd.gif    }
,
104InBlock.gif    
105InBlock.gif    get_OnButtonClickScript: function()
106ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
107InBlock.gif        return this._OnButtonClickScript;
108ExpandedSubBlockEnd.gif    }

109ExpandedBlockEnd.gif}

110None.gif
111None.gifHoverButton.HoverButtonBehavior.registerClass('HoverButton.HoverButtonBehavior', AjaxControlToolkit.BehaviorBase);
112None.gif

这里注意一下第80行,window.setTimeout(this._OnButtonClickScript, 0); 这个_OnButtonClickScript是一个字符串,表示客户端页面上用户自己定义的一个函数的名字,通过这个语句就能够执行整个客户端函数。有了这样的机制,用户就能够自己定义按钮的响应函数了。
HoverButtonExtender.cs

None.gif// (c) Copyright Microsoft Corporation.
None.gif
// This source is subject to the Microsoft Permissive License.
None.gif
// See http://www.microsoft.com/resources/sharedsource/licensingbasics/sharedsourcelicenses.mspx.
None.gif
// All other rights reserved.
None.gif

None.gif
using System;
None.gif
using System.Web.UI.WebControls;
None.gif
using System.Web.UI;
None.gif
using System.ComponentModel;
None.gif
using System.ComponentModel.Design;
None.gif
using AjaxControlToolkit;
None.gif
using Microsoft.Web.UI;
None.gif
None.gif
None.gif[assembly: System.Web.UI.WebResource(
"HoverButton.HoverButtonBehavior.js""text/javascript")]
None.gif
None.gif
namespace HoverButton
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    [Designer(
typeof(HoverButtonDesigner))]
InBlock.gif    [ClientScriptResource(
"HoverButton.HoverButtonBehavior""HoverButton.HoverButtonBehavior.js")]
InBlock.gif    [TargetControlType(
typeof(Control))]
InBlock.gif    [RequiredScript(
typeof(HoverExtender))]
InBlock.gif    [RequiredScript(
typeof(CommonToolkitScripts))]
InBlock.gif    
public class HoverButtonExtender : ExtenderControlBase
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
// TODO: Add your property accessors here.
InBlock.gif        
//
InBlock.gif
        [ExtenderControlProperty]
InBlock.gif        [DefaultValue(
"")]
InBlock.gif        
public string HoverButtonCssClass
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return GetPropertyStringValue("HoverButtonCssClass");
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                SetPropertyStringValue(
"HoverButtonCssClass", value);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        [ExtenderControlProperty]
InBlock.gif        [DefaultValue(
"")]
InBlock.gif        
public string UnHoverButtonCssClass
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return GetPropertyStringValue("UnHoverButtonCssClass");
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                SetPropertyStringValue(
"UnHoverButtonCssClass", value);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        [ExtenderControlProperty]
InBlock.gif        [DefaultValue(
"")]
InBlock.gif        
public string OnButtonClickScript
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return GetPropertyStringValue("OnButtonClickScript");
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                SetPropertyStringValue(
"OnButtonClickScript", value);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
InBlock.gif
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif


该扩展的使用,代码如下:
 1ExpandedBlockStart.gifContractedBlock.gif<%dot.gif@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
 2None.gif
 3None.gif<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
 4ExpandedBlockStart.gifContractedBlock.gif<%dot.gif@ Register Assembly="HoverButton" Namespace="HoverButton" TagPrefix="cc1"%>
 5None.gif
 6None.gif<html xmlns="http://www.w3.org/1999/xhtml">
 7None.gif<head runat="server">
 8None.gif    <link href="CSS/StyleSheet.css" rel="stylesheet" type="text/css" />
 9None.gif    <title>测试HoverButton</title>
10None.gif</head>
11None.gif<body>
12None.gif    <form id="form1" runat="server">
13None.gif        <asp:ScriptManager ID="ScriptManager1" runat="server" />
14None.gif        <div>
15None.gif            <asp:textbox id="TextBox1" runat="server"></asp:textbox>
16None.gif            <asp:button id="Button1" runat="server" text="Button" />            
17None.gif            <cc1:HoverButtonExtender runat="server" id="hbe" targetcontrolid="Button1"  HoverButtonCssClass="hoverbutton" unhoverbuttoncssclass="unhoverbutton"></cc1:HoverButtonExtender>
18None.gif        </div>
19None.gif    </form>
20None.gif</body>
21None.gif</html>
22None.gif

希望能对您有帮助。

转载于:https://www.cnblogs.com/strinkbug/archive/2006/12/21/599462.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/263209.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

jQuery:表格的奇偶行变色,jquery实例之表格隔一行

jQuery:表格的奇偶行变色<html> <head> <title>表格变色</title> <style type"text/css"> .odd{background:#ffffee;} .even{background:#fff38f;} .first{background:red;} .last{background:blue;}…

分金币 Uva 11300

题意 给定N个人成环状坐&#xff0c;每个人初始分配Ai的金币&#xff0c;金币总数可以被N整除&#xff0c;每个人可以给左右相邻的人一定数量的金币使得最终每个人的金币数量相同&#xff0c;求转移数量最小的方案所转移的总金币数量。 N<1000000 对每组数据保证输出在INT64…

LeetCode—<搜索与回溯专项>剑指 Offer 55 - I、55 - II、64、68 - I、68 - II

剑指 Offer 55 - I. 二叉树的深度、55 - II. 平衡二叉树、64. 求12…n、68 - I. 二叉搜索树的最近公共祖先、68 - II. 二叉树的最近公共祖先 题目描述&#xff1a; [55 - I] [55 - II] [64] 求 12…n &#xff0c;要求不能使用乘除法、for、while、if、else、switch、case等关…

Tip:使用Extender的ResolveControlID事件

1、分析 不知道大家在使用ASP.NET AJAX Control Toolkit里的各个Extender控件时&#xff0c;有没有接触过ResolveControlID事件&#xff1f;这个事件在官方也只用了很少的笔墨来描述。的确&#xff0c;这个事件不太常用&#xff0c;看看ASP.NET AJAX的演示站点&#xff0c;根本…

我常用的shell命令

我常用的shell命令tar:tar jxvf filename.tar.bz2tar xvf filename.tartar cvf filename.tar file tar cjvf filename.tar.bz2 file wc:ls -l | wc -ltr:ls -l | tr a-z A-Zgrep:ls -l | grep ^dls -l | grep -n ^dls -l | grep -v ^decho "theaaaathebbbbthe | grep \<…

Git常用命令整理

git init # 初始化本地git仓库&#xff08;创建新仓库&#xff09;git config --global user.name "xxx" # 配置用户名git config --global user.email "xxxxxx.com" # 配置邮件git config --global color.ui true # git status等命令自动着色git config -…

LeetCode—<分治专项>剑指 Offer 07、16、33

剑指 Offer 07. 重建二叉树、16. 数值的整数次方、33. 二叉搜索树的后序遍历序列 题目描述&#xff1a; [07] 输入某二叉树的前序遍历和中序遍历的结果&#xff0c;请构建该二叉树并返回其根节点。 假设输入的前序遍历和中序遍历的结果中都不含重复的数字。 [16] 实现 pow(x,…

唱歌的方法与技巧[收集]

Method 1我想很多人都有些嗓子限制,就认为自己不适合唱歌等...其实绝大多数人,都是可以唱的非常棒的,就算嗓子很不好,也可以通过自我的锻炼逐渐培养出来的.其实唱歌到达一定阶段,可以说,逐渐的少用嗓子.有些人唱歌时,用手压着嗓子,不让嗓子向上,其实是有原因的.但这并不是正确的…

练习(00008)

参考&#xff1a;第7章 JavaScript.doc/第7章 JavaScript.文件夹转载于:https://www.cnblogs.com/lesvies/archive/2011/09/19/2181495.html

《算法导论》——MergeSort

前言&#xff1a; 在今后的日子里&#xff0c;我将持续更新博客&#xff0c;讨论《算法导论》一书中的提到的各算法的C实现。初来乍到&#xff0c;请多指教。 今日主题&#xff1a;   今天讨论《算法导论》第二章算法基础中的归并排序算法。下面是该算法的代码Merge.h&#x…

MySQL 常用运算符

1.算数运算符 加 mysql> select 12; 减 mysql> select 2-1; 乘 mysql> select 2*3; 除 mysql> select 5/3; 商 mysql> SELECT 5 DIV 2; 模 mysql> select 5%2,mod(5,2); 2.比较运算符 等于 mysql> select 10,11,nullnull; 不等于 mysql> select 1<&…

react 路由v6

这里是区别&#xff1a;V5 vs V6 这里是官网&#xff1a;可以查看更多高级属性 基本使用&#xff1a; 1、配置文件 src/routes/index import React from "react";const Home React.lazy(() > import("../Pages/Home")); const About React.lazy(()…

LeetCode—<位运算专项>剑指 Offer 15、56 - I、56 - II、65

剑指 Offer 15. 二进制中1的个数、56 - I. 数组中数字出现的次数、56 - II. 数组中数字出现的次数 II、65. 不用加减乘除做加法 题目描述&#xff1a; [15] 编写一个函数&#xff0c;输入是一个无符号整数&#xff08;以二进制串的形式&#xff09;&#xff0c;返回其二进制表…

Asp.net2.0 学习资源(转载)

asp.net 2.0 http://beta.asp.net/QUICKSTART/aspnet/http://msdn.microsoft.com/vstudio/express/vwd/learning/default.aspxasp.net 1.0 名称&#xff1a;快速入门地址&#xff1a;http://chs.gotdotnet.com/quickstart/描述&#xff1a;本站点是微软.NET技术的快速入门网站&…

java学习笔记十一——对象转型

向上转型&#xff1a;子类对象当做父类对象来使用&#xff0c;因为子类对象拥有父类对象的所有成员&#xff0c;所以不会发生任何错误。向下转型&#xff1a;父类对象当做子类对象来使用&#xff0c;因为子类对象部分特性父类并没有&#xff0c;所以需要加强制转换符。向上转型…

LeetCode—<数学专项>剑指 Offer 14 - I、39、57 - II、62、66

剑指 Offer 14- I. 剪绳子、39. 数组中出现次数超过一半的数字、57 - II. 和为s的连续正数序列、62. 圆圈中最后剩下的数字、66. 构建乘积数组 题目描述&#xff1a; [14 - I] 给你一根长度为 n 的绳子&#xff0c;请把绳子剪成整数长度的 m 段&#xff08;m、n都是整数&#…

Win8下在Vmware11中安装使用苹果系统OS X 10.10

原文:Win8下在Vmware11中安装使用苹果系统OS X 10.10近来因为需要做 iOS 的项目&#xff0c;所以需要多花一些时间看看敲敲代码。因为自己手头上并没有 Mac&#xff08;过年为了闲的时候能玩玩游戏买了联想&#xff0c;唉&#xff09;&#xff0c;想想不能只靠每天在公司的时间…

图片滑动效果(转)

var $ function(id) {return "string" typeof id ? document.getElementById(id) : id; };function Event(e) {var oEvent document.all ? window.event : e;if (document.all) {if (oEvent.type "mouseout") {oEvent.relatedTarget oEvent.toEleme…

《Windows Communication Foundation之旅》系列之二

《Windows Communication Foundation之旅》系列之二 三、WCF的技术要素作为基于SOA&#xff08;Service Oriented Architecture&#xff09;的一个框架产品&#xff0c;WCF最重要的就是能够快捷的创建一个服务&#xff08;Service&#xff09;。如下图所示&#xff0c;一个WCF…

JS---捕捉URL地址,以及模仿GET方法

主页博客相册个人档案好友查看文章JS 模拟的GET方法代码: function _GET(){var url window.top.location.href;var start url.indexOf(?)1;var end url.length;var Query_String url.substring(start, end);var Get Query_String.split(&);for (var i in Get){;var t…