2 自定义控件之IPostBackDataHandler
2010-11-18 11:08
459 查看
1 IPostBackDataHandler 获取自动加载回发数据而必须实现的方法。
![](http://images.cnblogs.com/cnblogs_com/hbb0b0/WindowsLiveWriter/2IPostBackDataHandler_9C9B/image_thumb.png)
定义 ASP.NET 服务器控件为自动加载回发数据而必须实现的方法。
2 示例
2.1 CustomerTextBoxError:此事列演示由于无法获取http 协议post过来的name,value 键值对数据而无法保存控件的状态
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Text;
using System.Web;
using System.Web.UI;
namespace AspnetEssential.CustomerControl
{
/// <summary>
/// 自定义CustomerTextBoxError控件
/// </summary>
/// <remarks>
/// 不是现IPostBackDataHandler,没法获取自动post的数据
/// </remarks>
public class CustomerTextBoxError:Control
{
/// <summary>
/// 文本改变事件
/// </summary>
/// <summary>
/// 文本
/// </summary>
public string Text
{
get
{
return ViewState["_Text"] as string;
}
set
{
ViewState["_Text"] = value;
}
}
/// <summary>
/// 呈现
/// </summary>
/// <param name="writer"></param>
protected override void Render(HtmlTextWriter writer)
{
//write html tag <input type='text' name='ClientID' value='Text'></>
writer.AddAttribute(HtmlTextWriterAttribute.Name , this.ClientID);
writer.AddAttribute(HtmlTextWriterAttribute.Value, Text);
writer.RenderBeginTag(HtmlTextWriterTag.Input);
writer.RenderEndTag();
}
}
}
2.2 CustomerTextBox:实现IPostBackDataHandler,从而获取post过来的数据,从而可以保留控件的状态,以及引发文本改变事件。
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Text;
using System.Web;
using System.Web.UI;
namespace AspnetEssential.CustomerControl
{
/// <summary>
/// 自定义TextBox控件
/// </summary>
/// <remarks>
/// 学习IPostBackDataHandler的使用
/// </remarks>
public class CustomerTextBox:Control,IPostBackDataHandler
{
/// <summary>
/// 文本改变事件
/// </summary>
public event EventHandler TextChanged;
/// <summary>
/// 文本
/// </summary>
public string Text
{
get
{
return ViewState["_Text"] as string;
}
set
{
ViewState["_Text"] = value;
}
}
#region IPostBackDataHandler 成员
public bool LoadPostData(string postDataKey, NameValueCollection postCollection)
{
string postDataValue=postCollection[postDataKey ];
if (postDataValue!=null && !postDataValue.Equals(Text))
{
Text = postDataValue;
return true ;
}
else
{
return false ;
}
}
public void RaisePostDataChangedEvent()
{
if(TextChanged!=null)
{
EventArgs e=new EventArgs ();
TextChanged(this, e);
}
}
/// <summary>
/// 呈现
/// </summary>
/// <param name="writer"></param>
protected override void Render(HtmlTextWriter writer)
{
//write html tag <input type='text' name='%ClientID%' value='%Text%'></>
writer.AddAttribute(HtmlTextWriterAttribute.Name , this.ClientID);
writer.AddAttribute(HtmlTextWriterAttribute.Value, Text);
writer.RenderBeginTag(HtmlTextWriterTag.Input);
writer.RenderEndTag();
}
#endregion
}
}
3 实现IPostBackDataHandler 控件
TextBox, HtmlInputText, CheckBox, HtmlSelect, DropDownList 等
![](http://images.cnblogs.com/cnblogs_com/hbb0b0/WindowsLiveWriter/2IPostBackDataHandler_9C9B/image_thumb.png)
定义 ASP.NET 服务器控件为自动加载回发数据而必须实现的方法。
名称 | 说明 | |
![]() | LoadPostData | 当由某个类实现时,它为 ASP.NET 服务器控件处理回发数据。 |
![]() | RaisePostDataChangedEvent | 当由类实现时,它用信号要求服务器控件对象通知 ASP.NET 应用程序该控件的状态已更改。 |
2 示例
2.1 CustomerTextBoxError:此事列演示由于无法获取http 协议post过来的name,value 键值对数据而无法保存控件的状态
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Text;
using System.Web;
using System.Web.UI;
namespace AspnetEssential.CustomerControl
{
/// <summary>
/// 自定义CustomerTextBoxError控件
/// </summary>
/// <remarks>
/// 不是现IPostBackDataHandler,没法获取自动post的数据
/// </remarks>
public class CustomerTextBoxError:Control
{
/// <summary>
/// 文本改变事件
/// </summary>
/// <summary>
/// 文本
/// </summary>
public string Text
{
get
{
return ViewState["_Text"] as string;
}
set
{
ViewState["_Text"] = value;
}
}
/// <summary>
/// 呈现
/// </summary>
/// <param name="writer"></param>
protected override void Render(HtmlTextWriter writer)
{
//write html tag <input type='text' name='ClientID' value='Text'></>
writer.AddAttribute(HtmlTextWriterAttribute.Name , this.ClientID);
writer.AddAttribute(HtmlTextWriterAttribute.Value, Text);
writer.RenderBeginTag(HtmlTextWriterTag.Input);
writer.RenderEndTag();
}
}
}
2.2 CustomerTextBox:实现IPostBackDataHandler,从而获取post过来的数据,从而可以保留控件的状态,以及引发文本改变事件。
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Text;
using System.Web;
using System.Web.UI;
namespace AspnetEssential.CustomerControl
{
/// <summary>
/// 自定义TextBox控件
/// </summary>
/// <remarks>
/// 学习IPostBackDataHandler的使用
/// </remarks>
public class CustomerTextBox:Control,IPostBackDataHandler
{
/// <summary>
/// 文本改变事件
/// </summary>
public event EventHandler TextChanged;
/// <summary>
/// 文本
/// </summary>
public string Text
{
get
{
return ViewState["_Text"] as string;
}
set
{
ViewState["_Text"] = value;
}
}
#region IPostBackDataHandler 成员
public bool LoadPostData(string postDataKey, NameValueCollection postCollection)
{
string postDataValue=postCollection[postDataKey ];
if (postDataValue!=null && !postDataValue.Equals(Text))
{
Text = postDataValue;
return true ;
}
else
{
return false ;
}
}
public void RaisePostDataChangedEvent()
{
if(TextChanged!=null)
{
EventArgs e=new EventArgs ();
TextChanged(this, e);
}
}
/// <summary>
/// 呈现
/// </summary>
/// <param name="writer"></param>
protected override void Render(HtmlTextWriter writer)
{
//write html tag <input type='text' name='%ClientID%' value='%Text%'></>
writer.AddAttribute(HtmlTextWriterAttribute.Name , this.ClientID);
writer.AddAttribute(HtmlTextWriterAttribute.Value, Text);
writer.RenderBeginTag(HtmlTextWriterTag.Input);
writer.RenderEndTag();
}
#endregion
}
}
3 实现IPostBackDataHandler 控件
TextBox, HtmlInputText, CheckBox, HtmlSelect, DropDownList 等
相关文章推荐
- 自定义控件(10)数据回传IPostBackDataHandler
- web组件设计,利用接口(IPostBackDataHandler)产生数据回传的问题
- 服务器控件数据回发实现IPostBackDataHandler需注意的
- IPostBackDataHandler实现CollapablePanel控件的状态保持
- IPostBackEventHandler 與 IPostBackDataHandler
- asp.net组件(7)数据回发 IPostBackDataHandler
- 组件开发之IPostBackDataHandler和OnLoad
- web组件设计,利用接口(IPostBackDataHandler)产生数据回传的问题
- IPostBackDataHandler
- Web Control 开发系列(二) 深入解析Page的PostBack过程和IPostBackDataHandler
- page在处理post请求时需要处理数据的加载IPostBackDataHandler和事件的触发IPostBackEventHandler.
- [Joe 原创] Web Control 开发系列(二) 深入解析Page的PostBack过程和IPostBackDataHandler
- 【转】Web Control 开发系列(二) 深入解析Page的PostBack过程和IPostBackDataHandler
- Web Control 开发系列(二) 深入解析Page的PostBack过程和IPostBackDataHandler
- [Joe 原创] Web Control 开发系列(二) 深入解析Page的PostBack过程和IPostBackDataHandler
- Web Control 开发系列(二) 深入解析Page的PostBack过程和IPostBackDataHandler
- 自定义控件(12)回传事件IPostBackEventHandler
- 给自定义控件(Web Control)添加事件的几种方法。前两种方法可以不实现IPostBackEventHandler
- 给自定义控件(Web Control)添加事件的几种方法。前两种方法可以不实现IPostBackEventHandler
- jquery+ajax 的 data参数设置的内容,如何在HttpHandler的HttpContext中取得(包括post方式和get方式)