创建非继承的自定义控件
控件首先需要继承所有控件的基类:System.Web.UI.Control,并实现两个接口:Istate Manager(实现ViewState),IPostBackDataHandler(处理回发数据)。
TextBox
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
namespace Demo
{
/**//// <summary>
/// WebCustomControl1 的摘要说明。
/// </summary>
// 像前两个例子一样,先处理一下控件设计时属性。
[DefaultProperty("Text"),
Designer("Demo.DemoDesigner"),
ToolboxData("<{0}:DemoTextBox runat=server></{0}:DemoTextBox>")]
public class DemoTextBox : System.Web.UI.Control,IStateManager,IPostBackDataHandler
{
private StateBag _state;
private bool _marked;
//下面就是我们要实现的属性:Text
[Bindable(true),
Category("Appearance"),
DefaultValue("")]
public string Text
{
get
{
string _text = (string) ViewState["Text"];
return _text==null?"":_text;
}
set
{
string text = "";
text = value;
text = text.Replace("'","’");
ViewState["Text"] = text;
}
}
//为了能实现视图状态就必须实现IStateManager接口
object IStateManager.SaveViewState()
{
object _stateState = null;
if( _state != null )
_stateState = ((IStateManager)_state).SaveViewState();
if ( _stateState == null )
return null;
return _stateState;
}
void IStateManager.TrackViewState()
{
_marked = true;
if( _state != null )
((IStateManager)_state).TrackViewState();
}
void IStateManager.LoadViewState( object state )
{
if( state != null )
{
object _newState = (object)state;
((IStateManager)ViewState).LoadViewState( _newState );
}
}
bool IStateManager.IsTrackingViewState
{
get
{
return _marked;
}
}
internal new StateBag ViewState //注意,这里覆盖基类的ViewState属性
{
get
{
if( _state == null )
{
_state = new StateBag( true );
if( ((IStateManager)this).IsTrackingViewState )
((IStateManager)_state).TrackViewState();
}
return _state;
}
}
//下面把控件的表现输出到页面,其实System.Web.UI.WebControls.TextBox也是重新包装了Input而已。
protected override void Render(HtmlTextWriter output)
{
string strOutput = "<Input name=\""+this.ClientID+"\" type=\"text\" value=\""+this.Text+"\">";
output.Write(strOutput);
}
IPostBackDataHandler 成员#region IPostBackDataHandler 成员
public void RaisePostDataChangedEvent()
{
// TODO: 添加 DemoTextBox.RaisePostDataChangedEvent 实现
}
//下面的方法很重要,把回发的数据保存。
public bool LoadPostData(string postDataKey, System.Collections.Specialized.NameValueCollection postCollection)
{
// TODO: 添加 DemoTextBox.LoadPostData 实现
string presentValue = this.Text;
string postedValue = postCollection[postDataKey];
if (!presentValue.Equals(postedValue))//如果回发数据不等于原有数据
{
this.Text = postedValue;
return true;
}
return false;
}
#endregion
}
}
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
namespace Demo
{
/**//// <summary>
/// WebCustomControl1 的摘要说明。
/// </summary>
// 像前两个例子一样,先处理一下控件设计时属性。
[DefaultProperty("Text"),
Designer("Demo.DemoDesigner"),
ToolboxData("<{0}:DemoTextBox runat=server></{0}:DemoTextBox>")]
public class DemoTextBox : System.Web.UI.Control,IStateManager,IPostBackDataHandler
{
private StateBag _state;
private bool _marked;
//下面就是我们要实现的属性:Text
[Bindable(true),
Category("Appearance"),
DefaultValue("")]
public string Text
{
get
{
string _text = (string) ViewState["Text"];
return _text==null?"":_text;
}
set
{
string text = "";
text = value;
text = text.Replace("'","’");
ViewState["Text"] = text;
}
}
//为了能实现视图状态就必须实现IStateManager接口
object IStateManager.SaveViewState()
{
object _stateState = null;
if( _state != null )
_stateState = ((IStateManager)_state).SaveViewState();
if ( _stateState == null )
return null;
return _stateState;
}
void IStateManager.TrackViewState()
{
_marked = true;
if( _state != null )
((IStateManager)_state).TrackViewState();
}
void IStateManager.LoadViewState( object state )
{
if( state != null )
{
object _newState = (object)state;
((IStateManager)ViewState).LoadViewState( _newState );
}
}
bool IStateManager.IsTrackingViewState
{
get
{
return _marked;
}
}
internal new StateBag ViewState //注意,这里覆盖基类的ViewState属性
{
get
{
if( _state == null )
{
_state = new StateBag( true );
if( ((IStateManager)this).IsTrackingViewState )
((IStateManager)_state).TrackViewState();
}
return _state;
}
}
//下面把控件的表现输出到页面,其实System.Web.UI.WebControls.TextBox也是重新包装了Input而已。
protected override void Render(HtmlTextWriter output)
{
string strOutput = "<Input name=\""+this.ClientID+"\" type=\"text\" value=\""+this.Text+"\">";
output.Write(strOutput);
}
IPostBackDataHandler 成员#region IPostBackDataHandler 成员
public void RaisePostDataChangedEvent()
{
// TODO: 添加 DemoTextBox.RaisePostDataChangedEvent 实现
}
//下面的方法很重要,把回发的数据保存。
public bool LoadPostData(string postDataKey, System.Collections.Specialized.NameValueCollection postCollection)
{
// TODO: 添加 DemoTextBox.LoadPostData 实现
string presentValue = this.Text;
string postedValue = postCollection[postDataKey];
if (!presentValue.Equals(postedValue))//如果回发数据不等于原有数据
{
this.Text = postedValue;
return true;
}
return false;
}
#endregion
}
}