Repeater pulling as Null is code behind?

298 views Asked by At

I have a repeater for Frequently asked questions on a Website that I'm trying to get separated into categories depending on which treeid it is related to.

When I reference the repeater (rp_FAQ) in the code behind to check if it is null so I can pass the category id to the stored procedure it keeps returning null when I need the item to exist when check that it's null.

I can't seem to find the route of the problem so as second set of eyes would be really grateful.

Thanks.

I have this repeater:

                    <asp:Repeater ID="rp_FAQ" DataSourceID="DS_GetFAQs" runat="server" OnItemDataBound="rp_FAQCategories_ItemDataBound">
                    <HeaderTemplate>
                        <div class="container-full fares_container">
                            <div class="row">
                    </HeaderTemplate>
                    <ItemTemplate>

                        <dt>Q: <%# Eval("Question") %></dt>
                        <dd>A: <%# Eval("Answer") %></dd>

                    </ItemTemplate>
                    <FooterTemplate>
                        </dl>
                        </div>
                    </div>
                    </FooterTemplate>
                </asp:Repeater>

Here is my code behind:

    protected void rp_FAQCategories_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    Repeater rp_FAQRep = e.Item.FindControl("rp_FAQ") as Repeater;

    if (rp_FAQRep != null)
    {

        if (TreeData.CurrentDefault.IsRelation(Convert.ToInt32(Resources.Pages.FAQ)))
        {
            DS_GetFAQs.SelectParameters["CategoryID"].DefaultValue = "1";
        }

        rp_FAQRep.DataSource = DS_GetFAQs;
        rp_FAQRep.DataBind();
    }
}
3

There are 3 answers

1
DavidG On BEST ANSWER

The FindControl method is only needed if you are trying to get a reference to a control which is inside the control which raised the event (in this case ItemDataBound). So you can either reference the control directly if it's not nested inside another control or use sender. For example:

var rp_FAQRep = rp_FAQ; //A bit pointless but demonstrates the point

or

var rp_FAQRep = (Repeater)sender;
0
Patrick Hofman On

The problem is that you are trying to find the rp_FAQ control in a control inside rp_FAQ itself.

Since e.Item is the RepeatItem under rp_FAQ, you can just cast e.Item.Parent, or sender if you will.

0
Charles Mager On

e.Item is the RepeaterItem within the Repeater - it does not contain the Repeater itself. Just cast the sender of the event:

var rp_FAQRep = (Repeater)sender;