动态绑定GridView并自定义分页

后台代码

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.OracleClient;

public partial class _Default : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            fillGridView();
        }

    } 
    //填充GridView
    protected void fillGridView()
    {
        DataSet ds = new DataSet();

        ds = GetData();
        GridView1.DataSource = ds;
        GridView1.DataBind();
        getRefSet(ds);

    }
    //分页
    protected void getRefSet(DataSet ds)
    {
        lblCurrentIndex.Text = "第 " + (GridView1.PageIndex + 1).ToString() + " 页";
        lblPageCount.Text = "共 " + GridView1.PageCount.ToString() + " 页";
        lblRecordCount.Text = "总共 " + ds.Tables[0].Rows.Count.ToString() + " 条";

        if (ds.Tables[0].Rows.Count == 0)
        {
            lnkbtnFirst.Visible = false;
            lnkbtnPre.Visible = false;
            lnkbtnNext.Visible = false;
            lnkbtnLast.Visible = false;

            lblCurrentIndex.Visible = false;
            lblPageCount.Visible = false;
            lblRecordCount.Visible = false;
        }
        else if (GridView1.PageCount == 1)
        {
            lnkbtnFirst.Visible = false;
            lnkbtnPre.Visible = false;
            lnkbtnNext.Visible = false;
            lnkbtnLast.Visible = false;
        }
        lnkbtnFirst.CommandArgument = "1";
        lnkbtnPre.CommandArgument = (GridView1.PageIndex == 0 ? "1" : GridView1.PageIndex.ToString());
        lnkbtnNext.CommandArgument = (GridView1.PageCount == 1 ? GridView1.PageCount.ToString() : (GridView1.PageIndex + 2).ToString());
        lnkbtnLast.CommandArgument = GridView1.PageCount.ToString();
    }
    //第几页
    protected void lnkbtnJumpPage_Click(object sender, EventArgs e)
    {
        GridView1.PageIndex = int.Parse(txtJumpPage.Text) - 1;
        lblCurrentIndex.Text = "第 " + (GridView1.PageIndex + 1).ToString() + " 页";
        fillGridView();
        GridView1.DataBind();

    }
    protected void PagerButtonClick(object sender, EventArgs e)
    {
        GridView1.PageIndex = Convert.ToInt32(((LinkButton)sender).CommandArgument) - 1;
        fillGridView();
        GridView1.DataBind();

    }  
    // 转换SQL查询结果为DataSet
    private DataSet GetData()
    {

        OracleConnection conn = new OracleConnection("Data Source=wgyx;user=wgyx;password=wgyx;");
        OracleCommand cmd = new OracleCommand();
        cmd.Connection = conn;
        String strSql = "select id,fuid,the from sys_organ";
        cmd.CommandText = strSql;
        cmd.CommandType = CommandType.Text;
        DataSet ds = new DataSet();
        try
        {
            conn.Open();
            OracleDataAdapter ad = new OracleDataAdapter();
            ad.SelectCommand = cmd;
            ad.Fill(ds);

        }
        catch (Exception ex)
        {
            Response.Write("数据库错误,错误原因:" + ex.Message);
            Response.End();
        }
        finally
        {
            conn.Close();
        }
        return ds;
    }
}

前台代码
<%@ 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 id="Head1" runat="server">
    <title>分页测试程序</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        &nbsp;&nbsp;
        <asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
            AutoGenerateColumns="False" CellPadding="4" GridLines="None" 
            ForeColor="#333333" >
            <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
            <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
            <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
            <AlternatingRowStyle BackColor="#DCDCDC" />
            <PagerSettings Visible="False" />
            <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
            <RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
            <Columns>             
                <asp:TemplateField HeaderText="ID">
                <ItemTemplate>
                <%# Eval("id")%>
                </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="地名">
                <ItemTemplate>
                <%# Eval("the")%>
                </ItemTemplate>
                </asp:TemplateField>  
                <asp:ButtonField HeaderText="地图" Text="标注" />
            </Columns>
        </asp:GridView>
        <asp:LinkButton ID="lnkbtnFirst" runat="server" OnClick="PagerButtonClick">首页</asp:LinkButton><asp:LinkButton ID="lnkbtnPre" runat="server" OnClick="PagerButtonClick">上一页</asp:LinkButton>
        <asp:LinkButton ID="lnkbtnNext" runat="server" OnClick="PagerButtonClick">下一页</asp:LinkButton>
        <asp:LinkButton ID="lnkbtnLast" runat="server" OnClick="PagerButtonClick">尾页</asp:LinkButton>
     <p><asp:Label ID="lblCurrentIndex" runat="server" Text="第1页"></asp:Label>
        <asp:Label ID="lblPageCount" runat="server" Text="共1页"></asp:Label>
        <asp:Label ID="lblRecordCount" runat="server" Text="记录数"></asp:Label>
        <asp:TextBox ID="txtJumpPage" runat="server" Width="24px">1</asp:TextBox>
        <asp:LinkButton ID="lnkbtnJumpPage" runat="server" OnClick="lnkbtnJumpPage_Click" >跳转</asp:LinkButton>
      </p>  
        </div>
    </form>
</body>
</html>



效果图


你可能感兴趣的:(exception,server,object,asp,textbox,dataset)