ASP.NET Accessing web control inside DataList control

坚强是说给别人听的谎言 提交于 2019-12-02 05:24:41
Peter

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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!