Asp.Net教程,WinForm教程,Asp.Net MVC,vs2008教程,vs2010教程,Silverlight技术,源码下载,Asp.Net视频教程
全站热门标签
vs2010 Silverlight 存储过程 水晶报表 ADO.NET JavaScript LINQ AjaxPro DataGridView 面向对象 Extjs GridView XML DevExpress HTML教程 Oracle jQuery 分页 GDI+ Visual C++2010 MySQL Office2010 WPF MVC Dojo WCF4.0 VB.NET Sql2005 textbox cookie WCF WinForm Discuz!NT SQL经典语句 T-SQL checkbox ASPxGridView F# asp.net SQL VS2008新特性 DropDownList Access TreeView Ajax VS2008 页面执行时间 Flex 字符串 回调 VB2005 DataSet C#时间 ASP.NET性能优化 用户在线检测 动画
尚未分类 LINQ教程 Enterprise技术 性能优化/调试 水晶报表与打印 安全与加密 图形图像 文件处理 基础教程 Web Services 内置对象 控件示例 正则表达式\采集 ADO.NET 缓存\泛型\线程 XML技术 Url重写\静态页 vs2008综合教程
当前位置: 主页 > ASP.NET教程 > 尚未分类 >

自定义ascx文件来控制分页

时间:2010-06-27 21:51来源:未知 作者:admin 点击:

1、moonPage.ascx:

<%@ Control Language="c#" AutoEventWireup="false" Codebehind="moonPage.ascx.cs" Inherits="firstcs_03.moonPage" TargetSchema="http://schemas.microsoft.com/intellisense/ie5"%>
<TABLE id="Table1" cellSpacing="1" cellPadding="1" width="98%" border="0">
 
<TR>
  
<TD align="right"><asp:linkbutton id="FirstPage" runat="server">[首页]</asp:linkbutton>&nbsp;
   
<asp:linkbutton id="PrevPage" runat="server">[上页]</asp:linkbutton>&nbsp;
   
<asp:linkbutton id="NextPage" runat="server">[下页]</asp:linkbutton>&nbsp;
   
<asp:linkbutton id="LastPage" runat="server">[末页]</asp:linkbutton>&nbsp;
   
<asp:literal id="Literal1" runat="server" Text="转到第"></asp:literal><asp:textbox id="NewPageIndex" runat="server" Width="31px" CssClass="input1"></asp:textbox><asp:literal id="Literal2" runat="server" Text="页"></asp:literal>&nbsp;
   
<asp:button id="NewPageGo" runat="server" Text="Go" CssClass="input1"></asp:button>&nbsp;</TD>
 
</TR>
</TABLE>

2、moonPage.ascx.cs
namespace firstcs_03
{
 
using System;
 
using System.Data;
 
using System.Drawing;
 
using System.Web;
 
using System.Web.UI.WebControls;
 
using System.Web.UI.HtmlControls;
 
using System.Data.SqlClient;

 
/// <summary>
 
///  moonPage 的摘要说明。
 
/// </summary>

