您好,登錄后才能下訂單哦!
這期內容當中小編將會給大家帶來有關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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。