准備技術
常規開發技術
內容
數據回傳就是說客戶端將數據回交給服務器
一
此方法的原型
參數postDataKey就是回傳控件的UniqueID
要注意的一點是
二
RaisePostDataChangeEvent方法一般是在LoadPostData返回True的是否才去執行
三
下面就來模仿寫一個TextBox控件
首先我們實現控件的一些屬性
DefaultValue(
Category(
Description(
public string Text
{
get
{
return this
}
set
{
this
}
}
然後我們把控件呈現
{
get
{
return HtmlTextWriterTag
}
}
protected override void AddAttributesToRender(HtmlTextWriter writer)
{
base
if (Page != null)
{
Page
}
writer
writer
if (!Enabled)
{
writer
}
writer
}
protected override void AddParsedSubObject(object obj)
{
if (!(obj is Literal))
return;
base
}
這裡要注意的是一定要注冊其前台
{
string postData = valueCollection[postKey]
string strText = this
if (strText
return false;
this
return true;
}
public void RaisePostDataChangedEvent()
{
EventHandler handler = (EventHandler)Events[_objEvent];
if (handler != null)
{
handler(this
}
}
其實原理很簡單
using System;
using System
using System
using System
using System
using System
using System
namespace HenllyeeControls
{
[DefaultProperty(
ParseChildren(true
DefaultEvent(
ControlBuilder(typeof(HenllyeeTextBuider))]
public class HenllyeeText:WebControl
{
Properties#region Properties
[PersistenceMode(PersistenceMode
DefaultValue(
Category(
Description(
public string Text
{
get
{
return this
}
set
{
this
}
}
#endregion
Events#region Events
private static object _objEvent;
[Category(
public event EventHandler TextChanged
{
add
{
this
}
remove
{
this
}
}
#endregion
Render#region Render
protected override HtmlTextWriterTag TagKey
{
get
{
return HtmlTextWriterTag
}
}
protected override void AddAttributesToRender(HtmlTextWriter writer)
{
base
if (Page != null)
{
Page
}
writer
writer
if (!Enabled)
{
writer
}
writer
}
protected override void AddParsedSubObject(object obj)
{
if (!(obj is Literal))
return;
base
}
#endregion
IPostBackData#region IPostBackData
public bool LoadPostData(string postKey
{
string postData = valueCollection[postKey]
string strText = this
if (strText
return false;
this
return true;
}
public void RaisePostDataChangedEvent()
{
EventHandler handler = (EventHandler)Events[_objEvent];
if (handler != null)
{
handler(this
}
}
#endregion
}
public class HenllyeeTextBuider : ControlBuilder
{
public override bool AllowWhitespaceLiterals()
{
return false;
}
public override bool HtmlDecodeLiterals()
{
return true;
}
}
}
我們新建一個頁面中
From:http://tw.wingwit.com/Article/program/net/201311/11451.html