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;
}
}
}
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;
}
}
}
相关文章推荐
- 动态创建模板列并绑定数据(GridView,Repeater,DataGrid)
- 动态创建模板列并绑定数据(GridView,Repeater,DataGrid)
- 动态创建模板列并绑定数据(GridView,Repeater,DataGrid)
- 如何使用动态创建模板列并绑定数据(GridView,Repeater,DataGrid)
- 如何使用动态创建模板列并绑定数据(GridView,Repeater,DataGrid)
- 如何使用动态创建模板列并绑定数据(GridView,Repeater,DataGrid)
- VS2005 GridView动态绑定数据源(交叉表) 动态绑定列 动态生成模板列 自定义表头
- 动态绑定Gridview带模板列
- GridView动态添加模板列并自动绑定数据
- ASP.NET2.0中给GridView动态添加模板列并自动绑定数据
- GridView 动态添加绑定列和模板列
- GridView 动态添加绑定列和模板列
- GridView 动态创建列回发再绑定的解决!
- GridView动态添加模板列(数据绑定)
- GridView动态添加模板列(并为其添加事件和自动绑定数据)
- ASP.NET2.0中给GridView动态添加模板列并自动绑定数据
- GridView动态创建模板列templateField
- VS2005 GridView动态绑定数据源(交叉表) 动态绑定列 动态生成模板列 自定义表头
- asp.Net中Gridview动态创建模板列
- gridview中动态绑定通过模板列增加的textbox。