ASP.NET 3.5核心编程学习笔记(55):自定义扩展程序控件的创建
ASP.NET并沒有包含對擴展程序的具體實現。然而,它定義了供所有自定義擴展程序和ACT中所有擴展程序使用的基類ExtenderControl。我們可通過該類創建自己的擴展程序。但并不建議這樣做,因為利用ACT庫中的擴展程序更簡便易行。
下面的代碼給出了“焦點擴展程序”控件的源代碼,這個簡單的擴展程序能為目標控件添加高亮行為,以便在該控件獲得焦點時更改其外觀:
using AjaxControlToolkit;...
namespace Core35
{
[TargetControlType(typeof(Control)]
[ClientScriptResource("Core35.FocusBehavior", "focusBehavior.js")]
public string HighlightCssClass
{
get { return GetPropertyValue("HighlightCssClass", ""); }
set { SetPropertyValue("HighlightCssClass", value); }
}
[ExtenderControlProperty]
public string NoHighlightCssClass
{
get { return GetPropertyValue("NoHighlightCssClass", ""); }
set { SetPropertyValue("NoHighlightCssClass", value); }
}
}
TargetControlType特性用于指示該行為針對的控件類型。ClientScriptResource特性指示注入客戶端頁面的腳本類及相關源文件的名稱。基類ExtenderControlBase定義在ACT庫中。
我們在托管代碼中所能做的是,定義自定義的屬性集合。每個屬性必須帶有ExtenderControlProperty特性。屬性本身并不直接負責值的存儲,而僅限于通過基類GetPropertyValue和SetPropertyValue方法進行獲得或設置。這些存儲方法負責具體的存儲工作。
AJAX擴展程序控件的核心部分是其JavaScript代碼。焦點擴展程序所需的JavaScript代碼如下:
Type.registerNamespace('Core35');Core35.FocusBehavior = function(element)
{
Core35.FocusBehavior.initializeBase(this, [element]);
this._highlightCssClass = null;
this._nohighlightCssClass = null;
}
Core35.FocusBehavior.prototype =
{
initialize : function()
{
Core35.FocusBehavior.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 : function()
{
$clearHandlers(this.get_element());
Core35.FocusBehavior.callBaseMethod(this, 'dispose');
},
_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)
{
tis.get_element().className = this._nohighlightCssClass;
}
},
get_highlightCssClass : function()
{
return this._highlightCssClass;
},
set_highlightCssClass : function(value)
{
if(this._highlightCssClass != value)
{
this._highlightCssClass = value;
this.raisePropertyChanged('highlightCssClass');
}
},
get_nohighlightCssClass : function()
{
return this._nohighlightCssClass;
},
set_nohighlightCssClass : function(value)
{
if(this._nohighlightCssClass != value)
{
this._nohighlightCssClass = value;
this.raisePropertyChanged('nohighlightCssClass');
}
}
}
//Optinal descriptor for JSON serialization
Core35.FocusBehavior.descriptor = {
properties : [ {name: 'highlightCssClass', type : String},
{name: 'nohighlightCssClass', type : String}]
}
//Register the class as a type tha inherits from Sys.UI.Control
Core35.FocusBehavior.registerClass('Core35.FocusBehavior', Sys.UI.Behavior);
在測試頁面中,我們只需注冊ACT程序集和包含這個焦點擴展程序的程序集,然后添加以下代碼:
<asp:TextBox ID="TextBox1" runat="server" EnableTheming="false" /><asp:FocusExtender ID="FocusExtender1" runat="server"
TargetControlID="TextBox1"
NoHighlightCssClass="LowLightTextBox"
HighlightCssClass="HighLight" />
注意,為確保通過擴展程序應用以CSS樣式優先于主題設置的樣式,請將主題關閉。
轉載于:https://www.cnblogs.com/free722/archive/2011/05/03/2035565.html
總結
以上是生活随笔為你收集整理的ASP.NET 3.5核心编程学习笔记(55):自定义扩展程序控件的创建的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 在服务器端生成Excel文件然后从服务器
- 下一篇: UIEdgeInsets