首先新建一个名为article.aspx的文件,将以下内容拷贝到article.aspx.cs中:
ASP.NET DataGrid自定义分页代码1
- using System;
- using System.Data;
- using System.Configuration;
- using System.Collections;
- 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.SqlClient;
- public partial class article : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- int cpage = Convert.ToInt32(Request.QueryString["page"]);
- if(cpage==0)
- {
- this.hlFrontPage.NavigateUrl = Request.ServerVariables["SCRIPT_NAME"];
- this.hlNextPage.NavigateUrl = Request.ServerVariables["SCRIPT_NAME"] + "?page=" + Convert.ToString(cpage + 2);
- }
- else
- {
- this.hlFrontPage.NavigateUrl = Request.ServerVariables["SCRIPT_NAME"] + "?page=" + Convert.ToString(cpage - 1);
- this.hlNextPage.NavigateUrl = Request.ServerVariables["SCRIPT_NAME"] + "?page=" + Convert.ToString(cpage + 1);
- }
- this.hlFirstPage.NavigateUrl = Request.ServerVariables["SCRIPT_NAME"];
- dataBin();
- //***************************************************
- //PageCount的值必须在数据绑定之后才能得到,否则只能是0
- //***************************************************
- int lastPage =this.DataGrid1.PageCount;//lastPage是记录总页数
- this.hlLastPage.NavigateUrl = Request.ServerVariables["SCRIPT_NAME"] + "?page="+Convert.ToInt32(lastPage);
- if (cpage ==Convert.ToInt32(this.DataGrid1.PageCount))
- {
- this.hlNextPage.NavigateUrl = Request.ServerVariables["SCRIPT_NAME"] + "?page=" + Convert.ToString(lastPage);
- this.hlFrontPage.NavigateUrl = Request.ServerVariables["SCRIPT_NAME"] + "?page=" + Convert.ToString(cpage - 1);
- }
- this.lbShowMess.Text= "当前第"+Convert.ToInt32(this.DataGrid1.CurrentPageIndex+1)+"页";
- }
- public void dataBin()
- {
- DB mydb = new DB();
- SqlConnection conn = mydb.createConn();
- conn.Open();
- SqlDataAdapter sdr = new SqlDataAdapter();
- DataSet ds = new DataSet();
- sdr.SelectCommand = new SqlCommand("select * from [User]", conn);
- sdr.Fill(ds, "users");
- int page=Convert.ToInt32(Request.QueryString["page"]);
- if (page == 0)
- {
- page = 1;
- }
- this.DataGrid1.CurrentPageIndex =Convert.ToInt32(page-1);
- this.DataGrid1.DataSource = ds.Tables["users"];
- this.DataGrid1.DataBind();
- conn.Close();
- }
- protected void DataGrid1_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
- {
- }
- }
再将以下内容拷贝到article.aspx中
ASP.NET DataGrid自定义分页代码2
- < %@ Page Language="C#" AutoEventWireup="true" CodeFile="article.aspx.cs" Inherits="article" %>
- < !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>
- < mce:style type="text/css">< !--
- @import url('css.css');
- -->< /mce:style>< style type="text/css" mce_bogus="1"> @import url('css.css');
- < /style>
- < /head>
- < body>
- < form id="form1" runat="server">
- < div style="text-align: center" mce_style="text-align: center">
- < asp:DataGrid ID="DataGrid1" runat="server" AutoGenerateColumns="False" AllowPaging="True" OnPageIndexChanged="DataGrid1_PageIndexChanged" Width="500px" ShowHeader="False" BackColor="White" BorderColor="#CC9966" BorderStyle="None" CellPadding="4" Font-Bold="False" Font-Italic="False" Font-Overline="False" Font-Strikeout="False" Font-Underline="False" GridLines="None">
- < Columns>
- < asp:TemplateColumn>
- < ItemTemplate>
- < %# DataBinder.Eval(Container.DataItem,"id") %>
- < /ItemTemplate>
- < HeaderStyle Width="10px" />
- < ItemStyle Font-Bold="False" Font-Italic="False" Font-Overline="False" Font-Strikeout="False"
- Font-Underline="False" HorizontalAlign="Left" />
- < /asp:TemplateColumn>
- < asp:TemplateColumn>
- < ItemTemplate>
- < %# DataBinder.Eval(Container.DataItem,"Uid") %>
- < /ItemTemplate>
- < ItemStyle Font-Bold="False" Font-Italic="False" Font-Overline="False" Font-Strikeout="False"
- Font-Underline="False" HorizontalAlign="Left" />
- < HeaderStyle Width="450px" />
- < /asp:TemplateColumn>
- < asp:TemplateColumn>
- < ItemTemplate>
- < %# DataBinder.Eval(Container.DataItem,"title") %>
- < /ItemTemplate>
- < HeaderStyle Width="30px" />
- < ItemStyle Font-Bold="False" Font-Italic="False" Font-Overline="False" Font-Strikeout="False"
- Font-Underline="False" HorizontalAlign="Center" />
- < /asp:TemplateColumn>
- < asp:TemplateColumn>
- < ItemTemplate>
- < input id="Checkbox1" type="checkbox" value="< %# DataBinder.Eval(Container.DataItem,"id") %>" />
- < /ItemTemplate>
- < HeaderStyle Width="10px" />
- < ItemStyle Font-Bold="False" Font-Italic="False" Font-Overline="False" Font-Strikeout="False"
- Font-Underline="False" HorizontalAlign="Center" />
- < /asp:TemplateColumn>
- < /Columns>
- < PagerStyle Visible="False" BackColor="#FFFFCC" ForeColor="#330099" HorizontalAlign="Center" />
- < FooterStyle BackColor="#FFFFCC" ForeColor="#330099" />
- < SelectedItemStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" />
- < ItemStyle BackColor="White" ForeColor="#330099" />
- < HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="#FFFFCC" />
- < /asp:DataGrid> < br />
- < asp:HyperLink ID="hlFirstPage" runat="server">首页< /asp:HyperLink>
- < asp:HyperLink ID="hlFrontPage" runat="server">上一页< /asp:HyperLink>
- < asp:HyperLink ID="hlNextPage" runat="server">下一页< /asp:HyperLink>
- < asp:HyperLink ID="hlLastPage" runat="server">末页< /asp:HyperLink>
- < asp:Label ID="lbShowMess" runat="server" Width="104px">< /asp:Label>< /div>
- < /form>
- < /body>
- < /html>
以上就介绍了ASP.NET DataGrid自定义分页的实现。
责任编辑:周立方
来源:
CSDN博客
相关推荐
2009-08-06 17:13:56
2009-07-28 09:32:41
2011-04-19 10:33:16
2009-07-31 14:49:22
2011-05-19 10:16:27