利用MS AJAX 扩展服务器端控件
通过MS Ajax可以扩展一个服务器端控件在客户端呈现后的特性,使其界面更加友好。
实例代码:IScriptControl.rar
一、创建网站,选择ASP.NET AJAX-Enabled Web Site.
二、向项目中添加一个类,使其派生自TextBox,并实现IScriptControl接口。如下代码实例:
public class SampleTextBox : TextBox, IScriptControl
三、这个控件我们将实现两个属性:
HighlightCSSClass 控件得到焦点后的样式。当控件得到焦点的时候使其能够高亮显示。
NoHighlightCssClass 失去焦点的控件的样式。
public string HighlightCssClass
{
get { return _highlightCssClass; }
set { _highlightCssClass = value; }
}
public string NoHighlightCssClass
{
get { return _noHighlightCssClass; }
set { _noHighlightCssClass = value; }
}
四、接口IScriptControl 的实现。
GetScriptDescriptors() 返回一个包含控件客户端实例的属性和事件句柄的 ScriptDescriptor 类型的数组。
GetScriptReferences() 返回一个包含控件客户端 javascript 代码的ScriptReference 类型的数组。
在这个实例中,我们用四个函数来实现这两个函数。代码入下:
protected virtual IEnumerable<ScriptReference> GetScriptReferences()
{
ScriptReference reference = new ScriptReference();
reference.Path = ResolveClientUrl("SampleTextBox.js");
return new ScriptReference { descriptor };
}
IEnumerable<ScriptReference> IScriptControl.GetScriptReferences()
{
return GetScriptReferences();
}
IEnumerable<ScriptDescriptor> IScriptControl.GetScriptDescriptors()
{
return GetScriptDescriptors();
} 五、这册控件。代码比较简单,所以就不再多加讲述,入下:
protected override void OnPreRender(EventArgs e)
{
if (!this.DesignMode)
{
// Test for ScriptManager and register if it exists
sm = ScriptManager.GetCurrent(Page);
if (sm == null)
throw new HttpException("A ScriptManager control must exist on the current page.");
sm.RegisterScriptControl(this);
}
base.OnPreRender(e);
}
protected override void Render(HtmlTextWriter writer)
{
if (!this.DesignMode)
sm.RegisterScriptDescriptors(this);
base.Render(writer);
}
六、下边是我们新添加的类的完整代码:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
namespace TextBoxExtender
{
/**//// <summary>
/// SampleTextBox 的摘要说明
/// </summary>
public class SampleTextBox : TextBox, IScriptControl
{
private string _highlightCssClass;
private string _noHighlightCssClass;
private ScriptManager sm;
public string HighlightCssClass
{
get { return _highlightCssClass; }
set { _highlightCssClass = value; }
}
public string NoHighlightCssClass
{
get { return _noHighlightCssClass; }
set { _noHighlightCssClass = value; }
}
protected override void OnPreRender(EventArgs e)
{
if (!this.DesignMode)
{
// Test for ScriptManager and register if it exists
sm = ScriptManager.GetCurrent(Page);
if (sm == null)
throw new HttpException("A ScriptManager control must exist on the current page.");
sm.RegisterScriptControl(this);
}
base.OnPreRender(e);
}
protected override void Render(HtmlTextWriter writer)
{
if (!this.DesignMode)
sm.RegisterScriptDescriptors(this);
base.Render(writer);
}
protected virtual IEnumerable<ScriptReference> GetScriptReferences()
{
ScriptReference reference = new ScriptReference();
reference.Path = ResolveClientUrl("SampleTextBox.js");
return new ScriptReference { descriptor };
}
IEnumerable<ScriptReference> IScriptControl.GetScriptReferences()
{
return GetScriptReferences();
}
IEnumerable<ScriptDescriptor> IScriptControl.GetScriptDescriptors()
{
return GetScriptDescriptors();
}
}
}
七、创建客户端控件。为客户端控件注册一个命名空间,并实现各个属性和事件:
// 为控件注册命名控件
Type.registerNamespace("Samples");
//
// 定义控件的属性
//
Samples.SampleTextBox = function(element) {
Samples.SampleTextBox.initializeBase(this, );
this._highlightCssClass = null;
this._nohighlightCssClass = null;
}
//
// 为控件创建属性
//
Samples.SampleTextBox.prototype = {
initialize : function() {
Samples.SampleTextBox.callBaseMethod(this, "initialize");
this._onfocusHandler = Function.createDelegate(this, this._onFocus);
this._onblurHandler = Function.createDelegate(this, this._onBlur);
$addHandlers(this.get_element(),
{ "focus" : this._onFocus,
"blur" : this._onBlur },
this);
this.get_element().className = this._nohighlightCssClass;
},
dispose
-
相关文章
2秒记住本站域名
玩过泡泡龙吗?Readygo?Go! 再加上.Com.Cn的后缀,那就是大名小顶的ReadyGo.com.cn
