ASP.NET中怎么利用DataGrid自定义分页
这期内容当中小编将会给大家带来有关ASP.NET 中怎么利用DataGrid自定义分页,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。
创新互联是一家专业从事成都网站设计、网站制作、网页设计的品牌网络公司。如今是成都地区具影响力的网站设计公司,作为专业的成都网站建设公司,创新互联依托强大的技术实力、以及多年的网站运营经验,为您提供专业的成都网站建设、营销型网站建设及网站设计开发服务!
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自定义分页了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注创新互联行业资讯频道。
分享名称:ASP.NET中怎么利用DataGrid自定义分页
本文网址:http://myzitong.com/article/pcoejo.html