系统地学习ASP.NET AJAX(5) - 客户端脚本编程(Sys.UI命名空间下的类和快捷方法)(转)...

系统地学习ASP.NET AJAX(5) - 客户端脚本编程(Sys.UI命名空间下的类和快捷方法)



作者:webabcd


介绍
Sys.UI命名空间下包含与UI相关的类,像控件、事件和Microsoft AJAX Library里的UI属性之类的。快捷方法就是用简短的代码调用某个方法。


关键
1、Sys.UI.DomElement Class
    ·Sys.UI.DomElement.addCssClass(元素, "CssClass名");
    ·Sys.UI.DomElement.containsCssClass(元素, "CssClass名"); (返回值为元素是否包含有指定的CssClass名)
    ·Sys.UI.DomElement.getBounds(元素); (返回值为JSON对象,其内field分别为:x,y,width,height;分别代表元素的x坐标,y坐标,宽,高)
    ·Sys.UI.DomElement.getLocation(元素); (返回值为JSON对象,其内field分别为:x,y;分别代表元素的x坐标,y坐标)
    ·Sys.UI.DomElement.removeCssClass(元素, "CssClass名");
    ·Sys.UI.DomElement.setLocation(元素, x坐标, y坐标);
    ·Sys.UI.DomElement.toggleCssClass(元素, "CssClass名");

2、Sys.UI.DomEvent Class
    ·Sys.UI.DomEvent.addHandler(元素, "事件名称", 事件处理器);
    ·Sys.UI.DomEvent.addHandlers(元素, "事件名称", {"事件名称1", 事件处理器1, "事件名称2", 事件处理器2, ...});
    ·Sys.UI.DomEvent.clearHandlers(元素);
    ·Sys.UI.DomEvent.removeHandler(元素, "事件名称", 事件处理器);
    ·该类下的Field
        ·altKey // 是否是关联的alt键触发的事件?是true;否false
        ·button // Sys.UI.MouseButton枚举,用于指定当相关事件发生时,鼠标按键的状态
        ·charCode // 触发了事件的键的字符代码
        ·shiftKey // 发生事件时是否按下了Shift键
        ·clientX // 发生事件时鼠标的x坐标
        ·clientY // 发生事件时鼠标的y坐标
        ·ctrlKey // 发生事件时是否按下了Ctrl键
        ·offsetX // 发生事件时鼠标与触发事件的对象之间的x偏移量
        ·offsetY // 发生事件时鼠标与触发事件的对象之间的y偏移量
        ·screenX // 发生事件时鼠标与用户屏幕之间的x偏移量
        ·screenY // 发生事件时鼠标与用户屏幕之间的y偏移量
        ·target // 触发事件的对象
        ·type // 被触发的事件的名称

3、快捷方法
    ·$get()相当于Sys.UI.DomElement.getElementById()
    ·$addHandler()相当于Sys.UI.DomEvent.addHandler()
    ·$addHandlers()相当于Sys.UI.DomEvent.addHandlers()
    ·$clearHandlers()相当于Sys.UI.DomEvent.clearHandlers()
    ·$removeHandler()相当于Sys.UI.DomEvent.removeHandler()
    ·$create()相当于Sys.Component.create()
    ·$find()相当于Sys.Application.findComponent()

4、其它请查看官方文档


示例
DomElement.aspx

ExpandedBlockStart.gif ContractedBlock.gif <% dot.gif @ Page Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="DomElement.aspx.cs"
ExpandedBlockEnd.gif    Inherits
="ClientScripting_SysUI_DomElement" Title="Sys.UI.DomElement Class" 
%>
None.gif
None.gif
< asp:Content  ID ="Content1"  ContentPlaceHolderID ="ContentPlaceHolder1"  runat ="Server" >
ExpandedBlockStart.gifContractedBlock.gif    
< style  type ="text/css" > dot.gif
InBlock.gif        .redBackgroundColor 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{dot.gif}{ 
InBlock.gif            background-color
:Red;
ExpandedSubBlockEnd.gif        
}

InBlock.gif        .blueBackgroundColor 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{dot.gif}{ 
InBlock.gif            background-color
:Blue;
ExpandedSubBlockEnd.gif        
}

