GridView动态创建模板并绑定

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;
        }
    }


}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值