现在的位置: 首页 > 综合 > 正文

系统地学习ASP.NET AJAX(7) – 客户端脚本编程(Sys命名空间下的类)

2013年07月06日 ⁄ 综合 ⁄ 共 5602字 ⁄ 字号 评论关闭
作者:webabcd

介绍
Sys命名空间是Microsoft AJAX
Library的根命名空间。本文主要学习一下其中的Application类、CultureInfo类和StringBuilder类。

关键
1、Application
Class
    ·init事件 - 脚本加载完毕,对象创建之前。
    ·load事件 -
对象被创建和初始化。可以用pageLoad()
    ·unload事件 - window.unload时。可以用pageUnload()
   
·notifyScriptLoaded() - 通知ScriptManager某段脚本已经加载完毕

2、CultureInfo
Class
    ·CurrentCulture字段 - 当前的Culture,返回CurrentCulture对象
    ·name字段 -
Culture的名称
    ·dateTimeFormat - 获得dateTimeFormat对象,其内有n多格式化类型
   
·numberFormat - 获得numberFormat对象,其内有n多格式化类型

3、StringBuilder Class
   
·append(text) - 添加指定字符串到StringBuilder对象的结尾
    ·appendLine() -
添加一个换行符到StringBuilder对象的结尾
    ·appendLine(text) -
添加指定字符串到StringBuilder对象的结尾并添加一个换行符
    ·clear() -
清除StringBuilder对象所有内容
    ·isEmpty() -  StringBuilder对象的内容是否为空
   
·toString() - 将StringBuilder对象的内容转换为字符串
    ·toString(separator) -
在StringBuilder对象内的每一个元素的结尾处添加指定字符串

4、其它请查看官方文档

示例
CustomButton.js

Type.registerNamespace("Demo");

Demo.CustomButton 
= function(element) 
{
    
// 该类继承自Sys.UI.Control,调用基类构造函数
    Demo.CustomButton.initializeBase(this, [element]);

    
this._clickDelegate = null;
    
this._hoverDelegate = null;
    
this._unhoverDelegate = null;
}

Demo.CustomButton.prototype 
= 
{
    
// 定义text属性 - 元素显示的信息
    get_text: function() 
    
{
        
// element - Sys.UI.Control的属性
        return this.get_element().innerHTML;
    }
,
    set_text: 
function(value) 
    
{
        
this.get_element().innerHTML = value;
    }
,

    
// 添加或移除click事件
    add_click: function(handler) 
    
{
        
// events - Sys.Component的属性
        this.get_events().addHandler('click', handler);
    }
,
    remove_click: 
function(handler) 
    
{
        
this.get_events().removeHandler('click', handler);
    }
,
    
    
// 添加或移除hover事件
    add_hover: function(handler) 
    
{
        
this.get_events().addHandler('hover', handler);
    }
,
    remove_hover: 
function(handler) 
    
{
        
this.get_events().removeHandler('hover', handler);
    }
,

    
// 添加或移除unhover事件
    add_unhover: function(handler) 
    
{
        
this.get_events().addHandler('unhover', handler);
    }
,
    remove_unhover: 
function(handler) 
    
{
        
this.get_events().removeHandler('unhover', handler);
    }
,

    
// 释放资源
    dispose: function() 
    
{
        
var element = this.get_element();

        
if (this._clickDelegate) 
        
{
            
// Sys.UI.DomEvent removeHandler()
            $removeHandler(element, 'click', this._clickDelegate);
            
delete this._clickDelegate;
        }


        
if (this._hoverDelegate) 
        
{
            $removeHandler(element, 'focus', 
this._hoverDelegate);
            $removeHandler(element, 'mouseover', 
this._hoverDelegate);
            
delete this._hoverDelegate;
        }


        
if (this._unhoverDelegate) 
        
{
            $removeHandler(element, 'blur', 
this._unhoverDelegate);
            $removeHandler(element, 'mouseout', 
this._unhoverDelegate);
            
delete this._unhoverDelegate;
        }

        Demo.CustomButton.callBaseMethod(
this, 'dispose');
    }
,

    
// 初始化
    initialize: function() 
    
{
        
var element = this.get_element();

        
if (!element.tabIndex) element.tabIndex = 0;

        
if (this._clickDelegate === null
        
{
            
// Function.createDelegate用来创建一个调用“this”上下文下的特定函数的委托
            this._clickDelegate = Function.createDelegate(thisthis._clickHandler);
        }

        
// Sys.UI.DomEvent addHandler()
        $addHandler(element, 'click', this._clickDelegate);

        
if (this._hoverDelegate === null
        
{
            
this._hoverDelegate = Function.createDelegate(thisthis._hoverHandler);
        }

        $addHandler(element, 'mouseover', 
this._hoverDelegate);
        $addHandler(element, 'focus', 
this._hoverDelegate);

        
if (this._unhoverDelegate === null
        
{
            
this._unhoverDelegate = Function.createDelegate(thisthis._unhoverHandler);
        }

        $addHandler(element, 'mouseout', 
this._unhoverDelegate);
        $addHandler(element, 'blur', 
this._unhoverDelegate);

        Demo.CustomButton.callBaseMethod(
this, 'initialize');
    }
,
    
    
// click事件处理器
    _clickHandler: function(event) 
    
{
        
var h = this.get_events().getHandler('click');
        
if (h) h(this, Sys.EventArgs.Empty);
    }
,
    
// hover事件处理器
    _hoverHandler: function(event) 
    
{
        
var h = this.get_events().getHandler('hover');
        
if (h) h(this, Sys.EventArgs.Empty);
    }
,
    
// unhover事件处理器
    _unhoverHandler: function(event) 
    
{
        
var h = this.get_events().getHandler('unhover');
        
if (h) h(this, Sys.EventArgs.Empty);
    }

}

Demo.CustomButton.registerClass('Demo.CustomButton', Sys.UI.Control);

// 通知ScriptManager这段脚本已经加载完毕
if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();

Application.aspx

<%@ Page Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="Application.aspx.cs"
    Inherits
="ClientScripting_Sys_Application" Title="init Event和load Event和unload Event" 
%>

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
    
<asp:ScriptManagerProxy ID="ScriptManagerProxy1" runat="Server">
        
<Scripts>
            
<asp:ScriptReference Path="~/ClientScripting/Sys/CustomButton.js" />
        
</Scripts>
    
</asp:ScriptManagerProxy>

    
<script type="text/javascript">
    
        Sys.Application.add_init(applicationInitHandler);
        
function applicationInitHandler() 
        
{
            
// Sys.Component.create()
            $create
            (
                Demo.CustomButton, 
                
{text: '自定义Button(Button1)'}
                
{click: doClick, hover: doHover, unhover: doUnhover},
                
null
                $get('Button1')
            );
            
            $create
            (
                Demo.CustomButton, 
                
{text:

抱歉!评论已关闭.