T.J. Wallace T.J. Wallace - 1 year ago 75
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.


I have this repeater:

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

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


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;

Answer Source

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


var rp_FAQRep = (Repeater)sender;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download