惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

L
LINUX DO - 最新话题
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
PCI Perspectives
PCI Perspectives
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
H
Heimdal Security Blog
S
Security @ Cisco Blogs
N
News | PayPal Newsroom
J
Java Code Geeks
罗磊的独立博客
Security Archives - TechRepublic
Security Archives - TechRepublic
N
News and Events Feed by Topic
V
V2EX
WordPress大学
WordPress大学
Google Online Security Blog
Google Online Security Blog
N
News and Events Feed by Topic
www.infosecurity-magazine.com
www.infosecurity-magazine.com
月光博客
月光博客
AI
AI
小众软件
小众软件
The GitHub Blog
The GitHub Blog
MongoDB | Blog
MongoDB | Blog
A
Arctic Wolf
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
美团技术团队
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Hacker News - Newest:
Hacker News - Newest: "LLM"
T
Tailwind CSS Blog
S
Schneier on Security
博客园 - 三生石上(FineUI控件)
F
Full Disclosure
B
Blog RSS Feed
Forbes - Security
Forbes - Security
S
SegmentFault 最新的问题
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
人人都是产品经理
人人都是产品经理
云风的 BLOG
云风的 BLOG
Jina AI
Jina AI
Cisco Talos Blog
Cisco Talos Blog
U
Unit 42
Project Zero
Project Zero
H
Hacker News: Front Page
Y
Y Combinator Blog
Application and Cybersecurity Blog
Application and Cybersecurity Blog
The Cloudflare Blog
大猫的无限游戏
大猫的无限游戏
S
Secure Thoughts
The Hacker News
The Hacker News
Microsoft Azure Blog
Microsoft Azure Blog

博客园 - 胡枫

ASP.NET URL Rewrite. URL重写 实现类似textarea这样可以滚动显示内容的方法 各种浏览器CSS hack方法 HTML+CSS+JS学习笔记 JS学习笔记ZT Asp.net 备份、还原Ms SQLServer及压缩Access数据库 我该怎样卸载IE7? ASP.NET Application,Session,Cookie和ViewState等对象用法和区别 (转) ASP读取显示TXT文件内容 ASP.NET 防注入的两个通用函数 innerText,outerText,innerHTML,outerHTML 如何在TextBox控件中显示系统当前时间?包括:年、月、日、时、分 ASP.NET 防注入的两个通用函数 C# 操作ACCESS数据库 ASP判断是否包含字符串(InStr 函数) asp.net2.0中Repeater的分页使用 ASP.NET2.0下含有DropDownList的GridView编辑、删除的完整例子! 一个GridView编辑删除的例子 asp.net中没有类似asp中Left()函数怎么办?
asp.net 中DataGrid自定义分页(简单,实用,易懂)
胡枫 · 2008-01-12 · via 博客园 - 胡枫

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;
namespace WS.Webs
{
    
/// <summary>
    
/// WebForm1 的摘要说明。
    
/// </summary>

