开发者

find current element in repeater

How can i get current Ma开发者_如何学GoinNavigationMenu hyprelink in code behind and check if is current menu clicked then i will change him default CSS.

I try with this code but is always null

HyperLink mainNavigationMenu = siteMapAsBulletedList.FindControl("MainNavigationMenu") as HyperLink;

full repeater code:

<asp:Repeater runat="server" ID="siteMapAsBulletedList" DataSourceID="smdsMenu">
                        <HeaderTemplate>
                            <li><asp:HyperLink ID="MainNavigationMenu" runat="server" NavigateUrl='<%#SiteMap.RootNode.Url%>'
                                Text='<%#SiteMap.RootNode.Title%>'></asp:HyperLink></li>                        
                        </HeaderTemplate>
                        <ItemTemplate>
                            <li><asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%#Eval("Url")%>' Text='<%#Eval("Title")%>'></asp:HyperLink></li>
                        </ItemTemplate>
                        </asp:Repeater>


Are you verifying which type of repeater item you are looking at ?

Attach an ItemDataBound to your repeater and do something like this :

private void rptPanier_ItemDataBound(Object sender , RepeaterItemEventArgs e) 
{
    if (e.Item.ItemType == ListItemType.Header)
    {
      var myItem = (Hyperlink)e.Item.FindControl("YourControlName");
    }
}

Then you will have a reference on it and you can do whatever you want ! Beware tho, the ItemData item, which can be found in the repeateritemeventargs, is always null when the repeater is creating the header.

Hope this helps !


The repeater has a collection of Items. Each Item is a RepeaterItem, which has an ItemType property. For header items, this value will be "ListItemType.Header". Therefore, you want to perform .FindControl() upon that particular repeater item, not the entire Repeater itself.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