【问题标题】:Binding Repeater with Dictionary<string, Dictionary<int,[object]>使用 Dictionary<string, Dictionary<int,[object]> 绑定中继器
【发布时间】:2012-02-04 19:50:15
【问题描述】:

我是 .NET 的新手,所以我为此苦苦挣扎。我有一个带有中继器控件的内容页面。我有一个字典,它是Dictionary&lt;string, Dictionary&lt;int,[object]&gt;&gt;。我希望中继器控件内的控件值从对象属性中获取 - 候选名称,将是 object.CandName,候选电话将是 object.Phone 等。

我不确定如何将Eval 用于这种类型的字典。大多数示例都指向Eval("Value"),但它并没有为我提供正确的值。请帮忙!

<asp:Content ID="Content2" ContentPlaceHolderID="content" Runat="Server">
  <div id="rcontent"> 
    <table>
      <tr>
        <td>
          <asp:Label ID="lblerror" runat="server" Text="" Visible="true" CssClass="alert"></asp:Label>
        </td>
      </tr>
    </table>
    <div id ="rptdiv">
      <asp:Repeater ID="Repeater1" runat="server" EnableViewState="false">
        <ItemTemplate>
          <div id="Div3"> 
            <table class="GridViewStyleNoBorder" width=750px cellspacing="0" border="0" >
              <tr>
                <td class="PagerStyle" colspan="4"> 
                  <asp:Label ID="lblName" Runat="server"
Text='<%= Need the value of the [object].objectproperty from dictionary here %>' />
                </td>
              </tr>
            </table>
          </div>

这是我后面的Page_Load 代码-BLDecision 是我的业务层代码,它返回的字典和字典值是正确的。我在调试模式下检查了它们。

代码背后:

Dictionary(int, Dictionary(int, InterviewFeedback)) ;

CandIntDetails = new Dictionary(int, Dictionary(int, InterviewFeedback))();

BLDecision objBLDecision = new BLDecision();
int ReqCategoryID = 0;
if (Request.QueryString["ReqCategoryID"] != null)
    ReqCategoryID = int.Parse(Request.QueryString["ReqCategoryID"].ToString());
CandIntDetails = objBLDecision.GetCandidatesforReqCategory(ReqCategoryID);

Repeater1.DataSource = CandIntDetails;
Repeater1.DataBind();

我应该从代码隐藏中使用,我可以在 aspx 页面中不做Eval('&lt;% ....%&gt;') 吗?

提前感谢您的帮助。

【问题讨论】:

  • 您应该更准确地描述数据的格式。有时您说它是Dictionary&lt;string, Dictionary&lt;int, InterviewFeedback&gt;&gt;,而在另一种情况下,您暗示您正在使用Dictionary&lt;int, Dictionary&lt;int, InterviewFeedback&gt;&gt;。字典到底包含什么,你希望你的输出是什么样的?您想要按字典键对事物进行分组,还是想要访问特定键的所有值?
  • 它是: Dictionary(int, Dictionary(int, InterviewFeedback)) ;我想访问和显示特定键的所有值 - 每一行都需要访问和显示所有值。

标签: asp.net dictionary repeater eval bind


【解决方案1】:

你不能只用一个中继器来做到这一点。由于容器内有容器,因此中继器内需要中继器:

<asp:Repeater ID="Repeater1" runat="server" EnableViewState="false">
<ItemTemplate>
<div id="Div3"> 
    <table class="GridViewStyleNoBorder" width=750px cellspacing="0" border="0" >
    <asp:Repeater ID="Repeater2" runat="server" DataSource='<%# Eval("Value")' >
      <ItemTemplate>
    <tr>
    <td class="PagerStyle" colspan="4"> 
         <asp:Label ID="lblName" Runat="server"
        Text='<%# Eval("Name") %>' />
    </td>
    </tr>

      </ItemTemplate>
    </asp:Repeater>


    </table>
    </div>
</ItemTemplate>
</asp:Repeater>

【讨论】:

  • 感谢您的建议。我添加了另一个中继器,但无法直接访问该值,因为字典的值是一个业务对象。所以我在后面的代码上做了这个。我能够获得显示的记录值。但它只显示一条记录。它总共有 3 条记录,但显示第一行,但不显示其余记录。我应该把它循环到某个地方,我错过了什么吗?
【解决方案2】:

如果CandIntDetailsDictionary&lt;int, Dictionary&lt;int, InterviewFeedback&gt;&gt;,则需要从中提取要用作转发器数据源的特定集合。原因是因为您要渲染InterviewFeedback 对象的集合,而CandIntDetails不是CandIntDetails 可能看起来像这样:

{
    46: {
        0: [InterviewFeedback],
        1: [InterviewFeedback],
        2: [InterviewFeedback]
    }
}

从您的帖子中不清楚内部或外部字典的键是什么,所以这是推测性的。如果外部键是类别 ID(不知道为什么 GetCandidatesforReqCategory 会返回类似的内容),并且如果您不关心内部字典键,则可以像这样提取数据源:

Repeater1.DataSource = CandIntDetails[ReqCategoryID].Values;

这将使您的数据源成为InterviewFeedback 对象的直接集合。一旦这是您的数据源,您就可以Eval 访问InterviewFeedback 对象的属性。

【讨论】:

  • 感谢您的建议。现在可以使用了。
猜你喜欢
  • 2012-03-01
  • 1970-01-01
  • 2018-01-25
  • 1970-01-01
  • 2011-07-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-22
相关资源
最近更新 更多