准备技术
常规开发技术
了解控件开发的呈现属性状态简单原理
内容
数据回传就是说客户端将数据回交给服务器然后模型将会将处理后的数据回传到表单中去如果开发的控件需要回传的话就必须要去继承于IPostBackDataHandler接口 继承IPostBackDataHandler之后我们一般需要去重写两个方法
LoadPostData()方法返回一个bool值表示该控件的值是否发生改变(需要我们去判断)
RaisPostDataChangeEvent()此方法是在LoadPostData方法返回true的时候去执行的
一LoadPostData方法
此方法的原型LoadPostData(string postDataKeySystemCollectionsSpecializedNameValueCollection postCollection)
参数postDataKey就是回传控件的UniqueID在客户端表现为name属性postCollection就是回传的数据值的结合所以我们可以通过postCollection[postDataKey]来获取本控制回传的值所以我们就可以来比较原来控件的值跟回传的值从而来确定LoadPostData方法返回true还是false
要注意的一点是我们回传时需要name属性所以如果我们定义的控件要支持数据回传的话就必须要去添加属性name
二RaisePostDataChangeEvent()方法
RaisePostDataChangeEvent方法一般是在LoadPostData返回True的是否才去执行一般我们在此方法中去触发一些事件如TextChanged
三实例
下面就来模仿写一个TextBox控件当然现在只是去实现数据回传功能
首先我们实现控件的一些属性现在我们就让其有Text属性 [PersistenceMode(PersistenceModeEncodedInnerDefaultProperty)
DefaultValue()
Category(Behavior)
Description(文本内容)]
public string Text
{
get
{
return thisViewState[Text] == null ? StringEmpty : (string)thisViewState[Text];
}
set
{
thisViewState[Text] = value;
}
}
然后我们把控件呈现控件是继承于WebControl的 protected override HtmlTextWriterTag TagKey
{
get
{
return HtmlTextWriterTagInput;
}
}
protected override void AddAttributesToRender(HtmlTextWriter writer)
{
baseAddAttributesToRender(writer);
if (Page != null)
{
PageVerifyRenderingInServerForm(this);
}
writerAddAttribute(HtmlTextWriterAttributeType text);
writerAddAttribute(HtmlTextWriterAttributeName thisUniqueID);
if (!Enabled)
{
writerAddAttribute(HtmlTextWriterAttributeDisabled disabled);
}
writerAddAttribute(HtmlTextWriterAttributeValue thisText);
}
protected override void AddParsedSubObject(object obj)
{
if (!(obj is Literal))
return;
baseAddParsedSubObject(obj);
}
这里要注意的是一定要注册其前台name属性最后我们看如何让控件支持数据回传 public bool LoadPostData(string postKey SystemCollectionsSpecializedNameValueCollection valueCollection)
{
string postData = valueCollection[postKey]ToString();
string strText = thisText;
if (strTextEquals(postData StringComparisonOrdinal))
return false;
thisText = postData;
return true;
}
public void RaisePostDataChangedEvent()
{
EventHandler handler = (EventHandler)Events[_objEvent];
if (handler != null)
{
handler(this EventArgsEmpty);
}
}
其实原理很简单就是判断回传过来的数据是否跟原来相等不相等返回true则执行RaisePostDataChangeEvent方法控件的全部代码如下
using System;
using SystemCollectionsGeneric;
using SystemComponentModel;
using SystemText;
using SystemWeb;
using SystemWebUI;
using SystemWebUIWebControls;
namespace HenllyeeControls
{
[DefaultProperty(Text)
ParseChildren(trueText)
DefaultEvent(TextChanged)
ControlBuilder(typeof(HenllyeeTextBuider))]
public class HenllyeeText:WebControlIPostBackDataHandler
{
Properties#region Properties
[PersistenceMode(PersistenceModeEncodedInnerDefaultProperty)
DefaultValue()
Category(Behavior)
Description(文本内容)]
public string Text
{
get
{
return thisViewState[Text] == null ? StringEmpty : (string)thisViewState[Text];
}
set
{
thisViewState[Text] = value;
}
}
#endregion
Events#region Events
private static object _objEvent;
[Category(Action)]
public event EventHandler TextChanged
{
add
{
thisEventsAddHandler(_objEvent value);
}
remove
{
thisEventsRemoveHandler(_objEvent value);
}
}
#endregion
Render#region Render
protected override HtmlTextWriterTag TagKey
{
get
{
return HtmlTextWriterTagInput;
}
}
protected override void AddAttributesToRender(HtmlTextWriter writer)
{
baseAddAttributesToRender(writer);
if (Page != null)
{
PageVerifyRenderingInServerForm(this);
}
writerAddAttribute(HtmlTextWriterAttributeType text);
writerAddAttribute(HtmlTextWriterAttributeName thisUniqueID);
if (!Enabled)
{
writerAddAttribute(HtmlTextWriterAttributeDisabled disabled);
}
writerAddAttribute(HtmlTextWriterAttributeValue thisText);
}
protected override void AddParsedSubObject(object obj)
{
if (!(obj is Literal))
return;
baseAddParsedSubObject(obj);
}
#endregion
IPostBackData#region IPostBackData
public bool LoadPostData(string postKey SystemCollectionsSpecializedNameValueCollection valueCollection)
{
string postData = valueCollection[postKey]ToString();
string strText = thisText;
if (strTextEquals(postData StringComparisonOrdinal))
return false;
thisText = postData;
return true;
}
public void RaisePostDataChangedEvent()
{
EventHandler handler = (EventHandler)Events[_objEvent];
if (handler != null)
{
handler(this EventArgsEmpty);
}
}
#endregion
}
public class HenllyeeTextBuider : ControlBuilder
{
public override bool AllowWhitespaceLiterals()
{
return false;
}
public override bool HtmlDecodeLiterals()
{
return true;
}
}
}
我们新建一个页面中添加刚才的控件跟一个Button可是发现能支持数据回传了