收藏:又改好了一个DataList分页

转载 2004年09月10日 17:13:00

和我改的DataGrid分页一样,加了下拉框选择,这次没费多少功夫,有进步,有进步……

源代码如下(请修改数据库链接、表名、字段名):

<%@ Page Language="C#" ContentType="text/html" ResponseEncoding="gb2312" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<Script Language="C#" Runat="Server">
int PageSize,RecordCount,PageCount,CurrentPage;
SqlConnection myConnection;
public void Page_Load(Object src,EventArgs e)
 {
   PageSize = 10;
  ConnOpen();
  if(!Page.IsPostBack)
   {
    DataBind();
    CurrentPage = 0;
    ViewState["PageIndex"] = 0;
    //计算总共有多少记录
    RecordCount = CalculateRecord();
    lblRecordCount.Text = RecordCount.ToString();
    //计算总共有多少页
    PageCount = RecordCount/PageSize;
    lblPageCount.Text = PageCount.ToString();
    ViewState["PageCount"] = PageCount;
    DropDown();
   }
 }

private void ConnOpen()
 {
  string strConnection="Data Source=localhost;Initial Catalog=mydata;Persist Security Info=True;User ID=sa;Password=";
  myConnection = new SqlConnection(strConnection);
  myConnection.Open();
 }

private void DataBind()
  {
  int StartIndex;
  //设定导入的起终地址
  StartIndex = CurrentPage*PageSize;
  string strSel = "select * from news order by id desc";
  DataSet ds = new DataSet();
  SqlDataAdapter MyAdapter = new SqlDataAdapter(strSel,myConnection);
  MyAdapter.Fill(ds,StartIndex,PageSize,"news");
  MyDataGrid.DataSource = ds.Tables["news"].DefaultView;
   MyDataGrid.DataBind();
  lbnFirstPage.Enabled = true;
  lbnNextPage.Enabled = true;
  lbnPrevPage.Enabled = true;
  lbnLastPage.Enabled = true;
  if(CurrentPage==(PageCount-1))
   {
    lbnLastPage.Enabled = false;
    lbnNextPage.Enabled = false;
   }
  if(CurrentPage==0)
   {
    lbnFirstPage.Enabled = false;
    lbnPrevPage.Enabled = false;
   }
  lblCurrentPage.Text = (CurrentPage+1).ToString();
  ViewState["PageIndex"]=CurrentPage;
 }
 
public int CalculateRecord()
 {
  int intCount;
  string strCount = "select count(*) as co from news";
  SqlCommand MyComm = new SqlCommand(strCount,myConnection);
  SqlDataReader dr = MyComm.ExecuteReader();
  if(dr.Read())
   {
     intCount = Int32.Parse(dr["co"].ToString());
   }
  else
   {
    intCount = 0;
   }
  dr.Close();
  return intCount;
 }

public void Page_OnClick(Object sender,CommandEventArgs e)
{
 CurrentPage = (int)ViewState["PageIndex"];
 PageCount = (int)ViewState["PageCount"];

 string cmd = e.CommandName;
 //判断cmd,以判定翻页方向
 switch(cmd)
 {
  case "next":
   if(CurrentPage<(PageCount-1)) CurrentPage++;
   break;
  case "prev":
   if(CurrentPage>0) CurrentPage--;
   break;
  case "last":
   CurrentPage=PageCount-1;
   break;
  default:
  CurrentPage = System.Convert.ToInt32(cmd);
   break;
  }
  ViewState["PageIndex"] = CurrentPage;
  myDropDownList.SelectedIndex=CurrentPage;
  DataBind();
 }

private void DropDown()
  {
  for(int u=0;u<PageCount;u++)
   {
    myDropDownList.Items.Add(new ListItem("第"+(u+1).ToString()+"页",Convert.ToString(u)));
   }   
  }

private void listchanged(object sender, System.EventArgs e)
 {
  CurrentPage=myDropDownList.SelectedIndex;
  DataBind();
 }

</script>
<html>
<head>
<title></title>
</head>
<body>
<form runat="server">

