日期:2014-05-18  浏览次数:20442 次

关于ObjectDataSource + GridView分页的问题
我用DropDownList绑定ObjectDataSource的筛选参数,当没有用DropDownList选择参数时分页正常,但是选择了参数,筛选出的第一页的行数就是在没有选参数时候出现在第一页的那几条,这是什么原因?

<asp:ObjectDataSource   ID= "ObjectDataSource1 "   runat= "server "  
DeleteMethod= "DeleteProduct "   SelectMethod= "GetProductToShow "  
TypeName= "MyLINQ.Services.ProductService "   EnablePaging= "True "  
SelectCountMethod= "GetProductCount ">
<deleteparameters>
<asp:controlparameter   ControlID= "GridView2 "   Name= "productid "  
PropertyName= "SelectedValue "   Type= "Int32 "   />
</deleteparameters>
<selectparameters>
<asp:controlparameter   ControlID= "DropDownList1 "   DefaultValue= "0 "  
Name= "categoryid "   PropertyName= "SelectedValue "   Type= "Int32 "   />
<asp:parameter   Name= "startRowIndex "   Type= "Int32 "   />
<asp:parameter   Name= "maximumRows "   Type= "Int32 "   />
</selectparameters>
</asp:ObjectDataSource>


GridView的select方法是
public   static   IList   GetProductToShow(int   categoryid,   int   startRowIndex,   int   maximumRows)
{
using   (SaleDataContext   db   =   new   SaleDataContext())
{
var   query   =   from   c   in   db.Products
join   o   in   db.Categories   on   c.CategoryID   equals   o.CategoryID
select   new   {
c.ProductID,c.ProductName,c.CategoryID,o.CategoryName,c.QuantityPerUnit,c.UnitPrice,c.UnitsInStock,c.UnitsOnOrder,c.Discontinued
};
var   items   =   query.Skip(startRowIndex).Take(maximumRows);
if   (categoryid   !=   0)
return   items.Where(c   =>   c.CategoryID   ==   categoryid).Select(c   =>   c).ToList();
return   items.ToList();
}
}
SelectCount方法是
public   static   int   GetProductCount(int   categoryid,   int   startRowIndex,   int   maximumRows)
{
using   (SaleDataContext   db   =   new   SaleDataContext())
{
if   (categoryid   ==   0)
return   db.Products.Select(c   =>   c).Count();
return   db.Products.Where(c   =>   c.CategoryID   ==   categoryid).Select(c   =>   c).Count();
}
}

------解决方案--------------------

用ObjectDataSource也要重新绑定? 不要重新绑定

可能是你的 <selectparameter 有问题
------解决方案--------------------
ObjectDataSource

不需要重新绑定