ASP.NET Accessing web control inside DataList control

asp.netdatalist

Am not sure why I cannot access my Label control which was inside the Panel and the Panel is inside the DataList

<asp:DataList ID="DataList2" runat="server" DataSourceID="SqlDataSource1" Width="100%">
<ItemTemplate>
    <table border="0" cellpadding="0" cellspacing="0" width="100%">
        <tr>
            <!-- post details -->
            <td style="width: 60%">
                <asp:Panel ID="panelPostDetails" runat="server" CssClass="postpage_details">
                    <table border="0" cellpadding="5" cellspacing="0" width="100%">
                        <tr>
                            <td colspan="2"><div class="postpage_header"><%# Eval("Heading") %></div></td>
                        </tr>
                        <tr>
                            <td>
                                <img src="picserver/posts/<%# Eval("ImagePath") %>/1.jpg" alt="preview" style="width: 240px;" />
                                <div id="morepictures">
                                    <asp:Label ID="lblMorePictures" runat="server" />
                                </div>
                            </td>
                            <td>
                                <div style="padding: 0px 5px 0px 5px;">
                                    <div>
                                        more stuff here
                                    </div>
                                </div>
                            </td>
                        </tr>
                    </table>
                </asp:Panel>

                <asp:RoundedCornersExtender ID="RoundedCornersExtender1" runat="server" 
                    Radius="6" 
                    Corners="All" 
                    TargetControlID="panelPostDetails"></asp:RoundedCornersExtender>
            </td>
        </tr>
    </table>    

</ItemTemplate>
</asp:DataList>

but when I tried using "lbl" in Page_Load, it seems it cannot find the control? can you please help me?

ItemDataBound and Page_Load event
---------------------------------
Panel p = DataList2.FindControl("panelPostDetails") as Panel;
Label l = p.FindControl("lblMorePictures") as Label;
l.Text = code;

that code returns Object reference not set to an instance of an object.

Thanks in advance

update:

ItemDataBound and Page_Load event
---------------------------------
Panel p = DataList2.FindControl("panelPostDetails") as Panel;
if(p==null)
{
     System.Diagnostic.Debug.WriteLine("panel does not exist");
}
else
{
     System.Diagnostic.Debug.WriteLine("panel does exist");
}

output:
panel does not exist

what on earth is going on!?!

Best Answer

Typically, you access controls like this at runtime by handling either the DataList's ItemCreated or ItemDataBound event. Here's a sample event handler:

protected void DataList2_ItemDataBound(object sender, DataListItemEventArgs e) {
   if (e.Item.ItemType == ListItemType.Item) {
        Label lbl = (Label)e.Item.FindControl("panelPostDetails").FindControl("lblMorePictures");
        lbl.Text = code;
   }
}

Wire up your the event handler like this:

<asp:DataList ID="DataList2" runat="server" OnItemDataBound="DataList2_ItemDataBound" ...
Related Topic