代码如下:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Data.SqlTypes;
public partial class Information_Group : System.Web.UI.Page
{
SqlConnection MyConn;
int PageSize, RecordCount, PageCount, CurrentPage;
public string Cut(object obj)//切割字符串方法
{
string str = Convert.ToString(obj);
if (str.Length > 10)
{
str = str.Substring(0, 8) + "...";
}
return str;
}
protected void Page_Load(object sender, EventArgs e)//读取DataList1中的所有内容
{
PageSize=3;//该页显示信息的数量
string str = ConfigurationSettings.AppSettings["sql"].ToString();
SqlConnection con = new SqlConnection(str);
con.Open();
if(!IsPostBack)//第一次请求执行
{
ListBind();
CurrentPage=0;
ViewState["PageIndex"]=0;
//计算有多少记录
RecordCount=CalculateRecord();
lblRecordCount.Text=RecordCount.ToString();
//计算总共有多少页
PageCount = RecordCount / PageSize;
lblPageCount.Text= PageCount.ToString();
ViewState["PageCount"] = PageCount;
}
}
public int CalculateRecord()//计算一共有多少条记录
{
int intCount;
//string strCount="select count(DF_GROUP_PERSON_ID) from DF_GROUP_PERSON";
SqlCommand com=new SqlCommand("S_DF_Select_COUNT_DF_GROUP_PERSON_ID",MyConn);
com.CommandType=CommandType.StoredProcedure;
SqlDataReader ccc=com.ExecuteReader();
if(ccc.Read())
{
intCount=Int32.Parse(ccc["co"].ToString());
}
else
{
intCount=0;
}
ccc.Close();
return intCount;
}
ICollection CreateSource()
{
int StartIndex;//设定导入的起终位置
StartIndex = CurrentPage * PageSize;
SqlDataAdapter MyAdapter = new SqlDataAdapter("S_DF_Select_DF_GROUP_PERSON",MyConn);
MyAdapter.SelectCommand.CommandType = CommandType.StoredProcedure;
DataSet ds = new DataSet();
MyAdapter.Fill(ds,StartIndex,PageSize,"score");
return ds.Tables["score"].DefaultView;
}
public void ListBind()
{
this.score.DataSource= CreateSource();
this.score.DataBind();
lbnNextPage.Enabled = true;
lbnPrevPage.Enabled = true;
if (CurrentPage == (PageCount - 1)) lbnNextPage.Enabled = false;
if (CurrentPage == 0) lbnPrevPage.Enabled = false;
lblCurrentPage.Text = (CurrentPage + 1).ToString();
}
protected void Button1_Click(object sender, EventArgs e)
{
}
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;
ViewState["PageIndex"] = CurrentPage;
ListBind();
}
}
}
< %@ Page Language="C#" AutoEventWireup="true" CodeFile="Information_Group.aspx.cs" Inherits="Information_Group" %>
< !DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
< html xmlns="http://www.w3.org/1999/xhtml" >
< head runat="server">
<title>FLYSO Blog--CS团队展示 </title>
< /head>
< body>
<form id="form1" runat="server">
<div>
</div>
<br />
<br />
<br />
<br />
<asp:DataList ID="score" runat="server" GridLines="Vertical">
<ItemTemplate>
<table>
<tr>
<td style="width: 100px">
<asp:Label ID="Label4" runat="server" Text=' <%#DataBinder.Eval(Container,"DataItem.DF_GROUP_PERSON_NAME") %>'> </asp:Label> </td>
<td style="width: 100px">
<asp:Label ID="Label5" runat="server" Text=' <%# Cut(DataBinder.Eval(Container,"DataItem.DF_GROUP_PERSON_EXPERIENCE")) %>'> </asp:Label> </td>
<td style="width: 100px">
<asp:Label ID="Label6" runat="server" Text="Label"> </asp:Label> </td>
</tr>
</table>
</ItemTemplate>
<EditItemTemplate>
<table style="width: 350px; height: 32px">
<tr>
<td style="width: 88px; height: 26px">
<asp:TextBox ID="TextBox1" runat="server"> </asp:TextBox> </td>
<td style="width: 100px; height: 26px">
<asp:TextBox ID="TextBox2" runat="server" Width="85px"> </asp:TextBox> </td>
<td style="width: 100px; height: 26px">
<asp:TextBox ID="TextBox3" runat="server"> </asp:TextBox> </td>
</tr>
</table>
</EditItemTemplate>
<HeaderTemplate>
<table>
<tr>
<td style="width: 100px">
<asp:Label ID="Label1" runat="server" Text="标题"> </asp:Label> </td>
<td style="width: 100px">
<asp:Label ID="Label2" runat="server" Text="内容"> </asp:Label> </td>
<td style="width: 100px">
<asp:Label ID="Label3" runat="server" Text="时间"> </asp:Label> </td>
</tr>
</table>
</HeaderTemplate>
</asp:DataList>
<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> <br />
<br />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" /> <br />
<br />
<asp:Label ID="lblPageCount" runat="server" Text="Label"> </asp:Label> <br />
<asp:Label ID="lblCurrentPage" runat="server" Text="Label"> </asp:Label> <br />
<asp:Label ID="lblRecordCount" runat="server" Text="Label"> </asp:Label> <br />
<br />
</form>
< /body>
< /html>
补充:
下列代码除了page_load事件以外
string str = ConfigurationSettings.AppSettings["sql"].ToString();
SqlConnection con = new SqlConnection(str);
con.Open();
每个事件里都要写一下,最好的方法是写一个方法然后哪个事件有用到这个方法就去调用一下,con用完之后记得con.close()一下