public interface IPostBackDataHandler{ public bool LoadPostData ( string postDataKey, NameValueCollection postCollection ); public void RaisePostDataChangedEvent ();}
public virtual bool LoadPostData(string postDataKey,NameValueCollection postData)
{
string presentValue = Text;
//旧数据
string postedValue = postData[postDataKey];//新数据
//检查新旧数据
if(presentValue.Equals(postedValue) || presentValue == null) {
Text = postedValue;
return true;
}
return false;
}
public virtual void RaisePostDataChangedEvent()
{
OnTextChanged(EventArgs.Empty);
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebControlLibrary{
[DefaultProperty("Text")]
[ToolboxData("<{0}:WebCustomControl runat=server></{0}:WebCustomControl>")]
public class WebCustomControl : WebControl, IPostBackDataHandler {
// 实现Text属性
[Bindable(true)]
[Category("Appearance")]
[DefaultValue("")]
[Localizable(true)]
public string Text {
get {
string s = (String)ViewState["Text"];
return ((s == null) ? String.Empty : s);
}
set {
ViewState["Text"] = value;
}
}
//重写控件呈现方法RenderContents
protected override void RenderContents(HtmlTextWriter output) {
output.AddAttribute(HtmlTextWriterAttribute.Type, "text");
output.AddAttribute(HtmlTextWriterAttribute.Value, Text);
output.AddAttribute(HtmlTextWriterAttribute.Name, this.UniqueID);
output.RenderBeginTag(HtmlTextWriterTag.Input);
output.RenderEndTag();
}
//定义事件对象EventTextChanged
private static readonly object EventTextChanged = new object();
#region 实现IPostBackDataHandler 成员
bool IPostBackDataHandler.LoadPostData(string postDataKey, System.Collections.Specialized.NameValueCollection postCollection) {
//比较初始数据presentValue和回传数据postedValue
string postedValue = postCollection[postDataKey];
string presentValue = Text;
if (presentValue == null || postedValue != presentValue) {
Text = postedValue;
return true;
}
return false;
}
void IPostBackDataHandler.RaisePostDataChangedEvent() {
OnTextChanged(EventArgs.Empty);
}
#endregion // 实现事件处理程序OnTextChanged
private void OnTextChanged(EventArgs eventArgs) {
EventHandler textChangedHandler = (EventHandler)Events[EventTextChanged];
if (textChangedHandler != null) {
textChangedHandler(this, eventArgs);
}
}
// 为TextChanged实现事件属性结构
public event EventHandler TextChanged {
add {
Events.AddHandler(EventTextChanged, value);
}
remove {
Events.RemoveHandler(EventTextChanged, value);
}
}
}
}
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Register TagPrefix="wcl" Assembly="WebControlLibrary" Namespace="WebControlLibrary" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<script runat="server">
void demo1_TextChanged(object sender, EventArgs e) {
label1.Text = "您在文本框中输入的是 " + demo1.Text;
}
</script>
<html xmlns="">
<head runat="server">
<title>处理回传数据</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<wcl:WebCustomControl ID="demo1" runat="server" OnTextChanged="demo1_TextChanged" />
<asp:Button ID="button1" runat="server" Text="提交" />
<br />
<asp:Label ID="label1" runat="server" Font-Size="small">
</asp:Label>
</div>
</form>
</body>
</html>