这期内容当中小编将会给大家带来有关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自定义分页了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注亿速云行业资讯频道。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。