 public class moonPage : System.Web.UI.UserControl
 
{
  
protected System.Web.UI.WebControls.LinkButton FirstPage;
  
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.Literal Literal1;
  
protected System.Web.UI.WebControls.TextBox NewPageIndex;
  
protected System.Web.UI.WebControls.Literal Literal2;
  
protected System.Web.UI.WebControls.Button NewPageGo;
  
protected int currentpage;
  
protected int pagesize;
  
protected string proc;
  
protected System.Web.UI.WebControls.DataGrid datagrid;
  


  
private void Page_Load(object sender, System.EventArgs e)
  
{
   
if(!IsPostBack)
   
{
    
using(SqlConnection conn = new SqlConnection("uid=sa;pwd=sa;server=localhost;initial catalog=pubs;timeout=90"))
       
{
     SqlCommand cmd 
= new SqlCommand("select count(*) as expr1 from authors",conn);
     cmd.Connection.Open();
     rowcount 
= (int)cmd.ExecuteScalar();
     cmd.Connection.Close();
     ViewState[
"rowscount"= rowcount;
    }

    ViewState[
"currentpage"= currentpage;
    FillGrid(proc,currentpage,pagesize,datagrid);
   }

  }


 

  

  
private void FillGrid(string proc,int currentpage,int pagesize,DataGrid datagrid)
  
{
   
using(SqlConnection conn = new SqlConnection("Uid=sa;pwd=sa;server=localhost;initial catalog=pubs;timeout=90"))
      
{
    SqlCommand cmd 
= new SqlCommand(proc,conn);
    cmd.CommandType 
= CommandType.StoredProcedure;
    cmd.Parameters.Add(
"@CurrentPage",currentpage);
    cmd.Parameters.Add(
"@PageSize",pagesize);
    cmd.Connection.Open();

    SqlDataReader sdr 
= cmd.ExecuteReader();
    datagrid.DataSource 
= sdr;
    datagrid.DataBind();
    sdr.Close();
    cmd.Connection.Close();
   }

  }


  
//首页
  private void FirstPage_Click(object sender, System.EventArgs e)
  
{
   
//disabled首页按钮和上一页按钮
   FirstPage.Enabled = false;
   PrevPage.Enabled 
= false;   
   currentpage 
= 0;
   ViewState[
"currentpage"= currentpage;
   FillGrid(proc,currentpage,pagesize,datagrid);
   
//如果不止一页
   if((int)ViewState["rowscount"]>((int)ViewState["currentpage"]+1)*pagesize)
   
{
    NextPage.Enabled 
= true;
   }

   
if((int)ViewState["rowscount"]>((int)ViewState["currentpage"]+1)*pagesize)
   
{
    LastPage.Enabled 
= true;
   }

  }


  
//上一页
  private void PrevPage_Click(object sender, System.EventArgs e)
  
{

   NextPage.Enabled 
= true;
   LastPage.Enabled 
= true;
   currentpage 
= (int)ViewState["currentpage"]-1;
   ViewState[
"currentpage"= currentpage;
   FillGrid(proc,currentpage,pagesize,datagrid);   
   
//如果到首页则disabled首页和上一页按钮
   if((int)ViewState["currentpage"]==0)
   
{
    PrevPage.Enabled 
= false;
    FirstPage.Enabled 
= false;
    
//return;
   }

  }


  
//下一页
  private void NextPage_Click(object sender, System.EventArgs e)
  
{
   ViewState[
"currentpage"= (int)ViewState["currentpage"]+1;
   currentpage 
= (int)ViewState["currentpage"];
   FillGrid(proc,currentpage,pagesize,datagrid);
   PrevPage.Enabled 
= true;
   FirstPage.Enabled 
= true;
   
//如果已经到了最后一页
   if(((int)ViewState["currentpage"]+1)*pagesize>(int)ViewState["rowscount"])
   
{
    NextPage.Enabled 
= false;
    LastPage.Enabled 
= false;
   }

  }


  
//末页
  private void LastPage_Click(object sender, System.EventArgs e)
  
{
   LastPage.Enabled 
= false;
   NextPage.Enabled 
= false;
   ViewState[
"currentpage"= (int)Math.Ceiling((int)ViewState["rowscount"]/pagesize);
   currentpage 
= (int)ViewState["currentpage"];
   FillGrid(proc,currentpage,pagesize,datagrid);
   
//如果有不止一页的纪录
   if((int)ViewState["currentpage"]>1)
   
{
    FirstPage.Enabled 
= true;
    PrevPage.Enabled 
= true;
   }

    
//如果只有一页的纪录
   else
   
{
    FirstPage.Enabled 
= false;
    PrevPage.Enabled 
= false;
   }

  }

  
//跳转
  private void NewPage_Go(string i)
  
{
   
try
   
{
    
int PageIndex = Int32.Parse(i);
    
if (PageIndex<=0)
    
{
     PageIndex 
= 0;
    }

    
else
    
{
     
if(PageIndex>(int)Math.Ceiling((int)ViewState["rowscount"]/pagesize))
     
{
      PageIndex 
= (int)Math.Ceiling((int)ViewState["rowscount"]/pagesize);
     }

     
else
     
{
      PageIndex
--;
     }

    }

    
//简单起见,将所有的linkbutton全部改为enable=true
    FirstPage.Enabled = true;
    NextPage.Enabled 
= true;
    LastPage.Enabled 
= true;
    PrevPage.Enabled 
= true;
    ViewState[
"currentpage"= PageIndex;
    FillGrid(proc,(
int)ViewState["currentpage"],pagesize,datagrid);
   }

   
catch(Exception)
   
{
    
return;
   }

  }


  
private void NewPageGo_Click(object sender, System.EventArgs e)
  
{
   NewPage_Go(NewPageIndex.Text.Trim());
  }


 }

}

(责任编辑:admin)

Tags:分页
责任编辑:admin
返回顶部
------分隔线----------------------------
推荐内容
骆驼户外男 真皮磨砂日常休闲鞋 低帮 2011秋冬新款 专柜正品特价 骆驼户外男 真皮磨砂日常休闲鞋 低帮 2011秋冬新款 专柜正品特价