没事写着完。自己写个分页,代码虽然烦锁,但是只是想实现里面的功能

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 Webedtor
{
 /// <summary>
 /// TestFenye 的摘要说明。
 /// </summary>
 public class TestFenye : System.Web.UI.Page
 {
  protected System.Web.UI.WebControls.DataGrid DataGrid1;
  private SqlConnection con=new SqlConnection("server=192.168.1.188;uid=sa;pwd=;database=northwind");
  protected System.Web.UI.WebControls.TextBox TextBox1;
  protected System.Web.UI.WebControls.Button Button1;
  protected System.Web.UI.WebControls.Button Button2;
  protected System.Web.UI.WebControls.Label lblcount;
  protected System.Web.UI.WebControls.PlaceHolder ph;
  protected System.Web.UI.WebControls.Label lblCur;
  protected System.Web.UI.WebControls.LinkButton LinkButton1;
  protected System.Web.UI.WebControls.LinkButton LinkButton2;
  protected System.Web.UI.WebControls.Label lblPagecount;
  private SqlCommand cmd=null;
  private const int PageCount=20;
 
  private void Page_Load(object sender, System.EventArgs e)
  {
   if(!this.IsPostBack)
   {
    bindAutoPage(0);
    lblcount.Text=GetAllCount().ToString();
    Mode();
    
   }
   Feye();
  }

  public void Feye()
  {
   int count=int.Parse(lblPagecount.Text);
   LinkButton lb;
   ph.Controls.Clear();
   if(count<=10)
   {
    for(int i=0;i<count;i++)
    {
     lb=new LinkButton();
     lb.Text="["+(i+1).ToString()+"]";
     lb.ID="lbl"+(i+1).ToString();
     lb.CommandName=i.ToString();
     lb.Command+=new System.Web.UI.WebControls.CommandEventHandler(this.PageLbFenye);
     ph.Controls.Add(lb);
    }
   }
   else
   {
    int cur=int.Parse(lblCur.Text);
    if(cur>=count-6)
    {
     for(int i=count-10;i<count;i++)
     {
      lb=new LinkButton();
      lb.Text="["+(i+1).ToString()+"]";
      lb.ID="lbl"+(i+1).ToString();
      lb.CommandName=i.ToString();
      lb.Command+=new System.Web.UI.WebControls.CommandEventHandler(this.PageLbFenye);
      ph.Controls.Add(lb);
     }
    }
    else
    {
     if(cur<=4)
     {
      for(int i=0;i<10;i++)
      {
       lb=new LinkButton();
       lb.Text="["+(i+1).ToString()+"]";
       lb.ID="lbl"+(i+1).ToString();
       lb.CommandName=i.ToString();
       lb.Command+=new System.Web.UI.WebControls.CommandEventHandler(this.PageLbFenye);
       ph.Controls.Add(lb);
      }
     }
     else
     {
      for(int i=cur-5;i<cur+5;i++)
      {
       lb=new LinkButton();
       lb.Text="["+(i+1).ToString()+"]";
       lb.ID="lbl"+(i+1).ToString();
       lb.CommandName=i.ToString();
       lb.Command+=new System.Web.UI.WebControls.CommandEventHandler(this.PageLbFenye);
       ph.Controls.Add(lb);
      }
     }
    }
   }
  }

  public void PageLbFenye(Object sender,CommandEventArgs e)
  {
   int pageindex=int.Parse(e.CommandName);
   lblCur.Text=(int.Parse(e.CommandName)+1).ToString();
   bindAutoPage(pageindex);
   Feye();
  }

  public void Mode()
  {
   int count=int.Parse(lblcount.Text);
   int countPage=0;
   if(count%PageCount==0)
   {
    countPage=count/PageCount;
   }
   else
   {
    countPage=count/PageCount+1;
   }
   lblPagecount.Text=countPage.ToString();
  }


  public int GetAllCount()
  {
   cmd=con.CreateCommand();
   cmd.Connection=con;
   cmd.CommandText="test_getCount";
   cmd.CommandType=CommandType.StoredProcedure;

   SqlParameter[] para={
         new SqlParameter("@tablename",SqlDbType.VarChar,50),
    new SqlParameter("@where",SqlDbType.VarChar,50),
        };
   para[0].Value="Orders";
   para[1].Value="";
   foreach(SqlParameter paras in para)
   {
    cmd.Parameters.Add(paras);
   }
   cmd.Connection.Open();
   //cmd.ExecuteNonQuery();
   DataSet ds=new DataSet();
   SqlDataAdapter sda=new SqlDataAdapter(cmd);
   sda.Fill(ds);
   cmd.Connection.Close();
   return int.Parse(ds.Tables[0].Rows[0][0].ToString());
  }

  public void bindAutoPage(int pageIndex)
  {
   if(pageIndex==0)
   {
    Button1.Enabled=false;
    LinkButton1.Enabled=false;
   }
   else
   {
    Button1.Enabled=true;
    LinkButton1.Enabled=true;
   }
   try
   {
    if(pageIndex==int.Parse(lblPagecount.Text)-1)
    {
     Button2.Enabled=false;
     LinkButton2.Enabled=false;
    }
    else
    {
     Button2.Enabled=true;
     LinkButton2.Enabled=true;
    }
   }
   catch{}
   cmd=con.CreateCommand();
   cmd.Connection=con;
   cmd.CommandText="Test_autoPage";
   cmd.CommandType=CommandType.StoredProcedure;
   
   lblCur.Text=(pageIndex+1).ToString();
   SqlParameter[] para={
    new SqlParameter("@tablekey",SqlDbType.VarChar,20),
    new SqlParameter("@tablename",SqlDbType.VarChar,50),
    new SqlParameter("@pageindex",SqlDbType.Int,4),
    new SqlParameter("@pagesize",SqlDbType.Int,4),
    new SqlParameter("@where",SqlDbType.VarChar,500)
        };
   para[0].Value="OrderID";
   para[1].Value="Orders";
   para[2].Value=pageIndex;
   para[3].Value=PageCount;
   para[4].Value="";
   
   foreach(SqlParameter paras in para)
   {
    cmd.Parameters.Add(paras);
   }
   cmd.Connection.Open();
   //cmd.ExecuteNonQuery();
   DataSet ds=new DataSet();
   SqlDataAdapter sda=new SqlDataAdapter(cmd);
   sda.Fill(ds);
   DataGrid1.DataSource=ds.Tables[0].DefaultView;
   DataGrid1.DataBind();
   cmd.Connection.Close();
  }

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

  }
  #endregion

  private void LinkButton3_Click(object sender, System.EventArgs e)
  {
  
  }

  private void LinkButton1_Click(object sender, System.EventArgs e)
  {
   bindAutoPage(0);
   Feye();
  }

  private void LinkButton2_Click(object sender, System.EventArgs e)
  {
   bindAutoPage(int.Parse(lblPagecount.Text)-1);
   Feye();
  }

  private void Button1_Click(object sender, System.EventArgs e)
  {
   int pagecur=int.Parse(lblCur.Text);
   if(pagecur-1!=0)
   {
    bindAutoPage(pagecur-2);
   }
   else
   {
    bindAutoPage(0);
   }
   Feye();
  }

  private void Button2_Click(object sender, System.EventArgs e)
  {
   int pagecur=int.Parse(lblCur.Text);
   if(pagecur==int.Parse(lblPagecount.Text))
   {
    bindAutoPage(int.Parse(lblPagecount.Text));
   }
   else
   {
    bindAutoPage(pagecur);
   }
   Feye();
  }

  private void TextBox1_TextChanged(object sender, System.EventArgs e)
  {
   
   int pagecur=0;
   try
   {
    pagecur=int.Parse(TextBox1.Text);
    bindAutoPage(pagecur-1);
   Feye();
   }
   catch
   {
    Response.Write("<script>alert('请输入数字');</script>");
   }
   
  }
 }
}


