Asp.net里实现分页浏览方法大总结

.NET ASP.NET 中web窗体数据分页显示案列 数据库数据-User表: 分页查询存储过程: create proc [dbo].[selectuser] @pagesize int, @pageindex int as select top (@pagesize) * from (select row_number() over(order by ID asc) as rownumber,* from [User]) temp_... 阅读详情

一、Datagrid+Sql语句(数据导航条为链接,有跳转到第几页的文本框可输入)

1.webform1.aspx

<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="page.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm1</title>
<meta content="Microsoft Visual Studio 7.0" name="GENERATOR">
<meta content="C#" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<table align="center">
<tr>
<td style="HEIGHT: 104px" colspan=2><asp:datagrid id="DataGrid1" runat="server" PageSize="5" Font-Size="8pt" Font-Names="Verdana" BorderWidth="1px" BackColor="#FFE0C0" BorderColor="#FFC080" HorizontalAlign="Center" CellPadding="3" AllowPaging="True" Height="152px" Width="700px">
<AlternatingItemStyle BackColor="Linen">
</AlternatingItemStyle>

<ItemStyle VerticalAlign="Bottom">
</ItemStyle>

<HeaderStyle HorizontalAlign="Center" VerticalAlign="Bottom" BackColor="#FF8000">
</HeaderStyle>

<FooterStyle HorizontalAlign="Justify" BackColor="White">
</FooterStyle>

<PagerStyle HorizontalAlign="Right" ForeColor="Red" Mode="NumericPages">
</PagerStyle>
</asp:datagrid></td>
</tr>
<tr align="middle">
<td align=left><font size=2>一共</font> <asp:Label ID="lblallcount" Runat=server Font-size="8pt" ForeColor="#0000ff" font-names="verdana"></asp:Label> <font size=2>记录</font>
<font size="2">共</font> <asp:label id="lblpagecount" runat="server" Font-Size="8pt" ForeColor="#0000ff" Font-Names="Verdana"></asp:label> <font size="2">页</font>
<font size="2">第</font> <asp:label id="lblcurrentindex" runat="server" Font-Size="8pt" ForeColor="#0000ff" Font-Names="Verdana"></asp:label> <font size="2">页</font></td>
<td align=right><asp:linkbutton id="first" runat="server" font-size="8pt" ForeColor="#3300ff" CommandArgument="first" Font-Names="Verdana"></asp:linkbutton>
<asp:linkbutton id="prev" Font-Size="8pt" ForeColor="#3300ff" CommandArgument="prev" Runat="server" Font-Names="Verdana"></asp:linkbutton>
<asp:linkbutton id="next" Font-Size="8pt" ForeColor="#3300ff" CommandArgument="next" Runat="server" Font-Names="Verdana"></asp:linkbutton>
<asp:linkbutton id="last" Font-Size="8pt" ForeColor="#3300ff" CommandArgument="last" Runat="server" Font-Names="Verdana"></asp:linkbutton>
<font size="2">跳转到第</font><asp:TextBox ID="to" Runat="server" Columns="1" /><font size="2">页</font><asp:Button ID="go" Text="GO" Runat="server" BackColor="WhiteSmoke" Font-Names="Verdana"/>
</td>
</tr>
</table>
</form>
</body>
</HTML>


2.webform1.cs

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

namespace page
{
/// <summary>
/// WebForm1 的摘要说明。
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label lblpagecount;
protected System.Web.UI.WebControls.Label lblcurrentindex;
protected System.Web.UI.WebControls.LinkButton first;
protected System.Web.UI.WebControls.LinkButton prev;
protected System.Web.UI.WebControls.LinkButton next;
protected System.Web.UI.WebControls.DataGrid DataGrid1;
protected System.Web.UI.WebControls.TextBox to;
protected System.Web.UI.WebControls.Button go;
protected System.Web.UI.WebControls.Label lblallcount;
protected System.Web.UI.WebControls.LinkButton last;

void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
first.Text="最首页";
prev.Text="前一页";
next.Text="下一页";
last.Text="最后页";
if(!IsPostBack)
{
BindGrid();
}
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.DataGrid1.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.Page_Changed);
this.first.Click += new System.EventHandler(this.btnClick);
this.prev.Click += new System.EventHandler(this.btnClick);
this.next.Click += new System.EventHandler(this.btnClick);
this.last.Click += new System.EventHandler(this.btnClick);
this.go.Click += new System.EventHandler(this.goClick);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

