将 Repeater 与 Dictionary 绑定

发布于 2025-01-03 06:24:37 字数 1998 浏览 1 评论 0 原文

我是 .NET 的新手,所以我正在为此苦苦挣扎。我有一个内容页面,带有转发器控件。我有一个字典,它是一个 Dictionary>。我希望转发器控件内的控件值从对象属性中获取 - 候选名称为 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('<% ....%>') 吗?

预先感谢您的帮助。

I am new to .NET, so I'm struggling with this. I have a content page, with a repeater control. I have a Dictionary, which is a Dictionary<string, Dictionary<int,[object]>>. I want the value of the controls inside the repeater control to get it from the object attributes - Candidate Name, would be object.CandName, candidate phone would be object.Phone etc.

I am not sure how to use Eval for this type of Dictionary. Most of the examples point to Eval("Value"), but it is not giving the correct value for me. Kindly help!

<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>

This is my Page_Load code behind - BLDecision is my business layer code, which returns the dictionary and dictionary values are correct. I checked them in debug mode.

Code Behind:

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();

Should I use from codebehind, can I not do Eval('<% ....%>') in the aspx page?

Thanks in advance for your help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

那小子欠揍 2025-01-10 06:24:37

仅用一台中继器是无法做到这一点的。由于容器内有容器,因此中继器内需要一个中继器:

<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>

You cannot do it with only one repeater. Since you have a container inside a container, you need a Repeater inside a repeater:

<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>
故事未完 2025-01-10 06:24:37

如果 CandIntDetailsDictionary>,您需要从该集合中提取要用作数据源的特定集合。中继器。原因是您想要渲染 InterviewFeedback 对象的集合,而 CandIntDetails不是CandIntDetails 可能看起来像这样:

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

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

Repeater1.DataSource = CandIntDetails[ReqCategoryID].Values;

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

If CandIntDetails is a Dictionary<int, Dictionary<int, InterviewFeedback>>, you need to extract from that the specific collection you want to use as the data source of your repeater. The reason why is because you want to render a collection of InterviewFeedback objects, which CandIntDetails is not. CandIntDetails probably looks something like this:

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

It's not clear from your post what the keys are for the inner or outer dictionaries, so this is speculative. If the outer key is the the category ID (not sure why GetCandidatesforReqCategory would return something like that), and if you don't care about the inner dictionary keys, you can extract your data source like this:

Repeater1.DataSource = CandIntDetails[ReqCategoryID].Values;

That will make your data source a straight collection of InterviewFeedback objects. Once that's your data source, you can Eval to access the properties of the InterviewFeedback objects.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文