【问题标题】:ASP.NET Strongly-typed DataControl (Repeater) with complex ItemType failing具有复杂 ItemType 失败的 ASP.NET 强类型 DataControl(中继器)
【发布时间】:2013-04-23 05:31:31
【问题描述】:

我有一个由三个嵌套转发器组成的结构来呈现一个选项卡,然后在这些选项卡下,一些按标题分组的项目。我一直在标准IEnumerable 类型上成功使用.NET 4.5 中的强类型数据控件,但这是我第一次使用Dictionary<T1,T2>。尽管如此,我还是看到另一个Stackoverflow question 通过将ItemType 声明为KeyValuePair<T1, T2> 成功地将Dictionary 用作DataSource

标记:

<asp:Repeater ID="rptTabs" runat="server"
    ItemType="System.Collections.Generic.KeyValuePair<System.String, System.Collections.Generic.Dictionary<System.String, System.Collections.Generic.List<Sitecore.Data.Items.Item>>>">
        <ItemTemplate>
           <%# Item.Key %>
           <%-- ... more repeaters under but they are commented out for now as it's the outer on that is failing --%>
        </ItemTemplate>
</asp:Repeater>

代码隐藏:

var myDatasource = new Dictionary<string, Dictionary<string, List<Item>>>();
// add some items
rptTabs.DataSource = myDatasource;
rptTabs.DataBind();

这是我得到的异常/堆栈跟踪:

似乎无法构建页面,因为它无法通过 string 名称解析 ItemType。问题是,我在 &lt;%# Item.Key #%&gt; 上获得了智能感知,因此智能感知引擎可以通过字符串名称解析类型,但运行时不能?

注意:我怀疑这是 Sitecore.Data.Items.Item 类型失败,因为我之前在 IEnumerable 中使用过它,这意味着 ItemType 是 Sitecore.Data.Items.Item

任何帮助将不胜感激!

【问题讨论】:

  • 我真的不想编写ItemDataBound 处理程序来简单地通过ID 查找内部控件并绑定它们的数据源。烦人!

标签: c# asp.net repeater


【解决方案1】:

据我所知,ItemType 属性不喜欢&lt; 尖括号&gt;。我读过的答案表明,用各自的方括号替换所有尖括号似乎有效。

替换以下内容:

  • &lt;[
  • &gt;]

所以你得到:

ItemType="System.Collections.Generic.KeyValuePair[System.String, System.Collections.Generic.Dictionary[System.String, System.Collections.Generic.List[Sitecore.Data.Items.Item]]]

我遇到了类似的错误。但是,就我而言,我发现我试图从HeaderTemplate 中访问&lt;%# Item.Property %&gt;

【讨论】:

  • 谢谢伙计。有机会我会看看,如果可行,我会回来标记为答案。
  • 嗯没有用.. 现在我在尝试访问项目时得到以下信息。 New Error CS0305:使用泛型类型 'System.Collections.Generic.KeyValuePair' 需要 2 个类型参数
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-18
相关资源
最近更新 更多