透過 MS Ajax,我們可以擴充一個伺服器端控制項在客戶端呈現後的特性,讓界面更加友善。以下以一個會在獲得焦點時高亮的 TextBox 為例。
建立網站時選擇「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; } }
GetScriptDescriptors() 回傳包含控制項客戶端實例屬性與事件的控制項 ScriptDescriptor 陣列;GetScriptReferences() 回傳包含客戶端 JavaScript 程式碼的 ScriptReference 陣列:
protected virtual IEnumerable<ScriptReference> GetScriptReferences()
{
ScriptReference reference = new ScriptReference();
reference.Path = ResolveClientUrl("SampleTextBox.JS");
return new ScriptReference[] { reference };
}
protected virtual IEnumerable<ScriptDescriptor> GetScriptDescriptors()
{
ScriptControlDescriptor descriptor = new ScriptControlDescriptor("Samples.SampleTextBox", this.ClientID);
descriptor.AddProperty("highlightCssClass", this.HighlightCSSClass);
descriptor.AddProperty("nohighlightCssClass", this.NoHighlightCSSClass);
return new ScriptDescriptor[] { descriptor };
}
在 OnPreRender 中取得 ScriptManager 並註冊控制項,在 Render 中註冊描述器:
protected override void OnPreRender(EventArgs e)
{
if (!this.DesignMode)
{
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);
}
為客戶端控制項註冊命名空間,並實作屬性與事件,重點是在 initialize 時用 $addHandlers 掛上 focus 與 blur 事件,分別切換高亮與非高亮樣式:
Type.registerNamespace('Samples');
Samples.SampleTextBox = function(element) {
Samples.SampleTextBox.initializeBase(this, [element]);
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;
},
_onFocus : function(e) {
if (this.get_element() && !this.get_element().disabled) {
this.get_element().className = this._highlightCSSClass;
}
},
_onBlur : function(e) {
if (this.get_element() && !this.get_element().disabled) {
this.get_element().className = this._nohighlightCSSClass;
}
}
}
Samples.SampleTextBox.registerClass('Samples.SampleTextBox', Sys.UI.Control);
if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();
在 Default.ASPx 頁面註冊命名空間與 TagPrefix,並放入 ScriptManager 與幾個 SampleTextBox 即可測試。只要設定 NoHighlightCssClass 與 HighlightCSSClass 兩個樣式,控制項在獲得/失去焦點時就會自動切換背景顏色。
<%@ Register Namespace="TextBoxExtender" TagPrefix="sample" %>
<ASP:ScriptManager ID="ScriptManager1" runat="server" />
<sample:SampleTextBox ID="TextBox1" runat="server" NoHighlightCssClass="LowLight" HighlightCSSClass="HighLight" />