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

和我改的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>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值