【问题标题】:Binding to a List<string> using DataBinder.Eval使用 DataBinder.Eval 绑定到 List<string>
【发布时间】:2010-09-05 21:11:47
【问题描述】:

我有以下对象:

CrossTabDataObject
{
   string RowName{get;set;};
   int RowId{get;set;}
   List<string> CellContents = new List <string>();
   //Constructor..... etc.

}

我在 asp.net 3.5 中使用 GridView 构建动态交叉表网格

我希望为动态列 1 绑定 CellContents[0],为动态列 2 绑定 CellContents[1](动态列 0 是 CrossTabDataObject 中的 RowName 字段)等。我正在使用:

object boundValueObj = null;
Control ctrl = (Control)sender;
IDataItemContainer dataItemContainer = (IDataItemContainer)ctrl.NamingContainer;
boundValueObj = DataBinder.Eval(dataItemContainer.DataItem, strSelectedID);

当我在交叉表的每个单元格中创建下拉列表模板时,此代码位于 gridview 的 InstantiateIn 函数中。

我有根据正在创建的列设置 strSelectedID 的代码(未显示)。

当动态列 [0] 的 strSelectedID 等于“RowName”时,DataBinder.Eval 函数可以正常工作并按预期设置 boundValueObj。当 strSelectedID 设置为“CellContents[n]”时,问题就出现了,其中 n 是列索引。

DataBinder.Eval 仅适用于对象的属性和字段。我该如何解决这个问题?

谢谢,

富有。

【问题讨论】:

    标签: c# asp.net databinder


    【解决方案1】:

    好的——我犯了一个愚蠢的错误!

    变化:

    CrossTabDataObject
    {
       string RowName{get;set;};
       int RowId{get;set;}
       List<string> CellContents = new List <string>();
       //Constructor..... etc.
    
    }
    

    到:

    CrossTabDataObject
    {
       string RowName{get;set;};
       int RowId{get;set;}
       List<string> CellContents{get;set;}
       //Constructor
       public CrossTabDataObject()
       {
         CellContents = new List<string>();
       }
    
    }
    

    让一切变得不同。换句话说,我将 CellContents 设置为该类的属性。

    丰富。

    【讨论】:

      猜你喜欢
      • 2013-12-07
      • 1970-01-01
      • 2013-09-05
      • 2012-12-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-31
      相关资源
      最近更新 更多