验证,只须标记控件的属性为 ValidationProperty
[ValidationProperty("Name")]
public class NameControl : Control
{}
数据绑定:
支持数据绑定的控件,首先对外有DataSource属性,还有DataTextField属性,在加上DataObject(用来表示绑定的对象)
重写基类的 OnDataBinding方法。这里还用到反射的一些方法。
代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.Data;
using System.ComponentModel;
using System.Reflection;
namespace OwnControl
{
public class DataBindControl:Control
{
//用来存放数据的ArrayList,这个也配合ViewState使用
private ArrayList cacheList = new ArrayList();
//要显示的字段
private string dataTextField;
[Category("自定义属性"),DefaultValue(""),Description("要显示的数据源的字段")]
public string DataTextField
{
set { this.dataTextField=value; }
get { return this.dataTextField; }
}
//数据源
private object dataSource;
[Category("自定义属性"),Description("数据源")]
public object DataSource
{
set{ this.dataSource=value; }
get{ return this.dataSource; }
}
private object dataObject;
[Category("自定义属性"),Description("绑定数据对象")]
public object DataObject
{
get { return dataObject; }
set { dataObject = value; }
}
private IEnumerable GetResloverDataSource(object ds)
{
if (ds is IEnumerable)
return (IEnumerable)ds;
else if (ds is DataTable)
return (IEnumerable)(((DataTable)ds).DefaultView);
else if (ds is DataSet)
return (IEnumerable)(((DataSet)ds).Tables[0].DefaultView);
else if (ds is IList)
return (IEnumerable)ds;
else return null;
}
/// <summary>
/// 从已转化为IEnumerable的数据源的一行中取出需要的DataTextField规定的数据
/// </summary>
/// <param name="item">已转化为IEnumerable的数据源的一行数据</param>
/// <returns>取得的数据的string形式</returns>
private string getDataItem(object item)
{
if (item is IDataRecord)
return ((IDataRecord)item)[this.dataTextField].ToString();
else if (item is DataRowView)
return ((DataRowView)item)[this.dataTextField].ToString();
else if (item.GetType().ToString() == dataObject.GetType().ToString())
return GetObjetFiledValue(item, this.dataTextField);
else return item.ToString();
}
private string GetObjetFiledValue(object obj, string fieldName)
{
Type dataType = obj.GetType();
PropertyInfo bindField = dataType.GetProperty(fieldName);
object bindvalue = bindField.GetValue(obj,null);
return bindvalue.ToString();
}
/// <summary>
/// 处理DataBinding事件,把数据源中需要的数据加到一个ArrayList中。
/// </summary>
/// <param name="e">EventArgs</param>
protected override void OnDataBinding(EventArgs e)
{
base.OnDataBinding(e);
if (this.dataSource != null)
{
//如果Count为0,则表示是第一次加载
if (this.cacheList.Count == 0)
{
IEnumerable source = this.GetResloverDataSource(this.dataSource);
IEnumerator item = source.GetEnumerator();
while (item.MoveNext())
{
//保存数据
this.cacheList.Add(this.getDataItem(item.Current));
}
}
}
}
/// <summary>
/// 呈现,用遍历来循环输出ArrayList中的数据
/// </summary>
/// <param name="writer">HtmlTextWriter</param>
protected override void Render(HtmlTextWriter writer)
{
foreach (string s in this.cacheList)
{
writer.RenderBeginTag(HtmlTextWriterTag.Div);
writer.Write(s);
writer.RenderEndTag();
}
}
/// <summary>
/// 保存自页回发到服务器后发生的任何服务器控件视图状态更改。
/// </summary>
/// <returns>返回服务器控件的当前视图状态。</returns>
protected override object SaveViewState()
{
object[] vState = new object[3];
vState[0]=base.SaveViewState();
vState[1]=this.cacheList;
vState[2] = this.dataObject;
return vState;
}
/// <summary>
///从 SaveViewState 方法保存的上一个页请求还原视图状态信息。
/// </summary>
/// <param name="savedState">表示要还原的控件状态的 Object</param>
protected override void LoadViewState(object savedState)
{
if (savedState != null)
{
object[] vState = (object[])savedState;
if (vState[0] != null)
base.LoadViewState(vState[0]);
if (vState[1] != null)
this.cacheList = (ArrayList)vState[1];
if (vState[2] != null)
this.dataObject = vState[2];
}
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.Data;
using System.ComponentModel;
using System.Reflection;
namespace OwnControl
{
public class DataBindControl:Control
{
//用来存放数据的ArrayList,这个也配合ViewState使用
private ArrayList cacheList = new ArrayList();
//要显示的字段
private string dataTextField;
[Category("自定义属性"),DefaultValue(""),Description("要显示的数据源的字段")]
public string DataTextField
{
set { this.dataTextField=value; }
get { return this.dataTextField; }
}
//数据源
private object dataSource;
[Category("自定义属性"),Description("数据源")]
public object DataSource
{
set{ this.dataSource=value; }
get{ return this.dataSource; }
}
private object dataObject;
[Category("自定义属性"),Description("绑定数据对象")]
public object DataObject
{
get { return dataObject; }
set { dataObject = value; }
}
private IEnumerable GetResloverDataSource(object ds)
{
if (ds is IEnumerable)
return (IEnumerable)ds;
else if (ds is DataTable)
return (IEnumerable)(((DataTable)ds).DefaultView);
else if (ds is DataSet)
return (IEnumerable)(((DataSet)ds).Tables[0].DefaultView);
else if (ds is IList)
return (IEnumerable)ds;
else return null;
}
/// <summary>
/// 从已转化为IEnumerable的数据源的一行中取出需要的DataTextField规定的数据
/// </summary>
/// <param name="item">已转化为IEnumerable的数据源的一行数据</param>
/// <returns>取得的数据的string形式</returns>
private string getDataItem(object item)
{
if (item is IDataRecord)
return ((IDataRecord)item)[this.dataTextField].ToString();
else if (item is DataRowView)
return ((DataRowView)item)[this.dataTextField].ToString();
else if (item.GetType().ToString() == dataObject.GetType().ToString())
return GetObjetFiledValue(item, this.dataTextField);
else return item.ToString();
}
private string GetObjetFiledValue(object obj, string fieldName)
{
Type dataType = obj.GetType();
PropertyInfo bindField = dataType.GetProperty(fieldName);
object bindvalue = bindField.GetValue(obj,null);
return bindvalue.ToString();
}
/// <summary>
/// 处理DataBinding事件,把数据源中需要的数据加到一个ArrayList中。
/// </summary>
/// <param name="e">EventArgs</param>
protected override void OnDataBinding(EventArgs e)
{
base.OnDataBinding(e);
if (this.dataSource != null)
{
//如果Count为0,则表示是第一次加载
if (this.cacheList.Count == 0)
{
IEnumerable source = this.GetResloverDataSource(this.dataSource);
IEnumerator item = source.GetEnumerator();
while (item.MoveNext())
{
//保存数据
this.cacheList.Add(this.getDataItem(item.Current));
}
}
}
}
/// <summary>
/// 呈现,用遍历来循环输出ArrayList中的数据
/// </summary>
/// <param name="writer">HtmlTextWriter</param>
protected override void Render(HtmlTextWriter writer)
{
foreach (string s in this.cacheList)
{
writer.RenderBeginTag(HtmlTextWriterTag.Div);
writer.Write(s);
writer.RenderEndTag();
}
}
/// <summary>
/// 保存自页回发到服务器后发生的任何服务器控件视图状态更改。
/// </summary>
/// <returns>返回服务器控件的当前视图状态。</returns>
protected override object SaveViewState()
{
object[] vState = new object[3];
vState[0]=base.SaveViewState();
vState[1]=this.cacheList;
vState[2] = this.dataObject;
return vState;
}
/// <summary>
///从 SaveViewState 方法保存的上一个页请求还原视图状态信息。
/// </summary>
/// <param name="savedState">表示要还原的控件状态的 Object</param>
protected override void LoadViewState(object savedState)
{
if (savedState != null)
{
object[] vState = (object[])savedState;
if (vState[0] != null)
base.LoadViewState(vState[0]);
if (vState[1] != null)
this.cacheList = (ArrayList)vState[1];
if (vState[2] != null)
this.dataObject = vState[2];
}
}
}
}
界面使用的方法:
代码
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="TestDataBindControlWebForm.aspx.cs" Inherits="TestCodeBehind.TestDataBindControlWebForm" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@ Register Namespace="OwnControl" TagPrefix="useown" Assembly="OwnControl" %>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<useown:DataBindControl ID="control" runat="server" DataTextField="Name" EnableViewState="true"></useown:DataBindControl>
<asp:Button ID="btnOk" runat="server" Text="确定" onclick="btnOk_Click" />
</div>
</form>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@ Register Namespace="OwnControl" TagPrefix="useown" Assembly="OwnControl" %>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<useown:DataBindControl ID="control" runat="server" DataTextField="Name" EnableViewState="true"></useown:DataBindControl>
<asp:Button ID="btnOk" runat="server" Text="确定" onclick="btnOk_Click" />
</div>
</form>
</body>
</html>
代码
后值的代码:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
namespace TestCodeBehind
{
public partial class TestDataBindControlWebForm : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnOk_Click(object sender, EventArgs e)
{
List<Student> studentList=new List<Student>();
for(int i=0;i<10;i++)
{
Student stu=new Student(i.ToString()+"name",i);
studentList.Add(stu);
}
this.control.DataObject = new Student("",0);
this.control.DataSource = studentList;
this.control.DataBind();
}
}
[Serializable]
public class Student
{
public Student(string name,int age)
{
this.name = name;
this.age = age;
}
private string name;
public string Name
{
get { return name; }
set { name = value; }
}
private int age;
public int Age
{
get { return age; }
set { age = value; }
}
}
}
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
namespace TestCodeBehind
{
public partial class TestDataBindControlWebForm : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnOk_Click(object sender, EventArgs e)
{
List<Student> studentList=new List<Student>();
for(int i=0;i<10;i++)
{
Student stu=new Student(i.ToString()+"name",i);
studentList.Add(stu);
}
this.control.DataObject = new Student("",0);
this.control.DataSource = studentList;
this.control.DataBind();
}
}
[Serializable]
public class Student
{
public Student(string name,int age)
{
this.name = name;
this.age = age;
}
private string name;
public string Name
{
get { return name; }
set { name = value; }
}
private int age;
public int Age
{
get { return age; }
set { age = value; }
}
}
}