<asp:DataList id="MyDataGrid" runat="server"
HeaderStyle-BackColor="#aaaadd"
AlternatingItemStyle-BackColor="Gainsboro"
EditItemStyle-BackColor="yellow"
>
 <ItemTemplate>
  姓名:<%# DataBinder.Eval(Container.DataItem,"title") %>
 </ItemTemplate>
</asp:DataList>
共有<asp:Label id="lblRecordCount" ForeColor="red" runat="server" />条记录&nbsp;
当前为<asp:Label id="lblCurrentPage" ForeColor="red" runat="server" />/<asp:Label id="lblPageCount" ForeColor="red" runat="server" />页&nbsp;
<asp:LinkButton id="lbnFirstPage" Text="首 页" CommandName="0" OnCommand="Page_OnClick" runat="server" />
<asp:LinkButton id="lbnPrevPage" Text="上一页" CommandName="prev" OnCommand="Page_OnClick" runat="server" />
<asp:LinkButton id="lbnNextPage" Text="下一页" CommandName="next" OnCommand="Page_OnClick" runat="server" />
<asp:LinkButton id="lbnLastPage" Text="末 页" CommandName="last" OnCommand="Page_OnClick" runat="server" />
<asp:DropDownList ID="myDropDownList" runat="server" AutoPostBack="true" OnSelectedIndexChanged="listchanged">
</asp:DropDownList>
</form>
</body>
</html>

Datalist控件实现分页功能

在.aspx页面里的代码 ...
  • fengqiyunran
  • fengqiyunran
  • 2009-07-22 21:43:00
  • 1682

实现DataList分页方法

前几天在做网上商城,要展示商品信息(有图片,有文字),DataView虽然可以分页,但它的缺点是不能自定义显示格式。而DataList解决了它的缺点,但DataList本身却不能分页。很是头痛,于是在...
  • mane_yao
  • mane_yao
  • 2010-03-22 10:56:00
  • 13161

如何让DataList分页功能[C#](转载)

一.前言使用asp.net 的Gridview控件,你可以方便地进行数据绑定、分页显示,模板能让数据显示更加多姿多彩,但是Gridview显示只能按记录一行一行垂直显示,不能横排,它更多地用来显示普通...
  • xinghongyang
  • xinghongyang
  • 2008-05-20 15:10:00
  • 642

DataList控件也玩分页(vb)

   今天开发了一个系统。需要用多一行多列去展示图片。查找了一下DATAGIRD的属性。发现其没有重复列的功能。于是我使用了datalist的RepeatColumns="4"列重复功能,却又发现DA...
  • payunshangdezhu
  • payunshangdezhu
  • 2005-01-13 22:54:00
  • 1275

如何让DataList具有分页的功能(asp.net)

一.前言使用asp.net 的Gridview控件,你可以方便地进行数据绑定、分页显示,模板能让数据显示更加多姿多彩,但是Gridview显示只能按记录一行一行垂直显示,不能横排,它更多地用来显示普通...
  • zhyuanshan
  • zhyuanshan
  • 2007-10-30 00:03:00
  • 8761

C# ASP.NET AspNetPager对DataList控件分页

1、控件代码段DataList数据绑定 < asp:DataList ID=”DataList1” runat=”server” RepeatColumns=”2” RepeatDirection=...
  • u010075060
  • u010075060
  • 2015-04-19 21:43:57
  • 672

datalist的简单分页

  • 2013年08月19日 20:28
  • 2KB
  • 下载

ASP.NET中对DataList控件进行数据绑定并分页

  • DonetRen
  • DonetRen
  • 2010-05-19 18:35:00
  • 2950

DataList 增删改查、分页

前端展示如下:
  • li185416672
  • li185416672
  • 2014-11-23 16:02:06
  • 2020

asp.net DataList控件分页代码

  • taomanman
  • taomanman
  • 2011-11-18 16:39:35
  • 1666
收藏助手
不良信息举报
您举报文章:收藏:又改好了一个DataList分页
举报原因:
原因补充:

(最多只允许输入30个字)