void Page_Changed(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
DataGrid1.CurrentPageIndex=e.NewPageIndex;
BindGrid();
}
void BindGrid()
{
SqlConnection cn=new SqlConnection("server=localhost;database=pubs;uid=sa;pwd=");
cn.Open();
SqlDataAdapter da=new SqlDataAdapter("select * from authors",cn);
DataSet ds=new DataSet();
da.Fill(ds,"authors");
DataGrid1.DataSource=ds.Tables["authors"].DefaultView;
DataGrid1.DataBind();
cn.Close();
showstate();
}
void showstate()
{
SqlConnection cn=new SqlConnection("server=localhost;database=pubs;uid=sa;pwd=");
cn.Open();
SqlDataAdapter da=new SqlDataAdapter("select * from authors",cn);
DataSet ds=new DataSet();
da.Fill(ds,"authors");
DataTable dt=ds.Tables["authors"];
int count=dt.Rows.Count;
lblallcount.Text=count.ToString();
lblpagecount.Text=DataGrid1.PageCount.ToString();
lblcurrentindex.Text=(DataGrid1.CurrentPageIndex+1).ToString();
}
void btnClick(object sender,System.EventArgs e)
{
string arg=((LinkButton)sender).CommandArgument;
switch(arg)
{
case ("first"):
DataGrid1.CurrentPageIndex=0;
break;
case ("prev"):
if(DataGrid1.CurrentPageIndex>0)
DataGrid1.CurrentPageIndex--;
break;
case ("next"):
if(DataGrid1.CurrentPageIndex<(DataGrid1.PageCount-1))
DataGrid1.CurrentPageIndex++;
break;
case ("last"):
DataGrid1.CurrentPageIndex=DataGrid1.PageCount-1;
break;
default:
DataGrid1.CurrentPageIndex=Convert.ToInt32(arg);
break;
}
BindGrid();
}

private void goClick(object sender, System.EventArgs e)
{
if(to.Text.Trim()!="")
{
int index=Int32.Parse(to.Text.Trim())-1;
if(index>=0&&index<DataGrid1.PageCount)
{
DataGrid1.CurrentPageIndex=index;
}
}
BindGrid();
}

}
}

 

 


二、动态输出法(页面只有导航按钮<>+<%showdata();%>a绑定输出数据的方法)

 

 1。PortSortCsql.aspx

<%@ Page language="c#" Codebehind="PageSortCSql.aspx.cs" AutoEventWireup="false" Inherits="localhost.PageSort.PageSortCSql" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
 <HEAD>
  <title>用C#和SQL结合进行数据浏览分页 </title>
  <LINK href="css/main.css" type="text/css" rel="stylesheet">
   <meta http-equiv="Content-Type" content="text/html; charset=GB2312">
   <meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
   <meta content="C#" name="CODE_LANGUAGE">
   <meta content="JavaScript" name="vs_defaultClientScript">
 </HEAD>
 <body MS_POSITIONING="GridLayout">
  <TABLE height="958" cellSpacing="0" cellPadding="0" width="63" border="0" ms_2d_layout="TRUE">
   <TR vAlign="top">
    <TD width="63" height="958">
     <form id="form1" method="post" runat="server">
      <TABLE height="46" cellSpacing="0" cellPadding="0" width="946" border="0" ms_2d_layout="TRUE">
       <TR vAlign="top">
        <TD width="2" height="2">
         <table id="ShowData" cellSpacing="0" cellPadding="0" align="center" border="0" height="0"
          width="0">
          <%ShowData();%> <!--输出数据-->
         </table>
        </TD>
        <TD width="662"></TD>
        <TD width="284"></TD>
       </TR>
       <TR vAlign="top">
        <TD colSpan="3" height="17"></TD>
       </TR>
       <TR vAlign="top">
        <TD colSpan="2" height="29"></TD>
        <TD>
         <table align="right" height="28" width="283">
          <tr>
           <td><%PageLoad_Count();%>; <INPUT id="first" type="button" value="  |<  " name="first" runat="server"><!--第一页-->
            <INPUT id="prior" type="button" value="  <  " name="prior" runat="server"><!--上一页-->
            <INPUT id="next" type="button" value="  >  " name="next" runat="server"><!--下一页-->
            <INPUT id="last" type="button" value="  >|  " name="last" runat="server"><!--最后一页-->
           </td>
          </tr>
         </table>
        </TD>
       </TR>
      </TABLE>
     </form>
    </TD>
   </TR>
  </TABLE>
 </body>
</HTML>

2。PortSortCsql.aspx.cs

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

namespace localhost.PageSort
{
 /// <summary>
 /// PageSortCSql 的摘要说明。
 /// </summary>
 public class PageSortCSql : System.Web.UI.Page
 {
  protected System.Web.UI.HtmlControls.HtmlInputButton first;
  protected System.Web.UI.HtmlControls.HtmlInputButton prior;
  protected System.Web.UI.HtmlControls.HtmlInputButton next;
  protected System.Web.UI.HtmlControls.HtmlInputButton last;
 
  protected static int CurrentPage = 1;//初始化开始页面
  protected static int RowCount = 0 ;//本页有多少条
  private static bool IsPrior = false;//有“前一页”
  private static bool IsNext = false;//有“下一页”
  private static bool IsLast = false;//有“最后一页”
  protected static int not_shown_records=0;//计算未显示记录数
  private static string startID = "";//设置上一页开始ID
  private static string endID = "";//设置下一页结束ID

  private static int page_count = 10;//初始化页面记录数


