如何访问从ListView控件的OnItemDataBound方法当前的ItemType项目价值?(

2019-10-22 17:19发布

在aspx页面:

<asp:ListView ID="ListViewPosts" ItemType="Post" 
SelectMethod="ListViewPosts_GetData" runat="server"
OnItemDataBound="ListViewPosts_ItemDataBound">
        ...
        ...
    </asp:ListView>

后面的代码:

protected void ListViewPosts_ItemDataBound(object sender, ListViewItemEventArgs e)
{
  ...
  Post p = Item; //where Item stands for the current Post record in ListView.
  ...
}

如果我有这样ListView其中ItemType="Post" ; Post是一个数据库表。 如何访问的当前值Item (表示从当前记录Post在后面方法的代码表) ListViewPosts_ItemDataBound

Answer 1:

尝试这个:

请注意, 邮政的DataRow如果帖子是表。

protected void ListViewPosts_ItemDataBound(object sender, ListViewItemEventArgs e)
{
   ...
   DataRow p = (DataRow)e.Item.DataItem; //where Item stands for the current Post record in ListView.
    ...
 }


文章来源: How to access the current ItemType Item value from OnItemDataBound method of ListView?