您的位置:首页 > 其它

GridView动态创建模板并绑定

2009-09-25 16:52 661 查看
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Reflection;
using System.Data;

namespace ThsEnv.Data.Common
{
/// <summary>
/// 类 名 称:DataGridTemplate
/// 内容摘要:通用模板类
/// </summary>
public class DataGridTemplate : ITemplate
{
// 数据列名
private string fieldName;

// 控件值属性
private PropertyInfo valueProperty;

// 控件
private Control oCtrl;

/**/
/// <summary>
/// 构造方法
/// </summary>
/// <param name="oControl">源控件</param>
/// <param name="ValueName">控件值属性的名称</param>
/// <param name="strFieldName">数据列名</param>
public DataGridTemplate(Control oControl, string ValueName, string strFieldName)
{
Type oType = oControl.GetType();
//PropertyInfo oPropertyInfo = oType.GetProperty(ValueName);
valueProperty = oType.GetProperty(ValueName);
oCtrl = oControl;
fieldName = ValueName;
}

public void Ddp_DataBinding(object sender, EventArgs e)
{
DropDownList ddp = (DropDownList)sender;
GridViewRow container = (GridViewRow)ddp.NamingContainer;
//关键位置
//使用DataBinder.Eval绑定数据
//ProName,MyTemplate的属性.在创建MyTemplate实例时,为此属性赋值(数据源字段)
ddp.SelectedValue = DataBinder.Eval(container.DataItem, fieldName).ToString();

}

public void TextBox_DataBinding(object sender, EventArgs e)
{
TextBox textbox = (TextBox)sender;
GridViewRow container = (GridViewRow)textbox.NamingContainer;
//关键位置
//使用DataBinder.Eval绑定数据
//ProName,MyTemplate的属性.在创建MyTemplate实例时,为此属性赋值(数据源字段)
textbox.Text = DataBinder.Eval(container.DataItem, fieldName).ToString();
//((DataRowView)container.DataItem)fieldName.ToString();
}

/**/
/// <summary>
/// 实现ITemplate的InstantiateIn方法
/// </summary>
/// <param name="container">控件容器</param>
public void InstantiateIn(System.Web.UI.Control container)
{
Control tmpCtrl = ControlClone(oCtrl);
switch (oCtrl.GetType().ToString())
{
case "System.Web.UI.WebControls.TextBox": tmpCtrl.DataBinding += new EventHandler(TextBox_DataBinding); break;
case "System.Web.UI.WebControls.DropDownList": tmpCtrl.DataBinding += new EventHandler(Ddp_DataBinding); break;
}
//TextBox textbox = new TextBox();
//textbox.ID = fieldName + "textbox";
//textbox.DataBinding += new EventHandler(TextBox_DataBinding);//创建数据绑定事件
//container.Controls.Add(textbox);
//Control tmpCtrl = ControlClone(oCtrl);
//tmpCtrl.DataBinding += new EventHandler(ControlTemplate_DataBinding);
container.Controls.Add(tmpCtrl);
}

/**/
/// <summary>
/// 绑定事件处理
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ControlTemplate_DataBinding(object sender, System.EventArgs e)
{
//Control tmpCtrl;
//tmpCtrl = (Control)sender;
//DataGridItem container = (DataGridItem)tmpCtrl.NamingContainer;
//try
//{
// string s = valueProperty.GetValue(tmpCtrl, null).ToString();
// s += DataBinder.Eval(container.DataItem, fieldName);
// valueProperty.SetValue(tmpCtrl, s, null);
//}
//catch
//{
//}
}

/**/
/// <summary>
/// 复制控件
/// </summary>
/// <param name="oSource">源控件</param>
/// <returns></returns>
private Control ControlClone(Control oSource)
{
//创建复制品
Control oTarget = (Control)System.Activator.CreateInstance(oSource.GetType());

//检查控件是否列表控件
ListControl oListControl = oSource as ListControl;
if (oListControl != null) // 是列表控件
{
foreach (ListItem oItem in oListControl.Items) // 复制列表项
{
((ListControl)oTarget).Items.Add(new ListItem(oItem.Text, oItem.Value));
}
}

//将源对象的属性复制给复制品
Type oType = oSource.GetType();
// 复制控件属性
foreach (PropertyInfo oProperty in oType.GetProperties(BindingFlags.Instance | BindingFlags.Public))
{
try
{
object oData = oProperty.GetValue(oSource, null);

oProperty.SetValue(oTarget, oData, null);
}
catch { }
}

return oTarget;
}
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: