启用分页时在 asp.ListView 中查找具有 Value 的项目

发布于 2024-11-26 19:22:42 字数 614 浏览 2 评论 0原文

我试图在单独页面上的 aspx ListView 中找到选定的项目,然后切换页面并选择该项目。我有我正在寻找的 ListViewItem 的 value 属性,但似乎无法让它工作。这是我尝试过的:

for (int i = 0; i < lvProject.Items.Count; i++)
{
    if (((Label)lvProject.Items[i].FindControl("Project_IDLabel")).Text == project.ToString())
    {
        lvProject.SelectItem(i);
        break;
    }
}

所以 lvProject 是我的列表视图。项目变量是一个 Int64,代表我的项目的 UID。这也是我的 ListViewItems 的值。上面代码的问题是,当启用分页并且项目位于不同页面上时,这将不起作用,因为 listView.Items.Count 仅设置为当前页面上的项目数。

我的目标是找到该项目,将列表视图设置为显示正确的页面,最后选择该项目。您可能会认为我可以只设置 SelectedValue 属性,但这并不那么简单,因为它是只读的。任何想法都会有很大帮助,提前致谢。

——罗马

I'm trying to find the selected Item in an aspx ListView that is on a separate page, then switch the page and select the item. I have the value property of the ListViewItem I am looking for, but cannot seem to get it to work. Here is what I tried:

for (int i = 0; i < lvProject.Items.Count; i++)
{
    if (((Label)lvProject.Items[i].FindControl("Project_IDLabel")).Text == project.ToString())
    {
        lvProject.SelectItem(i);
        break;
    }
}

So lvProject is my list view. The project Variable is an Int64 which represents the UID of my Project. This is also the Value of my ListViewItems. The problem with the code above is that when paging is enabled, and the item is on a different page this will not work because the listView.Items.Count is set to the # of Items on the current page only.

My goal is to find the item, set the listview to display the correct page, and finally select the item. You would figure that I could just set the SelectedValue property, but this is not that simple as it is read only. Any ideas would help greatly, thanks in advance.

--Roman

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

陌伤浅笑 2024-12-03 19:22:42

为了从对象数据源获取总记录数,您应该使用 Selected 事件,如下所示:

protected void ObjectDataSource1_Selected(object sender, ObjectDataSourceStatusEventArgs e)
{
    // Get total count from the ObjectDataSource
    DataTable dt = e.ReturnValue as DataTable;
    if (dt != null) recordCount = dt.Rows.Count; // recordCount being declared outside the method
}

然后您将能够按如下方式搜索项目:

for (int i = 0; i < recordCount; i++)
{
    Label lblItem = (Label)lvProject.Items[i].FindControl("IdLabel");
    if (lblItem.Text.Equals(itemToSearch))
    {
        lvProject.SelectedIndex = i;
        break;
    }
}

希望它有帮助!

In order to get the total record count from the object data source, you should use the Selected event as follows:

protected void ObjectDataSource1_Selected(object sender, ObjectDataSourceStatusEventArgs e)
{
    // Get total count from the ObjectDataSource
    DataTable dt = e.ReturnValue as DataTable;
    if (dt != null) recordCount = dt.Rows.Count; // recordCount being declared outside the method
}

You would then be able to search for the item as follows:

for (int i = 0; i < recordCount; i++)
{
    Label lblItem = (Label)lvProject.Items[i].FindControl("IdLabel");
    if (lblItem.Text.Equals(itemToSearch))
    {
        lvProject.SelectedIndex = i;
        break;
    }
}

Hope it helps!

奢望 2024-12-03 19:22:42

如何绑定ListView项目?

  1. 如果您使用数据库级分页(存储过程、查询) - 您应该以相同的方式进行搜索 - 通过传递搜索条件使用数据库查询/存储过程。
  2. 如果您将 ListView 项目绑定到业务/数据层提供的项目集合 - 您必须在提供项目的层上创建搜索方法,以便该方法能够循环访问项目。

How do you bind ListView Items?

  1. If you are using database level paging (stored procedure, query) - you should do search in the same way - using database query/stored procedure by passing a search criteria.
  2. If you are bind ListView Items to a collection of items which are provided by a business/data layer - you have to create search method on layer which provides items so this method will be able to loop through the items.
第七度阳光i 2024-12-03 19:22:42

您应该将 SelectedIndex 属性设置为 i

for (int i = 0; i < lvProject.Items.Count; i++)
{
    if (((Label)lvProject.Items[i].FindControl("Project_IDLabel")).Text == project.ToString())
    {
        lvProject.SelectedIndex = i;
        break;
    }
 }

You should set the SelectedIndex property to i

for (int i = 0; i < lvProject.Items.Count; i++)
{
    if (((Label)lvProject.Items[i].FindControl("Project_IDLabel")).Text == project.ToString())
    {
        lvProject.SelectedIndex = i;
        break;
    }
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文