首页 文章

在listview中对现有项目进行排序

提问于
浏览
0

我正在尝试对列表视图中的项目进行排序,这些项目将从listview ASPX声明中的SelectMethod填充到列表视图中 .

<asp:ListView ID="productList" runat="server" 
            DataKeyNames="ProductID" GroupItemCount="4"
            ItemType="StoreTest.Models.Product" SelectMethod="GetProducts" >

该方法只是执行linq查询并将查询返回给Listview,这是listview类的内部工作方式并且工作正常 . 问题是我有一个下拉列表,其中包含每个项目的不同变量,用户可以使用此DDL对列表视图进行排序 .

我在DDL上放置了OnSelectedIndexChanged方法,该方法执行与原始查询相同的查询,但随后根据所需的排序机制进一步对其进行排序 . 问题是我无法用这个新查询替换listview中的现有项目 . 我的代码如下:

productList.Items.Clear();
        var _db = new StoreTest.Models.SiteContext();
        IQueryable<Product> query = _db.Products;
        switch(sortList.SelectedValue)
        {
            case "Price Asc":
                query = query.OrderBy(o=> o.UnitPrice);
                break;
            case "Price Des":
                query = query.OrderByDescending(o => o.UnitPrice);
                break;
            case "Name Asc":
                query = query.OrderBy(o => o.ProductName);
                break;
            case "Name Des":
                query = query.OrderByDescending(o => o.ProductName);
                break;
            case "Product ID Asc":
                query = query.OrderBy(o => o.ProductID);
                break;
            case "Product ID Des":
                query = query.OrderByDescending(o => o.ProductID);
                break;
        }
            productList.DataSource = query;
            productList.DataBind();

这给了我一个错误,“当它使用模型绑定时,不能在'productList'上定义DataSource或DataSourceID”,我怎么能解决这个问题 . 我试图将查询中的每个单独的产品转换为ListViewItem,但这些类型是不兼容的,并将它们转换为数组是行不通的 .

public IQueryable<Product> GetProducts([QueryString("id")] int? categoryID)
    {
        var _db = new StoreTest.Models.SiteContext();
        IQueryable<Product> query = _db.Products;
        if (categoryID.HasValue && categoryID > 0)
        {
            query = query.Where(p => p.CategoryID == categoryID);
            Session["categoryid"] = categoryID;
        }
        return query;
    }

谢谢 .

EDIT Solution: 以编程方式绑定两次,删除aspx中的绑定并将初始绑定更改为protected void Page_Load(object sender,EventArgs e){IQueryable itemList = GetProducts(); productList.DataSource = itemList.ToList(); productList.DataBind(); }

public IQueryable<Product> GetProducts()
    {
        int categoryID = -1;
        if(Request.QueryString["id"]!=null)
        {
            categoryID = Convert.ToInt32(Request.QueryString["id"]);
        }
        var _db = new StoreTest.Models.SiteContext();
        IQueryable<Product> query = _db.Products;
        if (categoryID!=-1 && categoryID > 0)
        {
            query = query.Where(p => p.CategoryID == categoryID);
            Session["categoryid"] = categoryID;
        }
        return query;
    }

第二次绑定工作正常:

productList.DataSource = query.ToList();
        productList.DataBind();

1 回答

  • 0

    问题出现的原因是您正在尝试 declarative binding (ASPX中的SelectMethod)以及 code binding (productList.DataSource = query) . 当您使用SelectMethod进行绑定时,您使用的是声明性绑定(它又是您的模型绑定 - 您显示的异常表示相同) .

    我建议您也可以通过代码执行初始绑定,可能在您的Page Load上并删除SelectMethod . 这样,您的方法都将使用代码绑定,这应该工作 .

相关问题