星期三, 10月 01, 2008

ObjectDataSource處理分頁Parameter的問題

這幾天在研究關於"分頁的效率"我是配合listview + objectdatasource來處理..參考這篇文章..他裡面有一段..

Remove the startRowIndex and maximumRows <asp:Parameter> Elements from the ObjectDataSource’s Declarative Markup – when configuring the ObjectDataSource through the wizard, Visual Studio automatically added two <asp:Parameter> elements for the GetProductsPaged method’s input parameters. By setting EnablePaging to true, these parameters will be passed automatically; if they also appear in the declarative syntax, the ObjectDataSource will attempt to pass four parameters to the GetProductsPaged method and two parameters to the TotalNumberOfProducts method. If you forget to remove these <asp:Parameter> elements, when visiting the page through a browser you’ll get an error message like: “ObjectDataSource 'ObjectDataSource1' could not find a non-generic method 'TotalNumberOfProducts' that has parameters: startRowIndex, maximumRows.”

這一小段是叫人家把<asp:Parameter></asp:Parameter>參數值砍掉..不然會有could not find a non-generic method 的錯誤..按照文章裡說的可以work的..現在問題來啦..那如果我的SelectCountMethod不是只有文章所說的startRowIndex和maximumRows這兩個參數..我需要自己的參數..那要怎麼辦..google一下..Sad..找不到..自己試了半天..終於被我找出方法了..就是..如果你要在<asp:Parameter></asp:Parameter>裡有參數..你的objectdatasource的SelectMethod跟SelectCountMethod就要有"相同"的參數..好像有點模糊..看code快一點..

<asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
OldValuesParameterFormatString="original_{0}" TypeName="ProductsBLL"
SelectMethod="GetProductsPaged" EnablePaging="True"
SelectCountMethod="TotalNumberOfProducts">
<SelectParameters>
<asp:Parameter Name="count" Type="Int32" DefaultValue="2" />
</SelectParameters>
</asp:ObjectDataSource>

//減掉屬性面板startRowIndex,maximumRows也是一個參數
public IQueryable<da.Products> GetProductsPaged(int startRowIndex, int maximumRows, int count)
{
//...
}
//一個參數
public int TotalNumberOfProducts(int count)
{
//...
}


這樣就可以了..SickSickSickSickSickSick..有問題可以一起討論..

參考:
http://www.asp.net/learn/data-access/tutorial-25-cs.aspx

沒有留言: