使用ObjectDataSource和GridView分页

来源:互联网 发布:阿里云域名优惠口令 编辑:程序博客网 时间:2024/04/30 06:38

(本人原创转载请注明地址!代码在VS2005下测试运行通过!) 

实体类:

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

/// <summary>
/// Paging 的摘要说明
/// </summary>
public class Paging
{
    int _ID;
    string _NAME;
    string _TAG;
 public Paging()
 {
 }
    public Paging(int ID, string NAME, string TAG)
    {
        this._ID = ID;
        this._NAME = NAME;
        this._TAG = TAG;
    }
    public int ID
    {
        get { return _ID; }
        set { _ID = value; }
    }
    public string NAME
    {
        get { return _NAME; }
        set { _NAME = value; }
    }
    public string TAG
    {
        get { return _TAG; }
        set { _TAG = value; }
    }
}

DAL:

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Data.OleDb;
using System.Collections.Generic;

/// <summary>
/// PagingDAL 的摘要说明
/// </summary>
public class PagingDAL
{
 public PagingDAL()
 {
 }
    static string sConnString = "Provider=MSDAORA;Data Source=BM;Persist Security Info=True;User ID=BM;Password=BM";

    public static List<Paging> GetPaging()
    {
        return GetPaging(int.MaxValue, 0, string.Empty);
    }
    public static List<Paging> GetPaging(int maximumRows,
      int startRowIndex)
    {
        return GetPaging(maximumRows, startRowIndex, string.Empty);
    }
    public static List<Paging> GetPaging(string SortExpression)
    {
        return GetPaging(int.MaxValue, 0, SortExpression);
    }

    public static List<Paging> GetPaging(int maxinumRows, int startRowIndex, string sortExpression)
    {
        string sql = "select ID,NAME,TAG from parentnodeinfo";
        if (!string.IsNullOrEmpty(sortExpression))
            sql += " order by " + sortExpression;

        using(OleDbConnection oCon = new OleDbConnection(sConnString)){
        OleDbDataAdapter da = new OleDbDataAdapter(sql, oCon);
        oCon.Open();
        DataTable dt = new DataTable();
        da.Fill(dt);
        List<Paging> results = new List<Paging>();
        int currentIndex = startRowIndex;
        int itemRead = 0;
        int totalRecords = dt.Rows.Count;
        while (itemRead < maxinumRows && currentIndex < totalRecords)
        {
            Paging p = new Paging();
            p.ID = Convert.ToInt32(dt.Rows[currentIndex]["ID"]);
            p.NAME = dt.Rows[currentIndex]["NAME"].ToString();
            p.TAG = dt.Rows[currentIndex]["TAG"].ToString();
            results.Add(p);
            itemRead++;
            currentIndex++;
        }
        oCon.Close();
        return results;
        }
    }
    public static int GetPagingCount()
    {
        string sql = "select count(*) from parentnodeinfo";
        OleDbConnection conn = new OleDbConnection(sConnString);
        OleDbCommand cmd = new OleDbCommand();
        cmd.CommandText = sql;
        cmd.Connection = conn;
        conn.Open();
        int count = int.Parse(cmd.ExecuteScalar().ToString());
        conn.Close();
        return count;
    }

}
界面层:

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>无标题页</title>
</head>
<body>
    <form id="form1" runat="server">
        <asp:GridView ID="GridView1" runat="server" DataSourceID="ObjectDataSource1" AllowPaging="True" AutoGenerateColumns="False" AllowSorting="True" CellPadding="4" ForeColor="#333333" GridLines="None">
            <Columns>
                <asp:BoundField DataField="TAG" HeaderText="TAG" SortExpression="TAG" />
                <asp:BoundField DataField="NAME" HeaderText="NAME" SortExpression="NAME" />
                <asp:BoundField DataField="ID" HeaderText="ID" SortExpression="ID" />
            </Columns>
            <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <RowStyle BackColor="#EFF3FB" />
            <EditRowStyle BackColor="#2461BF" />
            <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
            <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
            <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <AlternatingRowStyle BackColor="White" />
        </asp:GridView>
        &nbsp; &nbsp;
        <i>You are viewing page
        <%=GridView1.PageIndex + 1%>
        of
        <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectMethod="GetPaging"
            TypeName="PagingDAL" SelectCountMethod="GetPagingCount" MaximumRowsParameterName="maxinumRows" SortParameterName="sortExpression"  EnablePaging="True">
        </asp:ObjectDataSource>
        <%=GridView1.PageCount%>
        </i>
    </form>
</body>
</html>
注意的地方:最好不要用ObjectDataSource的智能标记-配置数据源,因为它会添加

<asp:ObjectDataSource>的

            <SelectParameters>
                <asp:Parameter Name="maxinumRows" Type="Int32" />
                <asp:Parameter Name="startRowIndex" Type="Int32" />
                <asp:Parameter Name="sortExpression" Type="String" />
            </SelectParameters>

删除也可以,如果保留会导致:

ObjectDataSource“ObjectDataSource1”未能找到带参数的非泛型方法“GetPagingCount”: maxinumRows, startRowIndex, sortExpression。

参考资料:http://msdn2.microsoft.com/en-us/library/Aa479347.aspx

原创粉丝点击