how to set label text inside listview from code behind

旧城冷巷雨未停 提交于 2019-12-02 08:13:16

Use the following event handler in code behind:

protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e)
{
    if (e.Item.ItemType == ListViewItemType.DataItem)
    {
        // Display the e-mail address in italics.
        Label lblProdID = (Label)e.Item.FindControl("lblProdID");

        // Here, lblProdID contains your data ProductID as text, change to "My Text"
        lblProdID.Text = "My Text";

        DataRowView rowView = e.Item.DataItem as DataRowView;
        string myProductID = rowView["ProductID"].ToString();
        // Here, you can access your data
    }
}

Connect this event handler to your listview:

<asp:ListView ID="ListView1" runat="server" OnItemDataBound="MyListView_ItemDataBound" />

ListView has ItemDataBind event which gets called everytime a records gets datebound to the ListView template. Create an even handler and within it

Use the e.Item.DataItem property to get the reference of object being bound, and then go ahead and do the formatting as you required.

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