如何从Repeater Control中的公共函数访问Panel id

时间:2012-09-09 06:12:48

标签: c# asp.net css webforms

我需要显示我有相册的年份列表。我使用Repeater控件和div控件中的包装器年份。我还将当前年份作为QueryString传递。我需要以红色显示当前年份作为div的背景。我的面板总是返回null,因为我无法通过Object Sender, RepeaterItemEventArgs e。如何更改此代码,以便我可以从后面的代码更改CssClass。

我可以使用Literal Control&amp;显示我的列表年份列表<ul> ...我可以使用&amp;完全控制我的名单css。

请建议我如何才能最好地实现这一目标。有效的方法。我也会按照我试图做的方式继续完成它,因为它可能在将来有用。非常适合我的教育

http://localhost:65123/Gallery/Albums.aspx?Year=2011&PageID=38

<asp:Repeater ID="rptAlbumsYears" runat="server" >
    <ItemTemplate>
        <div class="AlbumPageYearListing">
            <asp:HyperLink ID="hyplnkCat1" runat="server" NavigateUrl='<%# getAlbumURLbyCat(Eval("Year"))%>'>
                <asp:Panel ID="pnlYear" runat="server">
                    <asp:Label ID="lblAlbumYear" runat="server" Text='<%# Eval("Year")%>'></asp:Label>
                </asp:Panel>
            </asp:HyperLink>
        </div>
    </ItemTemplate>
</asp:Repeater>

.CS代码

protected String getAlbumURLbyCat(object Year)
{
    string url;
    int PageID = Helper.GetPageIDbyName("Albums.aspx", "en-us");
    url = "~/Albums.aspx?Year=" + Year + "&PageID=" + PageID;
    try
    {
        int CurYear = 0;
        CurYear = int.Parse(Request["Year"].ToString());
        int iYear = int.Parse(Year.ToString());
        //Image imgArticleList = (Image)e.Item.FindControl("imgArticleList") as Image;
        Panel pnlYear = (Panel)FindControl("pnlYear");

       // Panel pnlYear = (Panel) 
        if (CurYear == iYear)
        {
            pnlYear.CssClass = "AlbumPageYearListingCurrent";
        }
    }
    catch (Exception ex)
    {
    }

1 个答案:

答案 0 :(得分:0)

使用以下代码解决问题

protected void rptAlbumsYears_ItemBound(Object Sender, RepeaterItemEventArgs e)
{
    try
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            DataRowView drv = (DataRowView)e.Item.DataItem;
            int Year = Convert.ToInt32(drv["Year"]);
            Repeater Repeater2 = (Repeater)e.Item.FindControl("rptAlbumsYears");
            Panel pnlYear = (Panel)e.Item.FindControl("pnlYear");
            int CurYear = 0;
            CurYear = int.Parse(Request["Year"].ToString());
            if (CurYear == Year)
            {
                pnlYear.CssClass = "AlbumPageYearListingCurrent";
            }
        }
    }
    catch (Exception ex)
    {
        throw;
    }
}