How to access the current ItemType Item value from

2019-08-07 01:31发布

问题:

In aspx page:

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

Code behind:

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

If I have this ListView where in ItemType="Post"; Post is a database table. How to access the current value of Item (which stands for the current record from thePost table) in the code behind method ListViewPosts_ItemDataBound

回答1:

Try this:

Please note, Post should be DataRow if Post is the 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.
    ...
 }