HTML:
<%@ Page language="c#" Codebehind="TestFenye.aspx.cs" AutoEventWireup="false" Inherits="Webedtor.TestFenye" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
 <HEAD>
  <title>TestFenye</title>
  <meta content="Microsoft Visual Studio .NET 7.1" 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">
  <script language="javascript">
   var java="";
   function aaa(va)
   {
    java=va;
   }
   function bb()
   {
    alert(java);
   }
   
  </script>
 </HEAD>
 <body MS_POSITIONING="GridLayout">
  <form id="Form1" method="post" runat="server">
   <FONT face="宋体">
    <div>
     <div>共<asp:Label id="lblcount" runat="server"></asp:Label>条记录 当前<asp:Label id="lblCur" runat="server"></asp:Label>页
      共
      <asp:Label id="lblPagecount" runat="server"></asp:Label>页<INPUT type="button" onclick='aaa(document.getElementById("Ta").value)' value="Button"><INPUT type="button" value="Button" onclick="bb()"><input type=text id="Ta"></div>
     <div>
      <asp:LinkButton id="LinkButton1" runat="server">首页</asp:LinkButton><asp:Button id="Button1" runat="server" Width="32px" Text="<<"></asp:Button>
      <asp:PlaceHolder id="ph" runat="server"></asp:PlaceHolder>
      <asp:Button id="Button2" runat="server" Width="32px" Text=">>"></asp:Button>
      <asp:LinkButton id="LinkButton2" runat="server">尾页</asp:LinkButton>
      <asp:TextBox id="TextBox1" runat="server" Width="64px" AutoPostBack="True"></asp:TextBox>
     </div>
    </div>
    <asp:DataGrid id="DataGrid1" EnableViewState="False" style="Z-INDEX: 101; LEFT: 8px; POSITION: absolute; TOP: 80px"
     runat="server" Height="152px" Width="704px" BorderColor="#E7E7FF" BorderStyle="None" BorderWidth="1px"
     BackColor="White" CellPadding="3" GridLines="Horizontal" AutoGenerateColumns="False">
     <SelectedItemStyle Font-Bold="True" ForeColor="#F7F7F7" BackColor="#738A9C"></SelectedItemStyle>
     <AlternatingItemStyle BackColor="#F7F7F7"></AlternatingItemStyle>
     <ItemStyle ForeColor="#4A3C8C" BackColor="#E7E7FF"></ItemStyle>
     <HeaderStyle Font-Bold="True" ForeColor="#F7F7F7" BackColor="#4A3C8C"></HeaderStyle>
     <FooterStyle ForeColor="#4A3C8C" BackColor="#B5C7DE"></FooterStyle>
     <Columns>
      <asp:BoundColumn DataField="ORDERID" HeaderText="ORDERID"></asp:BoundColumn>
      <asp:BoundColumn DataField="CustomerID" HeaderText="CustomerID"></asp:BoundColumn>
      <asp:BoundColumn DataField="employeeID" HeaderText="employeeID"></asp:BoundColumn>
      <asp:BoundColumn DataField="shipname" HeaderText="shipname"></asp:BoundColumn>
      <asp:BoundColumn DataField="shipaddress" HeaderText="shipaddress"></asp:BoundColumn>
     </Columns>
     <PagerStyle HorizontalAlign="Right" ForeColor="#4A3C8C" BackColor="#E7E7FF" Mode="NumericPages"></PagerStyle>
    </asp:DataGrid></FONT></form>
 </body>
</HTML>


存储过程:
CREATE   proc Test_autoPage
@tablekey varchar(100),
@tablename varchar(100),
@pageindex int,
@pagesize int,
@where varchar(5000)
as
declare @sql varchar(8000)

set @sql='select top '+str(@pagesize)+' * from '+@tablename+ ' where 1=1 ' +@where+
 'and '+@tablekey+' not in (select top '+str(@pageindex*@pagesize)+' '+@tablekey+' from '+
@tablename  +' where 1=1 '+@where+' order by '+@tablekey+') order by '+@tablekey

set @sql=@sql+' ; select count(*) from '+@tablename+' where 1=1 '+@where

exec (@sql)
GO


create  proc test_getCount
@tablename varchar(100),
@where varchar(2000)
as
declare @sql varchar(5000)

set @sql=' select count(*) from '+@tablename+' where 1=1 '+@where

exec (@sql)


我写程序没有做优化的。嘿嘿,有兴趣的自己去优化吧 。。

你可能感兴趣的:(分页)