Tip - 将ListViewItem转换为ListViewDataItem

ListView控件有一个事件ItemDataBound,事件签名如下:
    protected void dlEmployee_ItemDataBound(object sender, ListViewItemEventArgs e)
参数ListViewItemEventArgs有一个属性是Item其类型为ListViewItem,然而ListViewItem并不能提供太多的实用属性或方法,例如当前ItemIndex,以及DataItem属性,ListViewDataItem类型可以提供这些属性,代码中可以做一个类型转换将ListViewItem对象转换为ListViewDataItem对象(ListViewDataItem继承自ListViewItem):

protected void dlEmployee_ItemDataBound(object sender, ListViewItemEventArgs e)
        {
            if (e.Item is ListViewDataItem)
            {
                HtmlAnchor linkDetails = (HtmlAnchor)e.Item.FindControl("linkDetails");
                ListView dlEmployee = (ListView)sender;
                linkDetails.HRef = dlEmployee.FindMetaTable().GetActionPath(PageAction.Details, ((ListViewDataItem)e.Item).DataItem);             
            }
        }

你可能感兴趣的:(ListView,object)