我需要给GridView动态添加一个模板列,该模板列中包含一个LinkButton,LinkButton的text绑定HOSPITAL
急啊,在线等~~~

解决方案 »

  1.   

    1.创建实现System.Web.UI.ITemplate 接口的新类.
        public class MyTemplate : ITemplate
        {
            private string proName;
            public MyTemplate()
            {
                //
                // TODO: 在此处添加构造函数逻辑
                //
            }        public string ProName//要绑定的数据源字段名称
            {
                set { proName = value; }
                get { return proName; }
            }        public void InstantiateIn(Control container)//关键实现这个方法
            {  
                HtmlImage hi = new HtmlImage();
                hi.Src = "";
                hi.Alt = "";
                hi.DataBinding += new EventHandler(hi_DataBinding);//创建数据绑定事件
                container.Controls.Add(hi);
            }        void hi_DataBinding(object sender, EventArgs e)
            {
                HtmlImage hi = (HtmlImage)sender;
                GridViewRow container = (GridViewRow)hi.NamingContainer;
                //关键位置
                //使用DataBinder.Eval绑定数据
                //ProName,MyTemplate的属性.在创建MyTemplate实例时,为此属性赋值(数据源字段)
                hi.Attributes.Add("onclick", "alert('" + DataBinder.Eval(container.DataItem, ProName).ToString() + "');");
            }
        }
     2.*.aspx页面后台cs代码
            if (!this.IsPostBack)
            {
                DataSet ds = null;
                BLL.model_task bll = new BLL.model_task();
                ds = bll.GetList(string.Empty);            TemplateField tf = new TemplateField();
                tf.HeaderText = "自定义模板列";
                MyTemplate mt = new MyTemplate();
                mt.ProName = "m_task_name";//数据源字段
                tf.ItemTemplate = mt;
                this.GridView1.Columns.Add(tf);
                this.GridView1.DataSource = ds;
                this.GridView1.DataBind();
            }
      

  2.   

    参考下,网上很多
    http://hi.baidu.com/sharenet/blog/item/df14b51c9a74078e86d6b65f.html
      

  3.   

    http://topic.csdn.net/u/20071113/23/A85BB167-DE36-4775-8030-4692B8E23C67.html
    http://www.cnblogs.com/feb9903/archive/2007/04/13/712462.html
    http://www.cnblogs.com/lmarsy/archive/2006/11/18/564634.html
    http://www.pin5i.com/showtopic-7134.html
    http://dotnet.aspx.cc/article/897cb049-ccff-4fe7-b990-df974493a0b1/read.aspx