GridView中,自动创建模板列后,如何为该模板列中的控件绑定数据?
 public class GridViewTemplate : ITemplate
    {
        private DataControlRowType templateType;
        private string columnName;
        private string count;        public GridViewTemplate(DataControlRowType type, string colname,string count11)
        {
            templateType = type;
            columnName = colname;
            count = count11;
        }
        //模板列
        public void InstantiateIn(System.Web.UI.Control container)
        {
            switch (templateType)
            {
                case DataControlRowType.Header:
                    Literal lc = new Literal();
                    lc.Text = columnName;
                    container.Controls.Add(lc);
                    break;
                case DataControlRowType.DataRow:
                    Label Lb_Id = new Label();
                    Lb_Id.Text = ????
                    container.Controls.Add(Lb_Id);
                    break;                default:
                    break;
            }
        }
    }上面是自动创建模板列的类,下面是调用:
TemplateField customField1 = new TemplateField();
        customField1.ShowHeader = true;
        customField1.HeaderTemplate = new GridViewTemplate(DataControlRowType.Header, "2222");        customField1.ItemTemplate = new GridViewTemplate(DataControlRowType.DataRow, "");
        GridView1.Columns.Add(customField1);
我想要的是在模板列中的label控件中绑定数据表的主键值,以前在前台可以直接<%#DataBinder.Eval(Container.DataItem,"Id")%>但现在不知道怎么绑定了

解决方案 »

  1.   

    <%# DataBinder.Eval(Container, "DataItem.字段名")%>
      

  2.   


            //绑定GridView
            this.GridView1.DataSource = cTable;
            this.GridView1.DataBind();
            //遍历GridView,查找模板列
            foreach (GridViewRow grRow in this.GridView1.Rows)
            {
                Button btn = (Button)grRow.Cells[0].FindControl("Button1");
                if (btn != null)
                {
                    btn.Text = grRow.Cells[1].Text;
                    //找到后赋值
                }
            }
      

  3.   


    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            Label lbl = (Label)e.Row.FindControl("Lb_Id");
            lbl.Text = "";
        }
      

  4.   

    但是这样根本找不到Lb_Id这个控件啊,因为那模板列是在后台自动创建的。