    public class WebForm1 : System.Web.UI.Page
    
{
        
protected System.Web.UI.WebControls.DataList datalist1;
        
protected System.Web.UI.WebControls.Label LPageCount;
        
protected System.Web.UI.WebControls.Label LRecordCount;
        
protected System.Web.UI.WebControls.LinkButton Fistpage;
        
protected System.Web.UI.WebControls.LinkButton Prevpage;
        
protected System.Web.UI.WebControls.LinkButton Nextpage;
        
protected System.Web.UI.WebControls.LinkButton Lastpage;
        
protected System.Web.UI.WebControls.Label LCurrentPage;
        
protected System.Web.UI.WebControls.TextBox gotoPage;
        
//自定义分页码记录
        const int PageSize=20;
        
int PageCount,RecCount,CurrentPage,Pages,JumpPage;//定义几个保存分页参数变量

    
        
private void Page_Load(object sender, System.EventArgs e)
        
{
            
if(!Page.IsPostBack)
            
{
                RecCount 
= Calc();//通过Calc()函数获取总记录数
                PageCount = RecCount/PageSize + OverPage();//计算总页数(加上OverPage()函数防止有余数造成显示数据不完整)
                ViewState["PageCounts"= RecCount/PageSize - ModPage();//保存总页参数到ViewState(减去ModPage()函数防止SQL语句执行时溢出查询范围,可以用存储过程分页算法来理解这句)
                ViewState["PageIndex"= 0;//保存一个为0的页面索引值到ViewState
                ViewState["JumpPages"= PageCount;//保存PageCount到ViewState,跳页时判断用户输入数是否超出页码范围
                
//显示LPageCount、LRecordCount的状态
                LPageCount.Text = PageCount.ToString();
                LRecordCount.Text 
= RecCount.ToString();
                
//判断跳页文本框失效
                if(RecCount <= 20)
                    gotoPage.Enabled 
= false;
                TDataBind();
//调用数据绑定函数TDataBind()进行数据绑定运算

            }

        }


        
//计算余页
        public int OverPage()
        
{
            
int pages = 0;
            
if(RecCount%PageSize != 0)
                pages 
= 1;
            
else
                pages 
= 0;
            
return pages;
        }


        
//计算余页,防止SQL语句执行时溢出查询范围
        public int ModPage()
        
{
            
int pages = 0;
            
if(RecCount%PageSize == 0 && RecCount != 0)
                pages 
= 1;
            
else
                pages 
= 0;
            
return pages;
        }


        
/*
   *计算总记录的静态函数
   *本人在这里使用静态函数的理由是:如果引用的是静态数据或静态函数,连接器会优化生成代码,去掉动态重定位项(对

海量数据表分页效果更明显)。
   *希望大家给予意见、如有不正确的地方望指正。
  
*/

        
public static int Calc()
        
{
            
int RecordCount = 0;
            SqlCommand MyCmd 
= new SqlCommand("select count(*) as co from fdcproject",MyCon());
            SqlDataReader dr 
= MyCmd.ExecuteReader();
            
if(dr.Read())
                RecordCount 
= Int32.Parse(dr["co"].ToString());
            MyCmd.Connection.Close();
            
return RecordCount;
        }


        
//数据库连接语句(从Web.Config中获取)
        public static SqlConnection MyCon()
        
{
            SqlConnection MyConnection 
= new SqlConnection(ConfigurationSettings.AppSettings["DSN2"]);
            MyConnection.Open();
            
return MyConnection;
        }


        
private void TDataBind()
        
{
            CurrentPage 
= (int)ViewState["PageIndex"];//从ViewState中读取页码值保存到CurrentPage变量中进行按钮失效运算
                Pages = (int)ViewState["PageCounts"];//从ViewState中读取总页参数进行按钮失效运算
            
//判断四个按钮(首页、上一页、下一页、尾页)状态
            if (CurrentPage + 1 > 1)
            
{
                Fistpage.Enabled 
= true;
                Prevpage.Enabled 
= true;
            }

            
else
            
{
                Fistpage.Enabled 
= false;
                Prevpage.Enabled 
= false;
            }

            
if (CurrentPage == Pages)
            
{
                Nextpage.Enabled 
= false;
                Lastpage.Enabled 
= false;
            }

            
else
            
{
                Nextpage.Enabled 
= true;
                Lastpage.Enabled 
= true;
            }

            
//数据绑定到DataList控件
            DataSet ds = new DataSet();
            
//核心SQL语句,进行查询运算(决定了分页的效率:))
            SqlDataAdapter MyAdapter = new SqlDataAdapter("Select Top "+PageSize+" * from fdcproject where projid not in(select top "+PageSize*CurrentPage+" projid from fdcproject order by projid asc) order by projid asc",MyCon());
             MyAdapter.Fill(ds,
"news");
            datalist1.DataSource 
= ds.Tables["news"].DefaultView;
            datalist1.DataBind();
            
//显示Label控件LCurrentPaget和文本框控件gotoPage状态
            LCurrentPage.Text = (CurrentPage+1).ToString();
            gotoPage.Text 
= (CurrentPage+1).ToString();
            
//释放SqlDataAdapter
            MyAdapter.Dispose();
        }












        
Web 窗体设计器生成的代码

        
private void gotoPage_TextChanged(object sender, System.EventArgs e)
        
{
            
try
          
{
           JumpPage 
= (int)ViewState["JumpPages"];//从ViewState中读取可用页数值保存到JumpPage变量中
           
//判断用户输入值是否超过可用页数范围值
           if(Int32.Parse(gotoPage.Text) > JumpPage || Int32.Parse(gotoPage.Text) <= 0)
            Response.Write(
"<script>alert('页码范围越界!');location.href='WebForm8.aspx'</script>");
           
else
            
{
                 
int InputPage = Int32.Parse(gotoPage.Text.ToString()) - 1;//转换用户输入值保存在int型InputPage变量中
                 ViewState["PageIndex"= InputPage;//写入InputPage值到ViewState["PageIndex"]中
               TDataBind();//调用数据绑定函数TDataBind()再次进行数据绑定运算
            }

           }

             
//捕获由用户输入不正确数据类型时造成的异常
            catch(Exception exp)
            
{
                Response.Write(
"<script>alert('"+exp.Message+"');location.href='WebForm8.aspx'</script>");
            }




        
        }


        
private void Page_OnClick(object sender, System.EventArgs e)
        
{
            CurrentPage 
= (int)ViewState["PageIndex"];//从ViewState中读取页码值保存到CurrentPage变量中进行参数运算
            Pages = (int)ViewState["PageCounts"];//从ViewState中读取总页参数运算

            
string cmd = ((LinkButton)sender).CommandName;
            
switch(cmd)//筛选CommandName
            {
                
case "next":
                    CurrentPage
++;
                    
break;
                
case "prev":
                    CurrentPage
--;
                    
break;
                
case "last":
                    CurrentPage 
= Pages;
                    
break;
                
default:
                    CurrentPage 
= 0;
                    
break;
            }

            ViewState[
"PageIndex"= CurrentPage;//将运算后的CurrentPage变量再次保存至ViewState
            TDataBind();//调用数据绑定函数TDataBind()
        }



    }

}