首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

GridView动态创建模板列,在绑定时出现异常

2012-01-12 
GridView动态创建模板列,在绑定时出现错误!//模板列publicclassLabelItemTemplate:ITemplate{publicLabelI

GridView动态创建模板列,在绑定时出现错误!
//模板列
public   class   LabelItemTemplate   :   ITemplate
{
    public   LabelItemTemplate(string   ID,   string   ColumnName)
    {
        id   =   ID;
        columnName   =   ColumnName;
    }

    public   void   InstantiateIn(Control   Container)
    {
        Label   lbl   =   new   Label();
        lbl.ID   =   id;
        lbl.DataBinding   +=   new   EventHandler(this.OnDataBinding);
        Container.Controls.Add(lbl);
      }

    public   void   OnDataBinding(object   sender,   EventArgs   e)
    {
        Label   lbl   =   (Label)sender;
        GridViewRow   container   =   (GridViewRow)lbl.NamingContainer;
        ((DataRowView)container.DataItem)[columnName].ToString();
      }
}

//创建模板列
TemplateField   tmp   =   new   TemplateField();
tmp.ItemTemplate   =   new   LabelItemTemplate( "1 ", "Field1 ");
GridView.Columns.Add(tmp);

//生成数据
DataTable   dt   =   new   DataTable();
for(int   i=0;   i <20;   i++)
{
    DataRow   row   =   dt.NewRow();
    分别设置列的指....
    dt.Rows.Add(row);
}

//绑定数据
GridView.DataSource   =   dt;
GridView.DataBind();

//运行到GridView.DataBind();就粗错:提示
提示ArgumentNullException:   值不能为空。参数名:   propName
System.Web.UI.DataBinder.GetPropertyValue(Object   container,   String   propName)
System.Web.UI.WebControls.GridView.CreateChildControls(IEnumerable   dataSource,   Boolean   dataBinding)
....
....

//如果不生成行,就不会报错!
//起初以为问题出在模板列的绑定上,就把
//((DataRowView)container.DataItem)[columnName].ToString();
//这句注释掉,结果还是报同样的错误.
//不知道是什么原因.

请大家帮忙!


[解决办法]
// 以下代码测试通过, !!! 部分是我添加的代码

<%@ Page Language= "C# " %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN " "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">

<script runat= "server ">

//模板列
public class LabelItemTemplate : ITemplate
{
private string id, columnName;
public LabelItemTemplate(string ID, string ColumnName)
{
id = ID;
columnName = ColumnName;
}

public void InstantiateIn(Control Container)
{
Label lbl = new Label();
lbl.ID = id;
lbl.DataBinding += new EventHandler(this.OnDataBinding);
Container.Controls.Add(lbl);
}

public void OnDataBinding(object sender, EventArgs e)
{
Label lbl = (Label)sender;
GridViewRow container = (GridViewRow)lbl.NamingContainer;
// ((DataRowView)container.DataItem)[columnName].ToString();
lbl.Text = ((DataRowView)container.DataItem)[columnName].ToString(); // !!!为 lbl 赋值
}


}

protected void Page_Load(object sender, EventArgs e)
{
//创建模板列
TemplateField tmp = new TemplateField();
tmp.ItemTemplate = new LabelItemTemplate( "1 ", "Field1 ");
GridView1.Columns.Add(tmp);

//生成数据
DataTable dt = new DataTable();
dt.Columns.Add( "Field1 ", typeof(int)); // !!!为 lbl 赋值
for (int i = 0; i < 20; i++) {
DataRow row = dt.NewRow();
//分别设置列的指....
row[0] = i;// !!!
dt.Rows.Add(row);
}

//绑定数据
GridView1.DataSource = dt;
GridView1.DataBind();
}
</script>

<html xmlns= "http://www.w3.org/1999/xhtml " >
<head runat= "server ">
<title> Untitled Page </title>
</head>
<body>
<form id= "form1 " runat= "server ">
<div>
<asp:GridView ID= "GridView1 " runat= "server " AutoGenerateColumns= "false ">
</asp:GridView>
</div>
</form>
</body>
</html>

热点排行