如何在列表视图中选择项目

如何在列表视图中选择项目

问题描述:

我通常使用gridview。当我想在gridview中只显示3个项目时,我使用数据绑定如下,它工作正常:

I normally use gridview. When i want to display only 3 items in gridview, I use databound as follows and it works fine:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if(e.Row.RowType == DataControlRowType.DataRow)
        {
            if(e.Row.RowIndex >2)
            {
                e.Row.Attributes.Add("style", "display:none");
            }
        }
    }





现在我想在listview中使用类似的代码。我尝试使用以下代码仅显示4项listview:



Now i want to use similar code in listview. I tried the following code for displaying only 4 items of listview:

protected void newestListView_ItemDataBound(object sender, ListViewItemEventArgs e)
    {
        if (newestListView.Items.DataItemIndex > 3)
        {
            e.Items.Attributes.Add("style", "display:none;");
        }
    }





这给了我一个错误:



This gives me an error:

Compiler Error Message: CS1061: 'System.Collections.Generic.IList<System.Web.UI.WebControls.ListViewDataItem>' does not contain a definition for 'DataItemIndex' and no extension method 'DataItemIndex' accepting a first argument of type 'System.Collections.Generic.IList<System.Web.UI.WebControls.ListViewDataItem>' could be found (are you missing a using directive or an assembly reference?)

Kindly help me with proper code. I am using ASP.NET C#

Many thanks for your help

What I have tried:

I have tried Index , DataItme Index also in place of ItemIndex but it did not work

您是否尝试过 dataItem.DisplayIndex dataItem。 DataItemIndex 吗?其中 dataItem 是:

Did you tried dataItem.DisplayIndex or dataItem.DataItemIndex? Where dataItem is:
ListViewDataItem dataItem = (ListViewDataItem)e.Item