T.J. Wallace T.J. Wallace - 3 months ago 7
SQL Question

Repeater pulling as Null is code behind?

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();
}
}

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;
Comments