0

我正在使用嵌套中继器控件按年份显示专辑

输出应该像

-- 专辑 1 专辑 2 专辑 3 专辑 4 [IMG More]

-- 专辑 1 专辑 2 专辑 3 专辑 4 [IMG More]

-- 专辑 1 专辑 2 专辑 3 专辑 4 [IMG More]

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

如果我用“//Generates Error”评论两条语句,代码工作正常。[IMG More]如果特定年份有超过 4 张专辑,我想从这段代码中显示图像,否则将其隐藏。

以下代码行生成错误

pnl.Visible = true; //Generates Error或者pnl.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;
    }
}

代码

<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>
4

1 回答 1

0

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

Panel Panel pnl = (Panel)e.Item.FindControl("pnlMore");
于 2012-09-05T10:14:02.560 回答