<table border="0" cellpadding="0" cellspacing="0" style="margin-bottom: 5px" width="100%">
<asp:Repeater runat="server" ID="rptypelist" OnItemDataBound="rptypelist_ItemDataBound">
<ItemTemplate>
<tr>
<td class="fb">
<a href="Lists.aspx?Qtid=<%#Eval("qtid") %>"><%#Eval("qtypename") %></a> <span id="askCount_15"
class="date2">[<%#Eval("typecount")%>]</span>
</td>
</tr>
<tr>
<td>
<div id="askTop2_15" class="ask_list">
<asp:Repeater runat="server" ID="rpquestionlist">
<ItemTemplate>
<a href="Question.aspx?qid=<%#Eval("liftquestionid")%>">
<%#Eval("questiontitle")%></a><br />
</ItemTemplate>
</asp:Repeater>
</div>
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
</table>
private void RpTypeBind()
{//GetQuestionTypeAndCount() 返回一个datatable
this.rptypelist.DataSource = LiftQuestionCtr.GetQuestionTypeAndCount();
this.rptypelist.DataBind();
}
protected void rptypelist_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
//判断里层repeater处于外层repeater的哪个位置( AlternatingItemTemplate,FooterTemplate,
//HeaderTemplate,,ItemTemplate,SeparatorTemplate)
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
Repeater rep = e.Item.FindControl("rpquestionlist") as Repeater;//找到里层的repeater对象
DataRowView rowv = (DataRowView)e.Item.DataItem;//找到分类Repeater关联的数据项
int typeid = Convert.ToInt32(rowv["qtid"]); //获取填充子类的id
rep.DataSource = LiftQuestionCtr.GetSomeQuestionsByTypeid(typeid, 2);
rep.DataBind();
}
}
此方法应该适合于repeater嵌套,datalist嵌套。
aspx:
<asp:Repeater ID="rtNav" runat="server" OnItemDataBound="rtNav_ItemDataBound">
<ItemTemplate>
<div class="listNav">
<dl class="colNav">
<dt>
<asp:Label ID="lblTitle" runat="server" Text='<%#Eval("title") %>'></asp:Label>
<asp:HiddenField ID="hidID" Value='<%#Eval("id")%>' runat="server" />
</dt>
<asp:DataList ID="dlNav" runat="server" RepeatColumns="5" Width="100%" ItemStyle-CssClass="dlItem">
<ItemTemplate>
<asp:HyperLink ID="hlNav" NavigateUrl='<%#Eval("path") %>' Text='<%#Eval("title") %>'
Target="_blank" Font-Underline="false" runat="server">
</asp:HyperLink>
</ItemTemplate>
</asp:DataList>
</dl>
</div>
</ItemTemplate>
</asp:Repeater>
cs
public SiFangOperations_News2Entities db = new SiFangOperations_News2Entities();
#region 加载数据
protected void Page_Load(object sender, EventArgs e)
{
BindRtNav();
}
# endregion
#region 绑定数据
/// <summary>
/// 绑定reapter
/// </summary>
protected void BindRtNav()
{
var query = from p in db.BaseMenuTypeNav
orderby p.id ascending
select p;
rtNav.DataSource = query.ToList();
rtNav.DataBind();
}
/// <summary>
/// 绑定内层datalist(同时也是reapter的行绑定)
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void rtNav_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if(e.Item.ItemType==ListItemType.Item||e.Item.ItemType == ListItemType.AlternatingItem)
{
DataList rep = e.Item.FindControl("dlNav") as DataList;
HiddenField hid=e.Item.FindControl("hidID") as HiddenField;
int id = Convert.ToInt32(hid.Value);
var query = from p in db.BaseMenuNav
where p.typeId == id
orderby p.sort ascending
select p;
rep.DataSource = query;
rep.DataBind();
}
}
#endregion