0

I am Using Nested repeater Controls to show albums by Year

Out put should be like

Year

-- Album 1 Album 2 Album 3 Album 4 [IMG More]

Year

-- Album 1 Album 2 Album 3 Album 4 [IMG More]

Year

-- Album 1 Album 2 Album 3 Album 4 [IMG More]

SOLVED: I was mentioning the wrong name of Panel Panel pnl = (Panel)e.Item.FindControl("pnlMore");

Code works fine if i comment the Two Statement with "//Generates Error". From this code i want to show an image [IMG More] if perticular year has more than 4 Albums otherwise hide it.

Following line of code generates Error

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

ERROR MESSAGE: Object reference not set to an instance of an object.

Parent Repeater Control rptAlbumsCategories & Child Repeater Control rptAlbums

I am not sure why this is generating error.

I would appreciate if there is a better way of doing same.

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>
Learning
  • 19,469
  • 39
  • 180
  • 373
  • Side-note: you should not create a new exception in your catch block, instead you should throw the same, so instead of `throw ex` or(what you've written) `throw new Exception(ex.ToString());` simply this: `throw;`. http://stackoverflow.com/a/4761295/284240 – Tim Schmelter Sep 05 '12 at 10:00

1 Answers1

0

SOLVED: I was mentioning wrong name of panel control

Panel Panel pnl = (Panel)e.Item.FindControl("pnlMore");
Learning
  • 19,469
  • 39
  • 180
  • 373