  private void Page_Load(object sender, System.EventArgs e)
  {  
   // 在此处放置用户代码以初始化页面
   if (!IsPostBack)
   {             
    this.CountRecord().ToString();// 记录总数
    this.Page_Count().ToString();//分页总数  

    Init_Brower();//初始化浏览
   }
  }

  #region Web 窗体设计器生成的代码
  override protected void OnInit(EventArgs e)
  {
   //
   // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
   //
   InitializeComponent();
   base.OnInit(e);
  }
 
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </summary>
  private void InitializeComponent()
  {
   this.first.ServerClick += new System.EventHandler(this.first_ServerClick);
   this.prior.ServerClick += new System.EventHandler(this.prior_ServerClick);
   this.next.ServerClick += new System.EventHandler(this.next_ServerClick);
   this.last.ServerClick += new System.EventHandler(this.last_ServerClick);
   this.Load += new System.EventHandler(this.Page_Load);

  }
  #endregion                
 
       
  /// <summary>
  /// 显示数据
  /// </summary>
  protected void ShowData()
  {
   DataSet ds = new DataSet();//数据集
//   ASWBLM.Include.UnionInfo_Provider _uip = new ASWBLM.Include.UnionInfo_Provider();
   string vSQL ="";
   endID="10";
   vSQL = GetSQLCommond(vSQL,startID,endID);
   SqlConnection SqlCon;
   SqlCon=new SqlConnection("server=localhost;database=chafen;uid=sa;pwd=''");
   SqlCon.Open();
   SqlDataAdapter da=new SqlDataAdapter(vSQL,SqlCon);
   da.Fill(ds,"Table");
//   ds = _uip.ShowAllUnionInfo(vSQL);//取得全部数据的数据集

   try
   {
    Response.Write("<p align=center>");  
    foreach(DataRow dr in ds.Tables["Table"].Rows)
    {      
     Response.Write("<tr align = center onmouseover = /"this.bgColor = '#cccccc'/" onmouseout = /"this.bgColor='';/">");
    
     Response.Write("<td align=/"left/" width=/"60%/"><font color=/"#00309C/">");
     Response.Write("<a href=/"UnionInfo_Read.aspx?id="+dr["Id"].ToString()+"/" target=/"_self/">");    
     Response.Write(dr["学号"].ToString());

     Response.Write("</a>");
     Response.Write("</td>");

     Response.Write("<td align=/"right/">");
     Response.Write("<font color=/"#999999/">");
     Response.Write("( "+dr["课程号"].ToString()+" )");
     Response.Write("     ( 已阅读"+dr["成绩"].ToString()+"次 )");
     Response.Write("</font>");
     Response.Write("</td>");

     Response.Write("</tr>");
    }               
    Response.Write("</p>");
    startID = ds.Tables["Table"].Rows[0].ItemArray[0].ToString();       //通过数组,取第一个数据,得到开始号“startID”
    RowCount = ds.Tables["Table"].DefaultView.Count;//得到表的行数
    endID = ds.Tables["Table"].Rows[RowCount-1].ItemArray[0].ToString();//通过数组,取最后一个数据,得到结束号“endID”
   }
   catch(SqlException e)
   {     
    Response.Write(e.Message);
   }
  }


  /// <summary>
  /// 计算未显示记录数
  /// </summary>
  /// <returns></returns>
  protected void NotShownRecords()
  {
   not_shown_records = this.CountRecord()/*查询总记录数*/ - (CurrentPage/*当前页*/ - 1) * page_count/*每页记录数*/;
  }


  /// <summary>
  /// 进行输出信息
  /// </summary>
  protected  void PageLoad_Count()
  {
   this.NotShownRecords();
   Response.Write("总共"+this.CountRecord()+"条记录       ");
   Response.Write("共有"+this.Page_Count()+"页       ");
   Response.Write("第"+CurrentPage.ToString()+"页       ");
   Response.Write("本页共有"+RowCount.ToString()+"条记录       ");
  }


  /// <summary>
  /// 获得总记录总数
  /// </summary>
  /// <returns>时间条件范围内记录总数intCount</returns>
  protected int CountRecord()
  {   
   int intCount = 0;
   SqlConnection SqlCon;
   SqlCon=new SqlConnection("server=localhost;database=chafen;uid=sa;pwd=''");
   SqlCon.Open();
  
   //找到条件范围内的记录总数
   string strCount = "select count(*) from Score";
  
   //找到符合条件的第一个记录
   //string strNum = "select top 1 Id from UnionInfo";

   SqlCommand MyComm = new SqlCommand(strCount,SqlCon);
   SqlDataReader dr = MyComm.ExecuteReader();//读取数据流
   if(dr.Read())
   {
                intCount = Int32.Parse(dr[0].ToString());
   }
   else
   {
               intCount = 0;
   }
   dr.Close();
   SqlCon.Close();              
   return intCount;
  }


  /// <summary>
  /// 总分页数
  /// </summary>
  /// <returns>分页总数</returns>
  protected int Page_Count()
  {
   int pageSum = 0;//分页总数  
   pageSum = this.CountRecord() / page_count;           ///记录总数/分页的页数
   if ((this.CountRecord() % page_count) > 0) pageSum++;  
   return pageSum;
  }


  /// <summary>
  /// 取得SQL语句
  /// </summary>
  /// <param name="vCmd">返回命令行</param>
  /// <returns></returns>
  private string GetSQLCommond(string vCommond,string startID,string endID)
  {
   this.NotShownRecords();//执行未显示的行
   int EndidPageNum=0;
   EndidPageNum=Convert.ToInt32(endID)*CurrentPage;;
   vCommond = "select * from (SELECT TOP "+page_count+"  {0},{1},{2},{3}  FROM";
//   (select top"+ Convert.ToInt32(endID)*CurrentPage;
//   vCommond+="* from [Score] order by {0} asc) as a order by {0} desc";
   vCommond+="(select top ";
   vCommond+=EndidPageNum.ToString();
   vCommond+="* from [Score] order by {0} asc) as a order by {0} desc) as b order by {0} asc";//见我的blog里的TOP的升序排法
   if(IsPrior)//判断“上一页”
   {
   
   }
 
   if(IsNext)//判断“下一页”
   {

   }

   if (IsLast)//判断“最后一页”
   {

   }

   vCommond = string.Format(vCommond,"Id","学号","课程号","成绩");//这个是数据表的字段
   return vCommond;
  }


  /// <summary>
  /// 输入按钮的状态,进行是否可用
  /// </summary>
  /// <param name="first">第一页的状态</param>
  /// <param name="prior">上一页的状态</param>
  /// <param name="next1">下一页的状态</param>
  /// <param name="last">最后一页的状态</param>
  protected void SetButtonState(bool first_,bool prior_,bool next_,bool last_)
  {
   if (CurrentPage==1)//到“第一页”
   {
    first.Disabled = true;//第一页状态
    prior.Disabled = true;//上一页状态
    next.Disabled = false;   //下一页状态
    last.Disabled = false; //最后一页状态
   }
   else if (CurrentPage==this.Page_Count())//到“最后一页”
   {
    first.Disabled = false;//第一页状态
    prior.Disabled = false;//上一页状态
    next.Disabled = true;   //下一页状态
    last.Disabled = true; //最后一页状态
   }
   else
   {
    first.Disabled = first_;//第一页状态
    prior.Disabled = prior_;//上一页状态
    next.Disabled = next_;   //下一页状态
    last.Disabled = last_; //最后一页状态
   }
  }


  /// <summary>
  /// 第一页按钮
  /// </summary>
  /// <param name="sender"></param>
  /// <param name="e"></param>
  private void first_ServerClick(object sender, System.EventArgs e)
  {            
   CurrentPage  = 1;
   this.SetButtonState(true,true,false,false);
   startID = "";
   endID = "";
   RowCount = '0';
   IsLast = false;
   IsPrior = false;
   IsNext = false;
  }


  /// <summary>
  /// 上一页按钮
  /// </summary>
  /// <param name="sender"></param>
  /// <param name="e"></param>
  private void prior_ServerClick(object sender, System.EventArgs e)
  {
   if( CurrentPage == 1)//判断“当前页”是否为1
   {
    this.SetButtonState(true,true,false,false);
   }
   else
   {
    CurrentPage=CurrentPage - 1;//“当前页”自减
    this.SetButtonState(false,false,false,false);
   }
   IsPrior = true;
   IsNext = false;
   IsLast = false; 
  }


  /// <summary>
  /// 最后一页
  /// </summary>
  /// <param name="sender"></param>
  /// <param name="e"></param>
  private void last_ServerClick(object sender, System.EventArgs e)
  {        
   CurrentPage=this.Page_Count();//到最后一页
   this.SetButtonState(false,false,true,true);
   IsLast = true;
   IsPrior = false;
   IsNext = false;
  }


  /// <summary>
  /// 下一页
  /// </summary>
  /// <param name="sender"></param>
  /// <param name="e"></param>
  private void next_ServerClick(object sender, System.EventArgs e)
  {                      
   if(CurrentPage == this.Page_Count())//判断“当前页”是否为“分页总数”
   {
    this.SetButtonState(false,false,true,true);
   }
   else
   {
    CurrentPage=CurrentPage + 1;//“当前页”自加
    this.SetButtonState(false,false,false,false);
   }
   IsNext = true;
   IsLast = false;
   IsPrior = false;
  }


  /// <summary>
  /// 初始浏览按钮
  /// </summary>
  /// <param name="sender"></param>
  /// <param name="e"></param>
  private void Init_Brower()
  {
   CurrentPage = 1;//肯定是从第一页开始
   if ((CurrentPage == 1) && (this.Page_Count() == 1))
   {
    first.Disabled = true;//第一页状态
    prior.Disabled = true;//上一页状态
    next.Disabled = true;//下一页状态
    last.Disabled = true; //最后一页状态
   }
   else
   {
    first.Disabled = true;//第一页状态
    prior.Disabled = true;//上一页状态
    next.Disabled = false;//下一页状态
    last.Disabled = false; //最后一页状态
   }
   startID = "";//开始号
   endID = "";//结束号 
   IsLast = false;
   IsPrior = false;
   IsNext = false;
  }
 }
}

 

三、Dataset分页

1。PageSortWithDataSet.aspx

<%@ Page language="c#" Codebehind="PageSortWithDataSet.aspx.cs" AutoEventWireup="false" Inherits="localhost.PageSort.PageSortWithDataSet" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
 <HEAD>
  <title>PageSortWithDataSet</title>
  <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
  <meta name="CODE_LANGUAGE" Content="C#">
  <meta name="vs_defaultClientScript" content="JavaScript">
  <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
 </HEAD>
 <body MS_POSITIONING="GridLayout">
  <form id="Form1" method="post" runat="server">
   <FONT face="宋体">
    <asp:DataGrid id="DataGrid1" style="Z-INDEX: 101; LEFT: 256px; POSITION: absolute; TOP: 0px" runat="server"
     AllowCustomPaging="True">
     <PagerStyle Mode="NumericPages"></PagerStyle>
    </asp:DataGrid>
    <asp:Label id="LabelPageLink" style="Z-INDEX: 102; LEFT: 296px; POSITION: absolute; TOP: 304px"
     runat="server">Label</asp:Label>
    <asp:DropDownList id="DropDownListPageLink" style="Z-INDEX: 103; LEFT: 512px; POSITION: absolute; TOP: 64px"
     runat="server" AutoPostBack="True"></asp:DropDownList></FONT>
  </form>
 </body>
</HTML>

2。PageSortWithDataSet.aspx.cs

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Text;

namespace localhost.PageSort
{
 /// <summary>
 /// PageSortWithDataSet 的摘要说明。
 /// </summary>
 public class PageSortWithDataSet : System.Web.UI.Page
 {
  protected System.Web.UI.WebControls.Label LabelPageLink;
  protected System.Web.UI.WebControls.DropDownList DropDownListPageLink;
  protected System.Web.UI.WebControls.DataGrid DataGrid1;
  protected int pagesize=10;//每页要显示的记录数
  protected int absolutepage;//要设定的当前页
  int pagecount;//总页数
  int rowstart;//数据库中分段记录开始的地方
  private void Page_Load(object sender, System.EventArgs e)
  {
   if(!IsPostBack)
   {
    // 在此处放置用户代码以初始化页面
    //int pagesize=10;//每页要显示的记录数
    absolutepage=1;//要设定的当前页
    if(Request["page"]!=null)
    {
     absolutepage=Int32.Parse(Request["page"]);
    }
    Bind(absolutepage);
    //用来显示每一页的连接,这个可以放到一个label里面
    StringBuilder sbPageLink=new StringBuilder();
    for(int i=1;i<=pagecount;i++)
    {
     sbPageLink.Append("--<a href='?page=");
     sbPageLink.Append(i);
     sbPageLink.Append("'>");
     sbPageLink.Append(i);
     sbPageLink.Append("</a>--");
    }
    //在Label里显示页码链接,点击datagrid数据改变为下一页
    LabelPageLink.Text=sbPageLink.ToString();
    //用来显示每一页的连接,这个可以放到一个DropDownList里面
    ArrayList al=new ArrayList();
    for(int i=1;i<=pagecount;i++)
    {
     al.Add(i);  
    } 
   
    //   for(int i=0;i<al.Count;i++)
    //   {
    //    ListItem altem=al[i] as ListItem;
    //    DropDownListPageLink.Items.Add(altem);
    //
    //   }
    DropDownListPageLink.DataSource=al;
    DropDownListPageLink.DataBind();

   }

   
  }

