如何从 ListView 的 OnItemDataBound 方法访问当前的 ItemType Item 值?

How to access the current ItemType Item value from OnItemDataBound method of ListView?

在 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.
  ...
}

如果我在 ItemType="Post" 中有这个 ListViewPost 是一个数据库 table。 如何在代码隐藏方法ListViewPosts_ItemDataBound

中访问Item的当前值(代表来自Posttable的当前记录)

试试这个:

请注意,如果 Post[=18,Post 应该是 DataRow =] 是 table.

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