1.WebResource
获取一个字符串,该字符串包含由 WebResourceAttribute 类所引用的资源的名称。
[assembly: WebResource(
"
Aerolite.Resources.Js.SearchTextBox.js
"
,
"
application/x-javascript
"
)]
[assembly: WebResource( " Aerolite.Resources.Js.MenuCommon.js " , " application/x-javascript " )]
[assembly: WebResource( " Aerolite.Resources.Js.MenuCommon.js " , " application/x-javascript " )]
2.TagPrefix
定义在网页中用于标识自定义控件的标记前缀。
[assembly: TagPrefix(
"
Aerolite.Web.WebControls
"
,
"
arl
"
)]
3.ToolboxData
指定当从MS等工具的工具箱拖动自定义控件时为它生成的默认标记
[ToolboxData(
"
<{0}:SearchTextBox runat=server />
"
)]
4.ParseChildren
定义可在开发asp.net服务器控件时使用的元数据属性
[ParseChildren(
true
)]
5.Bindable
指示成员是否用于绑定
6.Themeable
获取一个值,该值指示当前控件或控件的成员是否受 Web 应用程序中定义的主题和控件外观影响。
7.Browsable
获取一个值,该值指示此对象是否可浏览。是否显示在“属性”窗口中。
[Bindable(
true
), Themeable(
false
), Browsable(
true
)]
8.Category
指定当属性或者事件显示在一个设置为"按分类顺序"模式的System.Windows.Forms.PropertyGrid控件中时,用于给属性或事件分组的类别的名称。
9.DefaultValue
属性的默认值
10.Localizable
属性是否应本地化.
[Category(
"
Data
"
)]
[DefaultValue( " App_Code " )]
[Localizable( true )]
[DefaultValue( " App_Code " )]
[Localizable( true )]
上面是一些属性的设置,下面附封装google suggest控件的代码。
using
System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
// 引用两个js文件,该js文件是生成该控件的
[assembly: WebResource( " Aerolite.Resources.Js.SearchTextBox.js " , " application/x-javascript " )]
[assembly: WebResource( " Aerolite.Resources.Js.MenuCommon.js " , " application/x-javascript " )]
// 定义在网页中的标记前缀为arl
[assembly: TagPrefix( " Aerolite.Web.WebControls " , " arl " )]
namespace Aerolite.Web.WebControls
{
/**//// <summary>
/// 一个可以异步传输已进行右模糊搜索的文本输入框
/// </summary>
// 指定当从工具箱拖动时为它生成的默认标记
[ToolboxData( "<{0}:SearchTextBox runat=server />" )]
//定义开发服务器控件时使用的元数据属性
[ParseChildren( true )]
//定义一个属性
[PersistChildren( false )]
public class SearchTextBox : System.Web.UI.WebControls.TextBox, System.Web.UI.ICallbackEventHandler
{
/**//// <summary>
/// 获取或设置提供搜索结果的类的全名
/// </summary>
[Bindable( true ), Themeable( false ), Browsable( true )]
[Category( "Data" )]
[DefaultValue( "" )]
[Localizable( true )]
public string DataSourceFullName
{
get
{
return (ViewState["DataSourceFullName"] == null) ? "" : (string) ViewState["DataSourceFullName"];
}
set
{
ViewState["DataSourceFullName"] = value;
}
}
/**//// <summary>
/// 获取或设置提供搜索结果的类所在程序集的名称
/// </summary>
[Bindable( true ), Themeable( false ), Browsable( true )]
[Category( "Data" )]
[DefaultValue( "App_Code" )]
[Localizable( true )]
public string DataSourceAssembly
{
get
{
return (ViewState["DataSourceAssembly"] == null) ? "App_Code" : (string) ViewState["DataSourceAssembly"];
}
set
{
ViewState["DataSourceAssembly"] = value;
}
}
/**//// <summary>
/// 获取或设置提供搜索结果的方法的名称。
/// </summary>
[Bindable( true ), Themeable( false ), Browsable( true )]
[Category( "Data" )]
[DefaultValue( "" )]
[Localizable( true )]
public string GetDataMethod
{
get
{
return (ViewState["GetDataMethod"] == null) ? "" : (string) ViewState["GetDataMethod"];
}
set
{
ViewState["GetDataMethod"] = value;
}
}
private object _DataSource;
[Bindable( true ), Themeable( false ), Browsable( false )]
public object DataSource
{
get
{
if ( _DataSource == null )
{
OnDataSourceCreating();
}
return _DataSource;
}
set
{
_DataSource = value;
Properties.Resources;
}
}
public delegate void DataSoucreEventHandler( SearchTextBox sender );
[Bindable( false ), Browsable( false )]
public event DataSoucreEventHandler DataSourceCreating;
protected virtual void OnDataSourceCreating()
{
if ( DataSourceCreating != null )
{
DataSourceCreating( this );
}
else
{
_DataSource = System.Type.GetType( DataSourceFullName + "," + DataSourceAssembly ).InvokeMember( "", System.Reflection.BindingFlags.CreateInstance, null, null, null );
}
}
/**//// <summary>
/// 定义用于获取搜索上下文菜单项的方法
/// </summary>
/// <param name="searchKey">搜索框中输入的部分关键字</param>
/// <returns>构成上下文菜单的项</returns>
public delegate string[] GetSearchResultMethod( string searchKey );
private GetSearchResultMethod _getResult;
/**//// <summary>
/// 设置用于获取搜索上下文菜单项的方法
/// </summary>
[Bindable( false ), Themeable( false ), Browsable( false )]
public GetSearchResultMethod GetResultMethod
{
set
{
if ( value == null )
throw new ArgumentNullException( "value" );
_getResult = value;
}
}
private string[] _GetResult( string searchKey )
{
if ( _getResult == null )
return GetResult( searchKey );
else
return _getResult( searchKey );
}
/**//// <summary>
/// 获取搜索上下文菜单项
/// </summary>
/// <param name="searchKey">搜索框中输入的部分关键字</param>
/// <returns>构成上下文菜单的项</returns>
protected virtual string[] GetResult( string searchKey )
{
return FormatResult( DataSource.GetType().InvokeMember( GetDataMethod, System.Reflection.BindingFlags.InvokeMethod, null, DataSource, new object[] { searchKey } ) ).Split( ',' );
}
protected override void OnPreRender( EventArgs e )
{
base.OnPreRender( e );
Page.ClientScript.RegisterClientScriptResource( typeof( SearchTextBox ), "Aerolite.Resources.Js.MenuCommon.js" );
Page.ClientScript.RegisterClientScriptResource( typeof( SearchTextBox ), "Aerolite.Resources.Js.SearchTextBox.js" );
}
protected override void Render( HtmlTextWriter writer )
{
base.Render( writer );
writer.Write( "<script language=/"javascript/">/n" );
writer.Write( " var " + this.ClientID + "Menu=new searchTextBox();/n" );
writer.Write( " " + this.ClientID + "Menu.parentObj=document.getElementById(/"" + this.ClientID + "/");/n" );
writer.Write( " " + this.ClientID + "Menu.bindingEvent();/n" );
writer.Write( " " + this.ClientID + "Menu.callBack=function(){/n" + Page.ClientScript.GetCallbackEventReference( this, "this.parentObj.value", "this.getCallBackResult", null ) + ";/n};/n" );
writer.Write( @"</script>" );
}
protected virtual string FormatResult( object result )
{
string temp = "";
if ( result is string )
{
return result.ToString();
}
else if ( result is System.Collections.IEnumerable )
{
foreach ( object i in (System.Collections.IEnumerable) result )
{
temp += "," + i.ToString();
}
if ( temp != "" ) temp = temp.Substring( 1 );
}
else if ( result is System.Data.DataTable )
{
foreach ( System.Data.DataRow i in ((System.Data.DataTable) result).Rows )
{
temp += "," + i[0].ToString();
}
if ( temp != "" ) temp = temp.Substring( 1 );
}
return temp;
}
//异步回调
ICallbackEventHandler 成员#region ICallbackEventHandler 成员
string ICallbackEventHandler.GetCallbackResult()
{
return string.Join( ",", _searchResult );
}
private string[] _searchResult;
void ICallbackEventHandler.RaiseCallbackEvent( string eventArgument )
{
_searchResult = _GetResult( eventArgument );
}
#endregion
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
// 引用两个js文件,该js文件是生成该控件的
[assembly: WebResource( " Aerolite.Resources.Js.SearchTextBox.js " , " application/x-javascript " )]
[assembly: WebResource( " Aerolite.Resources.Js.MenuCommon.js " , " application/x-javascript " )]
// 定义在网页中的标记前缀为arl
[assembly: TagPrefix( " Aerolite.Web.WebControls " , " arl " )]
namespace Aerolite.Web.WebControls
{
/**//// <summary>
/// 一个可以异步传输已进行右模糊搜索的文本输入框
/// </summary>
// 指定当从工具箱拖动时为它生成的默认标记
[ToolboxData( "<{0}:SearchTextBox runat=server />" )]
//定义开发服务器控件时使用的元数据属性
[ParseChildren( true )]
//定义一个属性
[PersistChildren( false )]
public class SearchTextBox : System.Web.UI.WebControls.TextBox, System.Web.UI.ICallbackEventHandler
{
/**//// <summary>
/// 获取或设置提供搜索结果的类的全名
/// </summary>
[Bindable( true ), Themeable( false ), Browsable( true )]
[Category( "Data" )]
[DefaultValue( "" )]
[Localizable( true )]
public string DataSourceFullName
{
get
{
return (ViewState["DataSourceFullName"] == null) ? "" : (string) ViewState["DataSourceFullName"];
}
set
{
ViewState["DataSourceFullName"] = value;
}
}
/**//// <summary>
/// 获取或设置提供搜索结果的类所在程序集的名称
/// </summary>
[Bindable( true ), Themeable( false ), Browsable( true )]
[Category( "Data" )]
[DefaultValue( "App_Code" )]
[Localizable( true )]
public string DataSourceAssembly
{
get
{
return (ViewState["DataSourceAssembly"] == null) ? "App_Code" : (string) ViewState["DataSourceAssembly"];
}
set
{
ViewState["DataSourceAssembly"] = value;
}
}
/**//// <summary>
/// 获取或设置提供搜索结果的方法的名称。
/// </summary>
[Bindable( true ), Themeable( false ), Browsable( true )]
[Category( "Data" )]
[DefaultValue( "" )]
[Localizable( true )]
public string GetDataMethod
{
get
{
return (ViewState["GetDataMethod"] == null) ? "" : (string) ViewState["GetDataMethod"];
}
set
{
ViewState["GetDataMethod"] = value;
}
}
private object _DataSource;
[Bindable( true ), Themeable( false ), Browsable( false )]
public object DataSource
{
get
{
if ( _DataSource == null )
{
OnDataSourceCreating();
}
return _DataSource;
}
set
{
_DataSource = value;
Properties.Resources;
}
}
public delegate void DataSoucreEventHandler( SearchTextBox sender );
[Bindable( false ), Browsable( false )]
public event DataSoucreEventHandler DataSourceCreating;
protected virtual void OnDataSourceCreating()
{
if ( DataSourceCreating != null )
{
DataSourceCreating( this );
}
else
{
_DataSource = System.Type.GetType( DataSourceFullName + "," + DataSourceAssembly ).InvokeMember( "", System.Reflection.BindingFlags.CreateInstance, null, null, null );
}
}
/**//// <summary>
/// 定义用于获取搜索上下文菜单项的方法
/// </summary>
/// <param name="searchKey">搜索框中输入的部分关键字</param>
/// <returns>构成上下文菜单的项</returns>
public delegate string[] GetSearchResultMethod( string searchKey );
private GetSearchResultMethod _getResult;
/**//// <summary>
/// 设置用于获取搜索上下文菜单项的方法
/// </summary>
[Bindable( false ), Themeable( false ), Browsable( false )]
public GetSearchResultMethod GetResultMethod
{
set
{
if ( value == null )
throw new ArgumentNullException( "value" );
_getResult = value;
}
}
private string[] _GetResult( string searchKey )
{
if ( _getResult == null )
return GetResult( searchKey );
else
return _getResult( searchKey );
}
/**//// <summary>
/// 获取搜索上下文菜单项
/// </summary>
/// <param name="searchKey">搜索框中输入的部分关键字</param>
/// <returns>构成上下文菜单的项</returns>
protected virtual string[] GetResult( string searchKey )
{
return FormatResult( DataSource.GetType().InvokeMember( GetDataMethod, System.Reflection.BindingFlags.InvokeMethod, null, DataSource, new object[] { searchKey } ) ).Split( ',' );
}
protected override void OnPreRender( EventArgs e )
{
base.OnPreRender( e );
Page.ClientScript.RegisterClientScriptResource( typeof( SearchTextBox ), "Aerolite.Resources.Js.MenuCommon.js" );
Page.ClientScript.RegisterClientScriptResource( typeof( SearchTextBox ), "Aerolite.Resources.Js.SearchTextBox.js" );
}
protected override void Render( HtmlTextWriter writer )
{
base.Render( writer );
writer.Write( "<script language=/"javascript/">/n" );
writer.Write( " var " + this.ClientID + "Menu=new searchTextBox();/n" );
writer.Write( " " + this.ClientID + "Menu.parentObj=document.getElementById(/"" + this.ClientID + "/");/n" );
writer.Write( " " + this.ClientID + "Menu.bindingEvent();/n" );
writer.Write( " " + this.ClientID + "Menu.callBack=function(){/n" + Page.ClientScript.GetCallbackEventReference( this, "this.parentObj.value", "this.getCallBackResult", null ) + ";/n};/n" );
writer.Write( @"</script>" );
}
protected virtual string FormatResult( object result )
{
string temp = "";
if ( result is string )
{
return result.ToString();
}
else if ( result is System.Collections.IEnumerable )
{
foreach ( object i in (System.Collections.IEnumerable) result )
{
temp += "," + i.ToString();
}
if ( temp != "" ) temp = temp.Substring( 1 );
}
else if ( result is System.Data.DataTable )
{
foreach ( System.Data.DataRow i in ((System.Data.DataTable) result).Rows )
{
temp += "," + i[0].ToString();
}
if ( temp != "" ) temp = temp.Substring( 1 );
}
return temp;
}
//异步回调
ICallbackEventHandler 成员#region ICallbackEventHandler 成员
string ICallbackEventHandler.GetCallbackResult()
{
return string.Join( ",", _searchResult );
}
private string[] _searchResult;
void ICallbackEventHandler.RaiseCallbackEvent( string eventArgument )
{
_searchResult = _GetResult( eventArgument );
}
#endregion
}
}
来自 http://www.cnblogs.com/ustbwuyi/archive/2006/08/14/476671.html