ExpandedBlockEnd.gif    
</ style >
None.gif    
< p >
None.gif        
< input  type ="button"  id ="Button1"  value ="转换CssClass"   />
None.gif    
</ p >
None.gif    
< p >
None.gif        
< input  type ="button"  id ="Button2"  value ="移除CssClass"   />
None.gif    
</ p >
None.gif    
< p >
None.gif        
< input  type ="button"  id ="Button3"  value ="移动Lable1"  onclick ="Button3_onclick()"   />
None.gif    
</ p >
None.gif    
< p >
None.gif        
< asp:Label  ID ="Label1"  runat ="server"  BackColor ="Black"  ForeColor ="White"  Text ="Label1"
None.gif            Width
="102px" ></ asp:Label >
None.gif    
</ p >
None.gif    
< p >
None.gif        
< asp:TextBox  ID ="TextBox1"  runat ="server"  TextMode ="MultiLine"  Width ="500px"  Height ="300px" ></ asp:TextBox >
None.gif    
</ p >
None.gif
ExpandedBlockStart.gifContractedBlock.gif    
< script  type ="text/javascript"  language ="javascript" > dot.gif
InBlock.gif    
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//*
InBlock.gif    $get()相当于Sys.UI.DomElement.getElementById()
InBlock.gif    $addHandler()相当于Sys.UI.DomEvent.addHandler()
InBlock.gif    $addHandlers()相当于Sys.UI.DomEvent.addHandlers()
InBlock.gif    $clearHandlers()相当于Sys.UI.DomEvent.clearHandlers()
InBlock.gif    $removeHandler()相当于Sys.UI.DomEvent.removeHandler()
InBlock.gif    $create()相当于Sys.Component.create()
InBlock.gif    $find()相当于Sys.Application.findComponent()
ExpandedSubBlockEnd.gif    
*/

InBlock.gif
InBlock.gif    
// 给ID为“Button1”的元素增加“click”的事件处理器,处理方法为toggleCssClassMethod
InBlock.gif
    $addHandler($get("Button1"), "click", toggleCssClassMethod);
InBlock.gif    
// 给ID为“Button2”的元素增加“click”的事件处理器,处理方法为blueBackgroundColor
InBlock.gif
    $addHandler($get("Button2"), "click", removeCssClassMethod);
InBlock.gif
InBlock.gif    
// 给ID为“Button1”的元素增加增加名为“redBackgroundColor”的CssClass
InBlock.gif
    Sys.UI.DomElement.addCssClass($get("Button1"), "redBackgroundColor");
InBlock.gif    
// 给ID为“Button2”的元素增加增加名为“blueBackgroundColor”的CssClass
InBlock.gif
    Sys.UI.DomElement.addCssClass($get("Button2"), "blueBackgroundColor");
InBlock.gif
InBlock.gif    
function toggleCssClassMethod(eventElement)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
// 元素eventElement.target是否有名为“redBackgroundColor”的CssClass
InBlock.gif
        if (Sys.UI.DomElement.containsCssClass(eventElement.target, "redBackgroundColor"))
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
// 将eventElement.target的CssClass变为“blueBackgroundColor”
InBlock.gif
            Sys.UI.DomElement.toggleCssClass(eventElement.target, "blueBackgroundColor");
ExpandedSubBlockEnd.gif        }

InBlock.gif        
else
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
// 将eventElement.target的CssClass变为“redBackgroundColor”
InBlock.gif
            Sys.UI.DomElement.toggleCssClass(eventElement.target, "redBackgroundColor");
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif 
InBlock.gif    
function removeCssClassMethod(eventElement) 
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
// 移除eventElement.target的CssClass
InBlock.gif
        Sys.UI.DomElement.removeCssClass(eventElement.target, "blueBackgroundColor");
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
var elementRef = $get("<%= Label1.ClientID %>");
InBlock.gif    
InBlock.gif    
// 获取元素的Bounds信息
InBlock.gif
    var elementBounds = Sys.UI.DomElement.getBounds(elementRef);
InBlock.gif    
InBlock.gif    
var result = '';
InBlock.gif    result 
+= "Label1的x坐标 = " + elementBounds.x + "\r\n";
InBlock.gif    result 
+= "Label1的y坐标 = " + elementBounds.y + "\r\n";
InBlock.gif    result 
+= "Label1的宽度 = " + elementBounds.width + "\r\n";
InBlock.gif    result 
+= "Label1的高度 = " + elementBounds.height + "\r\n\r\n";
InBlock.gif    
InBlock.gif    
// 获取元素的位置信息
InBlock.gif
    var elementLoc = Sys.UI.DomElement.getLocation(elementRef);
InBlock.gif    
InBlock.gif    result 
+= "Label1的坐标(x, y) = (" + elementLoc.x + "," + elementLoc.y + ")\r\n\r\n";
InBlock.gif
InBlock.gif    $get(
"<%= TextBox1.ClientID %>").value = result;
InBlock.gif
InBlock.gif
InBlock.gif    
function Button3_onclick()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        result 
= "";
InBlock.gif        
InBlock.gif        
// 设置元素的位置(元素,x坐标,y坐标)
InBlock.gif
        Sys.UI.DomElement.setLocation(elementRef, 100, elementLoc.y + 100);
