ASP.NET访问DataList控件中的Web控件

时间:2010-09-02 11:17:09

标签: asp.net datalist

我不知道为什么我无法访问Panel内部的Label控件,而Panel位于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>

但是当我在Page_Load中尝试使用“lbl”时,似乎无法找到控件?你能帮帮我吗?

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

该代码返回对象引用未设置为对象的实例。

提前致谢

更新

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

到底是怎么回事!?!

2 个答案:

答案 0 :(得分:1)

通常,您可以通过处理DataList的ItemCreated或ItemDataBound事件来在运行时访问此类控件。这是一个示例事件处理程序:

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;
   }
}

将事件处理程序连接起来:

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

答案 1 :(得分:0)

@ Peter的代码必须有效。

你也可以试试这个:

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

}

并将断点与st联系起来。在我的情况下,我得到lblMorePictures的文字。