2011-05-30 77 views
7

我需要知道如何在用户控件中嵌套中继器。事情的html方面很好,它是我需要帮助的绑定和代码。我只能找到使用sql数据源的例子,这并没有什么帮助。如何在asp.net中嵌套中继器

我的中继器看起来像这样:

<asp:Panel ID="pnlDiscipline" runat="server" CssClass=""> 
    <asp:Repeater ID="rptDiscipline" runat="server"> 
     <ItemTemplate> 
      <h4><%#Eval("Discipline")%></h4> 
      <ul> 
       <asp:Repeater ID="rptPrograms" runat="server"> 
        <ItemTemplate> 
         <li><asp:HyperLink runat="server" Text='<%#Eval("Name") %>' NavigateUrl='<%#Eval("Link") %>'></asp:HyperLink> <%#Eval ("Notation") %></li> 
        </ItemTemplate> 
       </asp:Repeater> 
      </ul> 
     </ItemTemplate> 
    </asp:Repeater> 

我需要做的是希望合理明确的 - H4学科应出现一次,全部属于该学科的条目列出如下,然后是下一个h4,然后是相应的列表,下一个h4等等。

数据源是在codebehind中创建的dataview,其中每一行都有'Name','Link','NOtation'和'Discipline'。我将dataview绑定到最外层的repeater,并且它的行为如预期的那样 - 列出每个条目的学科名称,但表示在内部Repeater没有数据

如何去使这项工作

编辑:?只是为了澄清,我在代码隐藏一个数据表中的每一行。在这个表中是一个项目,每个项目属于一个学科,我想使用外部中继器列出学科,内部列出每个学科下分组的项目,像这样:

<h4>DISCIPLINE 1</h4> 
    <ul> 
     <li>Item</li> 
     <li>Item</li> 
     <li>Item</li> 
    </ul> 
<h4>DISCIPLINE 2</h4> 
    <ul>   
     <li>Item</li>    
     <li>Item</li> 
    </ul> 
<h4>DISCIPLINE 3</h4> 
    <ul>   
     <li>Item</li>    
     <li>Item</li> 
    </ul> 

目前,结合数据表到外中继器给出了这样的(例如使用上面的数据):

<h4>DISCIPLINE 1</h4> 
    <h4>DISCIPLINE 1</h4> 
    <h4>DISCIPLINE 1</h4> 
    <h4>DISCIPLINE 2</h4> 
    <h4>DISCIPLINE 2</h4> 
    <h4>DISCIPLINE 3</h4> 
    <h4>DISCIPLINE 3</h4> 

我用OnItemDataBound在外中继器的建议,并且作为一个测试用例我能够访问数据:

protected void rptDiscipline_ItemDataBound(Object Sender, RepeaterItemEventArgs e) 
{ 
    DataRowView drView = (DataRowView) e.Item.DataItem; 
    string name = drView["Name"] as string; 
    string link = drView["Link"] as string; 
    string notation = drView["Notation"] as string; 
    Response.Write(name + link + notation + "<br />") 
} 

所以数据是存在的,这正是我希望看到的,我只是不能把它绑定到内转发。如果有更高性能的方法来实现这一点,我很乐意重做我的解决方案。

+0

你可以从这个线程http://stackoverflow.com/questions/1220715/creating-a-nested-repeater-control-dynamically/1220836#1220836 – 2011-05-30 03:59:07

+0

什么是你想绑定到内得到的想法中继?也许发布你绑定到外部中继器的类。 – EdmundYeung99 2011-05-30 04:00:14

+0

@Edmund - 编辑我的问题,希望能够澄清我的问题 – Nathan 2011-05-30 04:30:09

回答

7

在外部控制,使用ItemDataBound事件,像这样:

<asp:Repeater ID="rptDiscipline" runat="server" 
    OnItemDataBound="rptDiscipline_ItemDataBound"> 
... 

然后,在代码隐藏,处理rptDiscipline_ItemDataBound事件和手动绑定内中继器。对于每个重复的项目,中继器的ItemDataBound事件触发一次。所以,你会做这样的事情:

protected void rptDiscipline_ItemDataBound(Object Sender, RepeaterItemEventArgs e) 
{ 
    // To get your data item, cast e.Item.DataItem to 
    // whatever you're using for the data object; for example a DataRow. 

    // Get the inner repeater: 
    Repeater rptPrograms = (Repeater) e.Item.FindControl("rptPrograms"); 

    // Set the inner repeater's datasource to whatever it needs to be. 
    rptPrograms.DataSource = ... 
    rptPrograms.DataMember = ... 
    rptPrograms.DataBind(); 
} 

编辑:更新,以符合您的问题的更新。