InBlock.gif        
InBlock.gif        elementLoc 
= Sys.UI.DomElement.getLocation(elementRef);
InBlock.gif        
InBlock.gif        result 
+= "点击移动按钮后  - Label1的坐标(x, y) = (" + elementLoc.x + "," + elementLoc.y + ")\r\n";
InBlock.gif
InBlock.gif        $get(
"<%= TextBox1.ClientID %>").value += result;
ExpandedSubBlockEnd.gif    }

InBlock.gif    
ExpandedBlockEnd.gif    
</ script >
None.gif
None.gif
</ asp:Content >
None.gif


运行结果
1、单击“转换CssClass”按钮
该按钮的样式会在指定的两种CssClass间切换

2、单击“移除CssClass”按钮
该按钮的指定CssClass会被移除

3、TextBox显示为:
Label1的x坐标 = 276
Label1的y坐标 = 189
Label1的宽度 = 102

Label1的高度 = 15 Label1的坐标(x, y) = (276,189)

单击“移动Lable1”按钮后(Lable1的位置发生变化)TextBox显示为:
Label1的x坐标 = 276
Label1的y坐标 = 189
Label1的宽度 = 102

Label1的高度 = 15 Label1的坐标(x, y) = (276,189)

点击移动按钮后  - Label1的坐标(x, y) = (100,289)


DomEvent.aspx

ExpandedBlockStart.gif ContractedBlock.gif <% dot.gif @ Page Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="DomEvent.aspx.cs"
ExpandedBlockEnd.gif    Inherits
="ClientScripting_SysUI_DomEvent" Title="Sys.UI.DomEvent Class" 
%>
None.gif
None.gif
< asp:Content  ID ="Content1"  ContentPlaceHolderID ="ContentPlaceHolder1"  runat ="Server" >
None.gif    
< p >
None.gif        单击按钮后查看事件的详细信息
None.gif    
</ p >
None.gif    
< p >
None.gif        
< input  type ="button"  id ="Button1"  value ="按钮(s)"  accesskey ="s"   />
None.gif    
</ p >
None.gif    
< p >
None.gif        
< asp:Label  ID ="Label1"  runat ="server" ></ asp:Label >
None.gif    
</ p >
None.gif    
< p > &nbsp; </ p >
None.gif    
< p >
None.gif        同时添加多个事件处理器
None.gif    
</ p >
None.gif    
< p >
None.gif        
< input  type ="button"  id ="Button2"  value ="按钮2"   />
None.gif    
</ p >
None.gif    
< p >
None.gif        
< asp:Label  ID ="Label2"  runat ="server" ></ asp:Label >
None.gif    
</ p >
None.gif
ExpandedBlockStart.gifContractedBlock.gif    
< script  type ="text/javascript"  language ="javascript" > dot.gif
InBlock.gif    
InBlock.gif    
// 给ID为“Button1”的元素增加“click”的事件处理器,处理方法为processEventInfo
InBlock.gif
    $addHandler($get("Button1"), "click", processEventInfo);
InBlock.gif    
InBlock.gif    
var ary = 
InBlock.gif    [
InBlock.gif        
// 以下为DomEvent类的Field
InBlock.gif
        'altKey', // 是否是关联的alt键触发的事件?是true;否false
InBlock.gif
        'button', // Sys.UI.MouseButton枚举,用于指定当相关事件发生时,鼠标按键的状态
InBlock.gif
        'charCode', // 触发了事件的键的字符代码
InBlock.gif
        'shiftKey', // 发生事件时是否按下了Shift键
InBlock.gif
        'clientX', // 发生事件时鼠标的x坐标
InBlock.gif
        'clientY', // 发生事件时鼠标的y坐标
InBlock.gif
        'ctrlKey', // 发生事件时是否按下了Ctrl键
InBlock.gif
        'offsetX', // 发生事件时鼠标与触发事件的对象之间的x偏移量
InBlock.gif
        'offsetY', // 发生事件时鼠标与触发事件的对象之间的y偏移量
InBlock.gif
        'screenX', // 发生事件时鼠标与用户屏幕之间的x偏移量
InBlock.gif
        'screenY', // 发生事件时鼠标与用户屏幕之间的y偏移量
InBlock.gif
        'target', // 触发事件的对象
InBlock.gif
        'type' // 被触发的事件的名称
InBlock.gif
     ];
InBlock.gif
InBlock.gif    
function processEventInfo(eventElement) 
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
var result = '';
InBlock.gif        
InBlock.gif        
for (var i = 0, l = ary.length; i < l; i++
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
var arrayVal = ary[i];
InBlock.gif            
InBlock.gif            
if (typeof(arrayVal) !== 'undefined') 
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
try 
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
// 输出结果举例:eventElement.altKey
InBlock.gif
                    result += arrayVal + " = " + eval("eventElement." + arrayVal) + '<br/>';
ExpandedSubBlockEnd.gif                }

