.net 2.0中的Repeater嵌套

in Develop

有时
我们需要在.net中做一些Repeater的嵌套,如论坛的大分类与小分类
如以下的.aspx代码

<asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="Repeater1_ItemDataBound">
<ItemTemplate>
<asp:Label ID="_lbl_" runat="server" Text='<%# Eval("aaaa")%>'></asp:Label>
<asp:Repeater ID="_rep_catalog" runat="server">
</asp:Repeater>
</ItemTemplate>
</asp:Repeater>

在最外层的Repeater1里嵌套了一个_rep_catalog
在将数据绑定到_rep_catalog时,我们需要从最外层的Repeater中获取某些条件值
这里我已经在Repeater1中加载了OnItemDataBound事件
在这个事件中我要查找_lbl_的Text值,然后查询得到结果再绑定到_rep_catalog上
.cs

protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
Label lbl = (Label)this.Repeater1.FindControl("_lbl_");
.............
}
}

但在实际运行中lbl为Null,根本没有获到这个Label控件,为什么呢?记得在1.1中就是这样查找的.到2.0中就找不到了?

后来注意到Repeater1_ItemDataBound事件的第二个参数RepeaterItemEventArgs,会不会是在这里找呢?
查了一下MSDN
使用此属性可以访问与该事件关联的 RepeaterItem 的属性。
看来是在参数e中查找了
改成这样就好了
Label lbl = (Label)e.Item.FindControl("_lbl_")

想不到在这样的问题上卡住了..

0 Comments

Leave a Reply

Using Gravatars in the comments - get your own and be recognized!

XHTML: These are some of the tags you can use: <a href=""> <b> <blockquote> <code> <em> <i> <strike> <strong>