我需要知道如何在用户控件中嵌套中继器。事情的html方面是好的,这是绑定和代码背后,我需要帮助。我只能使用sql数据源找到示例,这并没有真正的帮助。
我的中继器看起来像这样:
<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,依此类推。
数据源是在代码中创建的数据视图,其中每一行都有“name”、“Link”、“NOtation”和“discipline”。我已经将数据视图绑定到最外面的中继器,它的行为与预期一样-列出了每个条目的学科名称,但在内部中继器中没有显示任何数据。
我该怎么做才能让它工作呢?
编辑:为了清楚起见,我在代码背后有一个datatable。该表中的每一行都是一个项目,每个项目都属于一个规程。我想使用外部的中继器列出规则,内部的中继器列出每个规则下分组的项目。如下所示:
<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>目前,将datatable绑定到外部中继器实现了这一点(示例使用上面的数据):
<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 />")
}所以数据就在那里,这正是我期望看到的,我只是不能将其绑定到内部中继器。如果有更好的方法来达到同样的效果,我很乐意修改我的解决方案。
发布于 2011-05-30 11:00:33
在外部控件上,使用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();
} 发布于 2011-05-30 12:03:42
如果不想在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>发布于 2011-05-30 12:53:37
首先,您需要两个列表:规程列表,然后是所有数据的列表。
数据将学科列表绑定到外部中继器。如果有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();
}https://stackoverflow.com/questions/6171861
复制相似问题