InBlock.gif                
catch (e)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    alert(e.message);
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        result 
+= eventElement.target.id;
InBlock.gif        
InBlock.gif        $get(
"<%= Label1.ClientID %>").innerHTML = result;
ExpandedSubBlockEnd.gif    }

InBlock.gif    
ExpandedBlockEnd.gif    
</ script >
None.gif    
ExpandedBlockStart.gifContractedBlock.gif    
< script  type ="text/javascript"  language ="javascript" > dot.gif
InBlock.gif    
InBlock.gif    
// 给ID为“Button2”的元素增加多个事件处理器
InBlock.gif
    Sys.UI.DomEvent.addHandlers
InBlock.gif    (
InBlock.gif        $get(
"Button2"), 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            click: processEventInfo, 
InBlock.gif            mouseover: processEventInfo, 
InBlock.gif            mouseout: processEventInfo
ExpandedSubBlockEnd.gif        }

InBlock.gif    );
InBlock.gif
InBlock.gif    
function processEventInfo(eventElement) 
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
var result = '';
InBlock.gif        result 
+= eventElement.type;
InBlock.gif        $get(
"<%= Label2.ClientID %>").innerHTML = result;
ExpandedSubBlockEnd.gif    }

InBlock.gif    
ExpandedBlockEnd.gif    
</ script >
None.gif
None.gif
</ asp:Content >
None.gif


运行结果
1、单击“按钮(s)”(单击位置不同,则显示结果不同)
altKey = false
button = 0
charCode = undefined
shiftKey = false
clientX = 294
clientY = 109
ctrlKey = false
offsetX = 14
offsetY = 3
screenX = 294
screenY = 205
target = [object]
type = click
Button1


2、“按钮2”
鼠标经过“按钮2”显示mouseover
鼠标移出“按钮2”显示mouseout
单击“按钮2”显示click


Others.aspx

ExpandedBlockStart.gif ContractedBlock.gif <% dot.gif @ Page Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="Others.aspx.cs"
ExpandedBlockEnd.gif    Inherits
="ClientScripting_SysUI_Others" Title="Sys.UI Other Class" 
%>
None.gif
None.gif
< asp:Content  ID ="Content1"  ContentPlaceHolderID ="ContentPlaceHolder1"  runat ="Server" >
None.gif    
< fieldset >
None.gif        
< legend > title </ legend >
None.gif        
< div  id ="parentDiv" >
None.gif            
< div  id ="childDiv" >
None.gif                aabbcc
None.gif            
</ div >
None.gif        
</ div >
None.gif        
< div >
None.gif            
< input  type ="button"  id ="btnHide"  onclick ="btnHide_onclick()"  value ="VisibilityMode.hide"   />
None.gif            
&nbsp;
None.gif            
< input  type ="button"  id ="btnCollapse"  onclick ="btnCollapse_onclick()"  value ="VisibilityMode.collapse"   />
None.gif        
</ div >
None.gif    
</ fieldset >
None.gif
ExpandedBlockStart.gifContractedBlock.gif    
< script  type ="text/javascript"  language ="javascript" > dot.gif
InBlock.gif            
InBlock.gif        
// 让元素“childDiv”变为Control
InBlock.gif
        var a = new Sys.UI.Control($get('childDiv'));
InBlock.gif        
// 让元素“parentDiv”变为Control
InBlock.gif
        var b = new Sys.UI.Control($get('parentDiv'));
InBlock.gif
InBlock.gif        
// 先取得a的父控件,然后再取得这个父控件的id
InBlock.gif
        alert(a.get_parent().get_id());
InBlock.gif        
// 让控件“a”变为元素,然后取得这个元素的id
InBlock.gif
        alert(a.get_element().id);
InBlock.gif
InBlock.gif        
function btnHide_onclick()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
// 隐藏控件a
InBlock.gif
            a.set_visible(false);
InBlock.gif            
// 隐藏方式为hide,占用页面空间
InBlock.gif
            a.set_visibilityMode(Sys.UI.VisibilityMode.hide);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
InBlock.gif        
function btnCollapse_onclick()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
// 隐藏控件a
InBlock.gif
            a.set_visible(false);
InBlock.gif            
// 隐藏方式为collapse,不占用页面空间
InBlock.gif
            a.set_visibilityMode(Sys.UI.VisibilityMode.collapse);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
ExpandedBlockEnd.gif    
</ script >
None.gif
None.gif
</ asp:Content >
None.gif


运行结果
1、页面加载后
弹出框,信息:parentDiv
弹出框,信息:childDiv

2、单击“VisibilityMode.hide”按钮
“childDiv”被隐藏,但是会占用页面空间

3、单击“VisibilityMode.collapse”按钮
“childDiv”被隐藏,而且不会占用页面空间

转载于:https://www.cnblogs.com/footleg/archive/2007/09/22/902412.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值