在嵌套转发器中使用时,子转发器控件无法隐藏或显示Panel Control

时间:2012-09-05 09:57:46

标签: c# asp.net repeater webforms nested-repeater

我正在使用嵌套转发器控件按年显示相册

输出应该像

- 专辑1专辑2专辑3专辑4 [IMG更多]

- 专辑1专辑2专辑3专辑4 [IMG更多]

- 专辑1专辑2专辑3专辑4 [IMG更多]

已解决:我提到了Panel Panel pnl = (Panel)e.Item.FindControl("pnlMore");的错误名称

如果我用“// Generates Error”注释Two Statement,代码工作正常。从这段代码我想显示一个图像[IMG More]如果特定年份有超过4个专辑,否则隐藏它。

以下代码行生成错误

pnl.Visible = true; //Generates Errorpnl.Visible = false; //Generates Error

错误消息:对象引用未设置为对象的实例。

父级转发器控制rptAlbumsCategories&子女中继控制rptAlbums

我不确定为什么会产生错误。

如果有更好的方法,我将不胜感激。

protected void rptAlbumCategory_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("rptAlbums");
            Panel pnl = (Panel)e.Item.FindControl("pnlNext");
        //Control pnl = (Control)e.Item.FindControl("pnlNext");
        Repeater2.DataSource = getAlbumsYear(year, pnl);
        Repeater2.DataBind();
    }
        }
    catch (Exception ex)
    {
        throw ;
    }
}

private DataTable getAlbumsYear(int year, Panel pnl)
{
    try
    {
        DataSet ds = new DataSet();
        string strSql = "SELECT TOP 4 AlbumID, AlbumName, AlbumIcon FROM Album";
        strSql += " DATEPART(YYYY,AlbumDate) = " + year;
        strSql += " ORDER BY AlbumDate DESC ";
        ds = DataProvider.Connect_Select(strSql);
        DataTable dt = ds.Tables[0];
        //Code to show Next arrow
        string strSql2 = "SELECT AlbumID, AlbumName, AlbumIcon FROM Album ";
        strSql2 += "WHERE DATEPART(YYYY,AlbumDate) = " + year;
        DataSet ds2 = new DataSet();
        ds2 = DataProvider.Connect_Select(strSql2);
        if (ds2.Tables[0].Rows.Count > 3)
        {
            pnl.Visible = true;  //Generates Error
        }
        else
        {
            pnl.Visible = false; //Generates Error
        }
        return dt;
    }
    catch (Exception ex)
    {
        throw;
    }
}

CODE

<asp:Repeater ID="rptAlbumsCategories" runat="server" OnItemDataBound="rptAlbumCategory_ItemBound">
                <ItemTemplate>
                    <div class="AlbumCategory">
                        <asp:HyperLink ID="hyplnkCat1" runat="server" NavigateUrl='<%# Eval("Year")%>'>
                        <div id="AlbumCatbyYear" class="AlbumCatbyYear"  >
                            <asp:Label ID="lblAlbumYear" runat="server" Text='<%# Eval("Year")%>'></asp:Label>
                        </div>
                        </asp:HyperLink>
                    </div>
                    <div class="AlbumCatWrapper">
                        <asp:Repeater ID="rptAlbums" runat="server" >
                        <ItemTemplate>
                            <asp:HyperLink ID="hylnkToAlbum" NavigateUrl='<%# getAlbumURL(Eval("AlbumID")) %>' runat="server" >
                            <div class="PGImageFrame">
                                <div class="boxgrid  captionfull"> 
                                <asp:Image ID="Image1" ImageUrl='<%# getAlbumImagePath(Eval("AlbumIcon")) %>' AlternateText='<%# Eval("AlbumName") %>'  runat="server" />

                                    <div class="cover  boxcaption">
                                        <asp:Label ID="lblAlbumTitle"  runat="server" Text='<%# Eval("AlbumName") %>'></asp:Label>
                                    </div>
                                </div>
                            </div>
                            </asp:HyperLink>
                        </ItemTemplate>
                        </asp:Repeater>
                        <asp:Panel ID="pnlMore" runat="server" Visible="false">
                            <asp:Image ID="imgNext" runat="server" ImageUrl="~/Images/PG-Next.png" />
                        </asp:Panel>
                    </div>
                </ItemTemplate>
            </asp:Repeater>

1 个答案:

答案 0 :(得分:0)

解答:我提到了错误的面板控件名称

Panel Panel pnl = (Panel)e.Item.FindControl("pnlMore");