您需要将外部中继器绑定到每个您希望中继器呈现的项目只有一条记录的数据源。这意味着数据源需要是一个集合/列表/数据表/等,只有其中的纪律。在你的情况下,我会建议从DataTable的内部集合中获得一个List<string>规则,并将外部中继器绑定到该集合。然后,内部中继器使用ItemDataBound事件绑定到DataTable中的数据子集。要获取子集,请通过DataView过滤DataTable。

这里是代码:

protected void Page_Load(object sender, EventItems e) 
{ 
    // get your data table 
    DataTable table = ... 

    if (!IsPostBack) 
    { 
     // get a distinct list of disciplines 
     List<string> disciplines = new List<string>(); 
     foreach (DataRow row in table) 
     { 
      string discipline = (string) row["Discipline"]; 
      if (!disciplines.Contains(discipline)) 
       disciplines.Add(discipline); 
     } 
     disciplines.Sort(); 

     // Bind the outer repeater 
     rptDiscipline.DataSource = disciplines; 
     rptDiscipline.DataBind(); 
    } 
} 

protected void rptDiscipline_ItemDataBound(Object Sender, RepeaterItemEventArgs e) 
{ 
    // To get your data item, cast e.Item.DataItem to 
    // whatever you're using for the data object 
    string discipline = (string) e.Item.DataItem; 

    // Get the inner repeater: 
    Repeater rptPrograms = (Repeater) e.Item.FindControl("rptPrograms"); 

    // Create a filtered view of the data that shows only 
    // the disciplines needed for this row 
    // table is the datatable that was originally bound to the outer repeater 
    DataView dv = new DataView(table); 
    dv.RowFilter = String.Format("Discipline = '{0}'", discipline); 

    // Set the inner repeater's datasource to whatever it needs to be. 
    rptPrograms.DataSource = dv; 
    rptPrograms.DataBind(); 
} 
+1

可以通过在内部中继器上设置DataSource来完成吗? – Nathan 2011-05-30 03:06:30

+0

嗨@charlie - 我明白你的意思,但不能得到它的工作。你能详细说明你的答案吗? – Nathan 2011-05-30 03:48:27

+0

@Nathan - 是的,可以看到@ lomaxx的回答 – Venemo 2011-05-30 09:41:23

4

如果你不想做的ItemDataBound事件,你也可以做它内嵌在你的页面通过结合如果孩子的父项的子属性属性是这样一个集合:

<asp:Repeater runat="server" ID="OuterRepeater" > 
    <ItemTemplate> 
     Outer Content: <%# DataBinder.Eval(Container.DataItem, "ParentProperty")%> 
     <asp:Repeater runat="server" ID="InnerRepeater" DataSource='<%# DataBinder.Eval(Container.DataItem, "ChildCollection")%>' > 
      <ItemTemplate> 
       <%# DataBinder.Eval(Container.DataItem, "ChildProperty")%> 
      </ItemTemplate> 
     </asp:Repeater> 
    </ItemTemplate> 
</asp:Repeater> 
1

首先,你需要两个列表,学科列表,然后是所有数据的列表。

数据将规则列表绑定到外部中继器。如果有6个学科,那么中继器应该重复6次。

<asp:Repeater ID="rptDiscipline" runat="server" OnItemDataBound="rptDiscipline_ItemDataBound"> 
     <ItemTemplate> 
      <h4><%# Eval("Discipline")%></h4> 
      <ul> 
       <asp:Repeater runat="server" ID="InnerRepeater" > 
        <ItemTemplate> 
         <li> 
          <asp:Label runat="server" ID="lbl" /> 
         </li> 
        </ItemTemplate> 
       </asp:Repeater> 
      </ul> 
     </ItemTemplate> 
    </asp:Repeater> 

protected void rptDiscipline_ItemDataBound(Object Sender, RepeaterItemEventArgs e) 
{  

    Repeater inner= (Repeater) e.Item.FindControl("InnerRepeater"); 

    //You want to bind all the data related to this discipline 
    DataRowView drView = (DataRowView) e.Item.DataItem; 
    string discipline= drView["Discipline"] as string; 

    //filter your total data with ones that match the discipline 

    inner.DataSource = //data bind the filtered list here 
    inner.DataBind(); 
} 
+0

我并不担心我是如何实现它 - 认为嵌套将是一个解决方案,如果不是这样我就可以。你的例子不会产生我追逐的输出 - 我需要一个按名称分组的名称列表 - 数据表中有150行,每行都有一个唯一的名称和六个学科之一。 – Nathan 2011-05-30 04:57:27

+0

我想我现在明白你的问题了......我发布了一些psudo代码 – EdmundYeung99 2011-05-30 05:17:50