  #region Web 窗体设计器生成的代码
  override protected void OnInit(EventArgs e)
  {
   //
   // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
   //
   InitializeComponent();
   base.OnInit(e);
  }
  
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </summary>
  private void InitializeComponent()
  {   
   this.DropDownListPageLink.SelectedIndexChanged += new System.EventHandler(this.DropDownListPageLink_SelectedIndexChanged);
   this.Load += new System.EventHandler(this.Page_Load);

  }
  #endregion

  private void DropDownListPageLink_SelectedIndexChanged(object sender, System.EventArgs e)
  {
   absolutepage=Convert.ToInt32(DropDownListPageLink.SelectedValue);
   //int pagesize=10;//每页要显示的记录数
   //int absolutepage=1;//要设定的当前页
   //计算当前页
   Bind(absolutepage);
   
  }
  //绑定数据
  private void Bind(int absolutepage){
   
   
   
   SqlConnection MyConn=new SqlConnection(ConfigurationSettings.AppSettings["strConn"]);
   string MySql="select * from Score";
   SqlDataAdapter MyDa=new SqlDataAdapter(MySql,MyConn);   
   DataSet ds=new DataSet();
   //计算总记录数
   MyDa.Fill(ds,"counts");
   int rowcount=ds.Tables[0].Rows.Count;
   //计算总页数
   int n=rowcount/pagesize;
   if(rowcount==pagesize*n)
   {
    pagecount=n;
   }
   else
   {
    pagecount=n+1;
   }  
   //计算记录开始数,也就是分段的起始部分
   rowstart=pagesize*(absolutepage-1);
   MyDa.Fill(ds,rowstart,pagesize,"dtmemberx");////把当前的记录段内的记录填加到DataSet中,然后就把数据绑定到某个容器控件,比
   DataGrid1.DataSource=ds.Tables["dtmemberx"].DefaultView;
   DataGrid1.DataBind();
  }
 }
}

