Link to home
Start Free TrialLog in
Avatar of smithdale87
smithdale87

asked on

ASP.NET repeater giving me trouble

I have an repeater that fills in cells in a table ( see below ).
For some reason, when I try and find my table cell control with ID "litPropertyCell", I always get a null value.

Is there something simple I'm doing wrong??

Thanks in advance.



<tr>
                <asp:Repeater runat="server" ID="repProperties" OnItemDataBound="repProperties_ItemDataBound"
                    EnableViewState="true">
                    <ItemTemplate>
                        <td ID="litPropertyCell">
                            <asp:Literal ID="litProperty" runat="server"></asp:Literal>
                        </td>
                    </ItemTemplate>
                </asp:Repeater>
            </tr>
 
 
 
<%--- CodeBehind ---- %>
 protected void repProperties_ItemDataBound(Object Sender, RepeaterItemEventArgs e)
    {
        XmlNode node = e.Item.DataItem as XmlNode;
        Literal litProperty = e.Item.FindControl("litProperty") as Literal;
        HtmlTableCell cell = e.Item.FindControl("litPropertyCell")  as HtmlTableCell;
        if (cell == null) throw new Exception("LitPropertyCell is NULL!");
        cell.BgColor = "Red";
 
       litProperty.Text = node.InnerText;
    }

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of NazoUK
NazoUK
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of smithdale87
smithdale87

ASKER

thanks for the quick response

I edited the code ( see below ). Unfortunately, I still get the same error.

Any other idaes?


<td ID="litPropertyCell" runat="server">

Open in new window

Try putting the code inside your itemdatabound routine inside this if:

if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType== ListItemType.AlternatingItem)
{
...
}
Still getting the same error
Figured it out.
I had an outer repeater that had an itemTemplate and AlternatingItemTemplate. The ItemTemplate was posted above. It was missing the "runat='server'". I failed to realize that the Alternating template also needed the same thing.

Again thanks for your help

<AlternatingItemTemplate>
<tr>
                <asp:Repeater runat="server" ID="repProperties" OnItemDataBound="repProperties_ItemDataBound"
                    EnableViewState="true">
                    <ItemTemplate>
                        <td ID="litPropertyCell" runat="server">
                            <asp:Literal ID="litProperty" runat="server"></asp:Literal>
                        </td>
                    </ItemTemplate>
                </asp:Repeater>
            </tr>
</AlternatingItemTemplate>

Open in new window