1

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

3
  • panelPostDetails is it Exists? is Panel Element in ItemTemplate? Commented Sep 2, 2010 at 11:27
  • 2
    post the markup - chances are you've missed something obvious. Commented Sep 2, 2010 at 11:28
  • @loviji - yes the panelPostDetails exists and it's in ItemTemplate. Commented Sep 2, 2010 at 12:01

3 Answers 3

1

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" ...
Sign up to request clarification or add additional context in comments.

4 Comments

the Label is inside the Panel and the Panel is inside the DataList' ItemTemplate.
sorry Peter, but still doesn't work. I have updated the code again.
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.
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.
0

@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.

Comments

0

With my Form View I needed to add CType to the FindControl. I understand DataList doesn't necessarily need the Ctype however it is possible the Panel may need this prefix. In this case frmDelView is the name of my Form View. In my case the code line below is in my PreRender of my form. That was the only place at the time of creating the form it would return the data I needed to pass to a label on a next page of a Wizard Step page. Long story. Note: my code is vb and not C#. It should be nearly or exactly the same.

 CType(frmDelView.FindControl("txtcboDAcct"), TextBox).Text

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.