开发者

ASP.NET Accessing web control inside DataList control

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:R开发者_如何转开发oundedCornersExtender>
            </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!?!


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


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


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