Datalist分页代码

来源:互联网 发布:淘宝信誉排行榜 编辑:程序博客网 时间:2024/06/07 19:20
<%@ Page Language="c#" ResponseEncoding="gb2312" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<Script Language="C#" runat="server">
    
int PageSize;
    
int RecordCoutn;
    
int pageCount;
    
int CurrentPage;
    SqlConnection conn;
    
void Page_Load(object sender, EventArgs e)
    
{
        connOpen();
        PageSize 
= 2;
        
if (!IsPostBack)
        
{
            mathBd();
            CurrentPage 
= 0;
            RecordCoutn
=CalculateRecord();
            
this.lblRecordCount.Text=RecordCoutn.ToString();
            pageCount 
= RecordCoutn / PageSize;
            
this.lblPageCount.Text = pageCount.ToString();
            ViewState[
"PageIndex"= 0;
            ViewState[
"PageCount"= pageCount;
            DropDown();

        }

    }

    
private void connOpen()
    
{
        conn 
= new SqlConnection("server=ss/sqlexpress;database=Northwind;uid=sa;pwd=;");
        conn.Open(); 
    }

    
private void mathBd()
    
{
        
int startIndex;
        startIndex
=PageSize*CurrentPage;
        SqlDataAdapter sdr
=new SqlDataAdapter ("select * from Employees",conn);
        DataSet ds
=new DataSet ();
        sdr.Fill(ds,startIndex,PageSize,
"Employees");
        
this.MyDataGrid.DataSource=ds.Tables["Employees"].DefaultView;
        
this.MyDataGrid.DataBind();

        lbnFirstPage.Enabled 
= true;
        lbnNextPage.Enabled 
= true;
        lbnPrevPage.Enabled 
= true;
        lbnLastPage.Enabled 
= true;
        
if (CurrentPage == (pageCount-1))
        
{
            lbnNextPage.Enabled 
= false;
            lbnLastPage.Enabled 
= false;
        }

        
if (CurrentPage == 0)
        
{

            lbnFirstPage.Enabled 
= false;
            lbnPrevPage.Enabled 
= false;
        }

        
this.Label2.Text = (CurrentPage + 1).ToString();
        ViewState[
"PageIndex"= CurrentPage;
    }

    
public  int CalculateRecord()
    
{
        
int intRecord;
        
string strComm = "select count (*) as co from Employees";
        SqlCommand comm 
= new SqlCommand(strComm, conn);
        SqlDataReader dr 
= comm.ExecuteReader();
        
if (dr.Read())
        
{
            intRecord 
= Convert.ToInt32(dr["co"].ToString());
        }

        
else
        
{
            intRecord 
= 0
        }

        dr.Close();
        
return intRecord;
    }

    
private void Page_OnClick(object sender, CommandEventArgs e)
    
{
        CurrentPage 
=(int) ViewState["PageIndex"];
        pageCount
=(int)ViewState["PageCount"];
        
string cmd = e.CommandName;
        
switch (cmd)
        
{
            
case "next":
                
if (CurrentPage < (pageCount-1)) CurrentPage++break;
            
case"prev":
                
if (CurrentPage > 0) CurrentPage--break;
            
case"last":
                CurrentPage 
= pageCount - 1break;
            
default:
                CurrentPage 
= System.Convert.ToInt32(cmd); break;
        }

        ViewState[
"PageIndex"= CurrentPage;
        
//myDropDownList.SelectedIndex = CurrentPage;
        mathBd();
    }

    
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, EventArgs e)
    
{
        CurrentPage 
= myDropDownList.SelectedIndex;
        mathBd(); 
    }

</Script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Datalist 分页</title>
</head>
<body>
<form id="Form1" runat="server">

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

</form>
</body>
</html>

 
原创粉丝点击