views:

48

answers:

2

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!?!

A: 

Typically, you access 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" ...
Peter
the Label is inside the Panel and the Panel is inside the DataList' ItemTemplate.
Nullstr1ng
I've updated the code to match the sample you posted.
Peter
sorry Peter, but still doesn't work. I have updated the code again.
Nullstr1ng
Are you still trying to do this in Page_Load()? If so, that won't work. You need to do this in your DataLists's ItemDataBound event.
Peter
nope. I tried that ItemDataBound event.
Nullstr1ng
Sorry, I just looked at my code sample and realized that it had a bug. I've updated it. Within the ItemDataBound event handler you need to use e.Item.FindControl() **not DataList2.FindControl**.
Peter
let me try that
Nullstr1ng
finally!! thanks Peter!
Nullstr1ng
A: 

@Peter's code must work.

you can also try this:

protected void DataList2_ItemDataBound(object sender, DataListItemEventArgs e)
{
        string st= (e.Item.FindControl("lblMorePictures") as Label).Text;

}

and put breakpoint to wath to st. In my case I get a text of lblMorePictures.

loviji