日期:2014-05-19  浏览次数:20403 次

动态创建列.无法显示,求大家帮我看看?
1.在CS中
protected   void   BindGrid()  
{
DataGrid   dg   =   new   DataGrid();
TemplateColumn   col   =   new   TemplateColumn();
col1.HeaderText   =   "金额 ";
dg.Columns.Add(col1);
...
da.Fill(ds);
dg.DataSource   =   ds;
dg.DataBind();
Page.Controls[1].Controls.Add(dg);
2.在CS中
private   void   dg_ItemDataBound(object   sender,   System.Web.UI.WebControls.DataGridItemEventArgs   e)
{
TextBox   tb1   =   new   TextBox();
tb1.Text   =   "11 ";
e.Item.Cells[0].Controls.Add(tb1);
}
          为什么只能显示列名 "金额 ",而无法显示textbox.请大家救我啊.

------解决方案--------------------
用法错了

public class DynTemplate : ITemplate
{
public void InstantiateIn(Control container)
{
TextBox tb1 = new TextBox();
tb1.ID = "TextBox1 ";
container.Controls.Add(tb1);
}
}

TemplateColumn col = new TemplateColumn();
col.ItemTemplate = new DynTemplate();
...

private void dg_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
TextBox tb = (TextBox)e.Item.FindControl( "TextBox1 ");
tb.Text = "aaa ";
}
------解决方案--------------------
这其实很明显你的两个模板列中一共有4个TextBox,而且你给它们设置的ID有冲突。