  Label

ASP.NET GridView控件分页功能实现示例 本文还有配套的精品资源,点击获取 简介:在.NET框架的ASP.NET Web Forms项目中,使用GridView控件展示大量数据集时,实现分页功能是提高用户体验和加载效率的重要手段。本示例将指导如何通过简单配置和事件处理实现高效分页,包括设置分页控件、绑定数据源、自定义分页样式以及响应翻页事件。示例还提供了可下载的文件包,包含完整的源代码和配置,帮助开发者深入理解Gr... 阅读详情

相关推荐

ASP.NET GridView高效分页技术实现

GridView控件是ASP.NET Web表单应用程序中常用的数据显示组件。它提供了一种灵活的方式来展示表格数据,并允许用户执行诸如排序、分页和编辑等操作。其特点包括可定制的布局、灵活的事件模型和丰富的列类型,使其成为数据密集型Web应用程序的首选。高级属性如可以用来定制分页的外观和行为。通过设置属性,你可以决定分页控件是否应该显示在多行。此外,允许你完全自定义分页按钮的外观和功能。在SQL中,TOP关键字用于限制查询结果的返回数量。

weixin_42513870的博客 1186

分页学习---在aspx页面进行普通分页

要点 主要的思路 核心代码 效果图 总结 分页的思路 把数据库需要分页的的全部信息一次性取出来,放到内存中,然后在进行分页 按需所取,把具体需要显示的某一页所要展示的数据取出来,直接显示 我采取的是第二种思路,思路步骤如下图 核心代码实现步骤恰好与箭头的方向相反 1、先完成数据操作层(DAL)的代码 /// <summary> /// 获取指定页的数据

极_晓的博客 3268

asp.net多页签页面切换

页面实现多页签切换效果。即多个页面显示或隐藏于同一个页面中,页面通过选项卡切换。

ASP.NET中的几种分页

这种方法的优点在于,指定了要显示的页数和每页的大小之后,只需从数据库中提取要显示的那一页数据即可,不需要将大量数据全部提取出来,也就是牛腩视频中说到的“真分页”,而前面的方法即取出所有数据的分页方法就是“假分页”。

点滴汇聚,智在积累。——Danny 7763

AspNetAjaxPager,Asp.Net通用无刷新Ajax分页控件,支持多样式多数据绑定

本控件可以对GridView,Repeater,DataGrid,DataList…几乎所有的.net数据绑定控件进行分页,全部无刷新,数据绑定部分可以使用存储过程也可以直接使用sql语句,这对本控件没有任何干扰!本控件具有较好的用户界面,能够根据需要变换各种样式,配合css控制效果则更好! 1.分页样式效果图: 2.如何使用: 于bin目录下添加:AspNetAjaxPager.dll引用 aspx文件内容: 复制代码 代码如下:<%@ Page Language=”C#” AutoEventWireup=”true” CodeFile=”Default.aspx.cs” Inherits=

PCB设计教程【入门篇】——PCB设计基础-PCB构成与组成

本教程基于B站Expert电子实验室的PCB设计教学的整理,为个人学习记录,旨在帮助PCB设计新手入门。所有内容仅作学习交流使用,无任何商业目的。若涉及侵权,请随时联系,将会立即处理。

2501_91732643的博客 7656

Asp.net之真假分页大揭秘、使用AspNetPager实现真分页

最近在web界面的时候,遇到了一些非常现实的问题。最让人头疼的问题就是显示数据中的书画作品。这些书画作品都会以图片的形式展示给用户。 起初做的时候并没有想太多,只按着最简单的方式将所有的图片从数据库中查出来并显示在界面中,做完界面之后,自己在数据库中添加了一些数据做测试,发现每次打开网页都很慢。由于原来看视频的时候就知道真假分页,但是当时对于分页并没有深刻的体会,不知道真假分页的优缺点。由于每次

努力+坚持,而且还很年轻 2万+

gcc/g++ 链接库的编译与链接

gcc/g++ 链接库的编译与链接 surgewong@gmail.com http://blog.csdn.net/surgewong       程序编译一般需要经预处理、编译、汇编和链接几个步骤。在实际应用中,有些公共代码需要反复使用,就把这些代码编译成为“库”文件。在链接步骤中,连接器将从库文件取得所需的代码,复制到生成的可执行文件中,这种库称为静态(链接)库,其特点是可

Surge 4万+

ASP.NET处理浏览器中数据分页的方法

在ASP的数据库编程的时,由于浏览器的大小限制,在要浏览的数据记录比较多的时候,为了达到更直观的效果,我们把这些数据记录分成若干的页面,通过数据导航按钮(或者其他超链接),分页的浏览。其实这种数据记录的分页浏览在ASP.NET也能够实现。并且在实现的过程中比起在ASP的处理过程显得条理更清晰,也更容易些。  通过浏览器进行分页浏览数据记录基本类型主要有二种。其他类型的分页浏览要么是对这二种类型

大海的专栏 916

全双工AI语音交互:从原理到实践,构建实时对话系统

语音交互技术正从传统的半双工模式向全双工模式演进,以实现更自然、实时的对话体验。全双工交互的核心原理在于模拟人类电话通话,允许双方同时发送和接收音频流,这需要流式语音识别(Streaming ASR)和实时自然语言理解(Real-time NLU)技术的深度整合。其技术价值在于显著降低交互延迟,支持智能打断(Barge-in),从而提升人机对话的流畅度和自然度。在应用场景上,全双工技术是智能助手、实时翻译、车载语音及无障碍交互等领域的关键突破。本文以微软MAI Realtime等模型为例,探讨如何利用开源技

weixin_33693070的博客 404

CSP-J第一道模拟题——贪心的小朋友

第一行是一个整数 n,表示糖果的堆数 第二行是由空格分隔的 n 个正整数,即数组 candies,表示每堆糖果的数量。后一个离场的小朋友所拿的糖果总堆数必须小于前一个小朋友(对第一个小朋友没有限制)后一个离场的小朋友所拿的糖果总量必须小于前一个小朋友(对第一个小朋友没有限制)这道题目很简单,但是必须删去无用的部分,看清题目,才能得分哦!输出一行共一个整数,表示最多可能有多少个小朋友吃上了美味的糖果。输出最多可能有多少个小朋友吃上了美味的糖果。离场之后的小朋友才能吃上美味的糖果。每个小朋友可以拿多堆糖果。

weixin_29959235的博客 639

ASP.NET真假分页—假分页

不考虑里面的内容只看布局,上面这张图片是不是你也遇到过呢?拖动滚动条,下面还有无数条的数据显示,那您就慢慢的拖吧。。。肯定大家都很反感这样吧? 当有大量数据的情况时,想要所有的内容都显示在同一页是不现实的,这时候就用到了分页技术。分页,听到这个词,你脑海中闪过的第一幅画面是什么?对,书,我们手中拿的数十页甚至上百页的书就是以分页的形式展示出来的,网站对内容的分页也是这样:对其进行分页显示

思想上移,行动下移 2148

用友U8接口开发全攻略:从EAI到OpenAPI的5种方式详解(附避坑指南)

本文深度解析了用友U8系统接口开发的五种核心方式:EAI、API、OpenAPI、SQL直连与内部CO组件。文章详细对比了各方式的适用场景、优缺点及实战操作,并提供了清晰的选型决策框架与避坑指南,帮助开发者根据具体业务需求选择最高效、最稳定的集成路径。

weixin_29322855的博客 686

ASP.NET中利用DataGrid的自定义分页功能和存储过程结合实现高效分页

             ASP.NET中利用DataGrid的自定义分页功能和存储过程结合实现高效分页 ASP.Net中的DataGrid有内置分页功能, 但是它的默认的分页方式效率是很低的,特别是在数据量很大的时候,用它内置的分页功能几乎是不可能的事,因为它会把所有的数据从数据库读出来再进行分页, 这种只选取了一小部分而丢掉大部分的方法是不可去取的.在最进的一个项目中因为一个管理页

yzx110的专栏 1万+

分析4-20mA检查电路分析

本文介绍了一种工业领域常用的4-20mA电流信号调理电路,可将标准电流信号转换为适合MCU采集的电压信号。电路包含三个关键模块:首先通过10Ω取样电阻将4-20mA电流转换为0.04-0.2V电压;然后采用同向比例运算放大器KTA333-ST5进行信号放大;最后通过二阶有源低通滤波器滤除工业环境中的高频干扰。文章详细分析了各模块的工作原理和参数设计,并以压力传感器和温度变送器为例说明了实际应用中的参数调整方法。该电路具有抗干扰能力强、精度高等特点,适用于工业压力、温度等模拟量信号的采集处理。

qq_37999850的博客 1331

扩散模型与强化学习(13):In-Context Reward Hacking现象与解决方案

奖励作弊(Reward Hacking)指智能体通过钻规则漏洞而非真正完成任务来最大化奖励信号。以论文编辑实验为例,研究提出“上下文奖励作弊”(ICRH)概念,即模型利用共享上下文钻空子,如盲目添加“正确!”或输出无关内容。实验发现,模型规模扩大可能加剧ICRH,且提示词优化难以根治。与传统奖励作弊不同,ICRH发生在部署阶段,由通用性驱动。应对建议包括多轮测试、多样化反馈及异常场景模拟,但尚无完美解决方案。

沉迷单车的追风少年 1319

ASP.NET GridView分页

1 GridView实现分页只需要在属性框中将AllowPaging(是否在GridView中打开分页功能)设置为true即可 有时打开分页后不显示页码 按下图修改 确保AllowCustomPaging(是否打开对自定义分页的支持)为False即可 二、实现翻页功能 在属性框中事件选择设置PageIndexChanging(在Grid View的当前索引页正在更改事触发)这一事件 后台代...

xuanmu 2138

CATIA V6二次开发——如何利用CaptureToFile截屏

CaptureToFile 今天给大家分享一个很有趣的功能,如何利用CATIA自带的功能输出模型展示图? 先上方法: 方法本身并不难,只需要赋予格式及输出路径即可完成该功能。结果如下: 看到这样一张图片不禁让我陷入了沉思,怎么会这么丑???? 那么如何让这个简单的功能变得有趣,图片变得好看点呢? 第一步:背景换色 第二步:隐藏结构树 ...

WorkMan的博客 2984

Asp.net 实现GridView分页时记录CheckBox状态

用GridView自带的方法实现分页功能。用ViewState记录GridView分页后各个分页面的CheckBox的选中状态。GridView不同分页面之间的跳转只是刷新当前页面,并没有跳出当前Web页面的生命周期。(这也是ViewState的作用范围)如果想要在不同Web页面之间跳转实现信息传递的话,可以用Cookie,Session等。 当我们写Asp.net程序的时候避免不了

无名指的约定 2974

霍尼韦尔SoftMaster使用说明

霍尼韦尔SoftMaster使用说明

IDEA的使用指南

IDEA基于java的使用手册,可以最快速的上手,在使用中不断的丰富自己的使用技巧.

asp.net 大文章分页代码(c#)

这几天闲 来无事,所以写了个大文章分页代码,下面提供给大家。实现思路是:后台管理员文章发布的时候自己手动添加分页符,后台我用的也是FCKeditor,所以修改了他的source增加了自己的button。在button的事件里写上自己的代码,当然就是直接insert innerhtml自己的分页标志了,我目前用的是|,|,|,|,|,|当然可以自定义其他的。首先提供个类库。1 ContentPage

esnowxu的专栏 2241

win10 跳过U8环境检测的IE WebControls.rar

win10 安装u8的时候 提示IE WebControls 不通过,这个工具可以欺骗u8的检查。 跳过U8环境检测的IE WebControls。提示导入失败后,重新检测通过。

asp.net分页原理讨论

ASP.NET 的 GridView 控件、SqlDataSource 和 ObjectDataSource 控件,其内建的「数据分页 (pager)」功能,默认做法,是当 user 每次单击 GridView 的页码换页时,都重新去数据库里,把数据表的「所有」数据,「全部」重新 SELECT 一次,并「全部」传送至 AP server (IIS),并将大量数据「全部」存储至 memory 里的

wanglei_smartfish的专栏 2526

Data-Structures-and-Algorithm-Analysis-in-Java.pdf

作者: Weiss, Mark A. 出版社: Addison Wesley Data Structures and Algorithm Analysis in Java is an “advanced algorithms” book that fits between traditional CS2 and Algorithms Analysis courses. In the old ACM Curriculum Guidelines, this course was known as CS7. This text is for readers who want to learn good programming and algorithm analysis skills simultaneously so that they can develop such programs with the maximum amo...

在ASP.NET中实现自定义分页功能

在ASP.NET中实现自定义分页功能:第一步:在前台页码中定义翻页的按钮table>tr>td style="text-align: center;   height: 5%;width:100% ">                    asp:LinkButton ID="lnkbtnFirst" runat="server" CssClass="text_gg " Enab

宁静以致远,淡泊以明志 2417

asp.net分页方法

ASP.NET分页的是我们在编程过程中遇到的常见问题,下面就这个问题说下添美的看法。 q 启用部分数据控件的内置分页功能,如设置GridView控件的"AllowPaging"属性为"true",类似的数据控件还有DetailsView等。 q 通过SQL查询语句,以提取指定部分的数据的方式完成分页的功能。也包括调用数据库中内置的存储过程完成分页数据的提取。 q 调用所对应数据适配器对象(D

我在沈阳 1009

asp.net mvc 简单分页实现

分页首先最重要的两个参数就是当前页和页面大小。 在Controller中: 分页 public ActionResult Test(string index) { if (string.IsNullOrEmpty(

z767327552的专栏 2307
上一篇: 点击一个checkbox控件,使得Datagrid里的模版里放的checkbox全部选中
下一篇: StarterKit1.x-Commerce学习笔记
showker
博客等级 码龄21年 82粉丝 31原创
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值