以前做过一个大型的网络社区,不过在数据的在分页上下的功夫并不多,功能是实现了,实际效果也还可以,现在传上来,一来供各位参考,二呢自己做个记录,以便以后用得到。
/// <summary>
/// 功能简介:博客相册
/// 作者:冷雨箫声
/// 最后修改时间:2008-1-27
/// </summary>
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 WeYyzyq.Comp;
using WeYyzyq.Logic;
public partial class School_Hospital_MyPlacePhotos : System.Web.UI.Page
{
string strUserID = null;
Int64 BlogID;
DataTable dtPhoto = new DataTable();
MyPlace mp = new MyPlace();
int CurrentPage;//当前页数
private const int PageSize = 16; //每页条数
int PageCount; //总页数
int RecordCount;//总条数
int StartIndex;
protected void Page_Load(object sender, EventArgs e)
{
strUserID = Request.QueryString["UserID"].ToString().Trim();
BlogID = Convert.ToInt64(mp.GetBlogIDByUserID(strUserID));
if (!IsPostBack)
{
DataListPaging();
ShowSpaceUsed();
BindlogClass();
UserOrGuest();
}
}
private void DataListPaging()
{
CurrentPage = 0;//当前页习惯设为0
ViewState["PageIndex"] = 0;//页索引也设为0
StartIndex = CurrentPage * PageSize;
dtPhoto.Clear();
dtPhoto = mp.SelectPhotos(strUserID, null, StartIndex, PageSize, null);
RecordCount = dtPhoto.Rows.Count;
if (RecordCount / PageSize == 0)
{
if (RecordCount < PageSize)
{
PageCount = 1;
}
else
{
PageCount = RecordCount / PageSize;
}
}
else
{
PageCount = RecordCount / PageSize + 1;
}
TotalLbl.Text = "共" + PageCount.ToString() + "页";//显示总页数
ViewState["PageCount"] = PageCount;//
BindPhotos(null, null);
}
public void LinkButton_Click(Object sender, CommandEventArgs e)//自己编写的按钮点击事件
{
CurrentPage = (int)ViewState["PageIndex"];//获得当前页索引
PageCount = (int)ViewState["PageCount"];//获得总页数
string cmd = e.CommandName;
//判断cmd,以判定翻页方向
switch (cmd)
{
case "prev"://上一页
if (CurrentPage > 0) CurrentPage--;
break;
case "next":
if (CurrentPage < (PageCount - 1)) CurrentPage++;//下一页
break;
case "first"://第一页
CurrentPage = 0;
break;
case "end"://最后一页
CurrentPage = PageCount - 1;
break;
case "jump"://跳转到第几页
if (this.TextBox1.Text.Trim() == "" || Int32.Parse(this.TextBox1.Text.Trim()) > PageCount)//如果输入数字为空或超出范围则返回
{
return;
}
else
{
CurrentPage = Int32.Parse(this.TextBox1.Text.ToString()) - 1;
break;
}
}
ViewState["PageIndex"] = CurrentPage;//获得当前页
BindPhotos(null, null);
}
private void BindPhotos(string strOrder,string strClassname)
{
StartIndex = CurrentPage * PageSize;//设定导入的起终地址
dtPhoto.Clear();
dtPhoto = mp.SelectPhotos(strUserID, strOrder, StartIndex, PageSize, strClassname);
try
{
dlPhotos.DataSource = dtPhoto;
dlPhotos.DataBind();
this.PreviousLB.Enabled = true;
this.NextLB.Enabled = true;
if (CurrentPage == (PageCount - 1)) this.NextLB.Enabled = false;//当为最后一页时,下一页链接按钮不可用
if (CurrentPage == 0) this.PreviousLB.Enabled = false;//当为第一页时,上一页按钮不可用
this.CurrentLbl.Text = "当前第" + (CurrentPage + 1).ToString() + "页 跳到";//当前页数
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
public string GetUserId
{
#region
get
{
return (HttpContext.Current.Request.Cookies["userid"] == null) ? null : HttpContext.Current.Request.Cookies["userid"].Value;
}
#endregion
}
private void ShowSpaceUsed()
{
string dSize = mp.CountSpaceSize(strUserID);
if (dSize == "")
{
lblSize.Text = "空间使用情况:0MB/100MB|使用率为0%";
}
else
{//decimal.Round(Convert.ToDecimal(dSize), 2)
if (Convert.ToDecimal(dSize) >= 100)
{
lblSize.Text = "您的空间(100MB)已经用完,请自行维护使用情况!";
imgUpPhoto.Enabled = false;
}
else
{
string strSize = Convert.ToString((decimal.Round(Convert.ToDecimal(dSize), 2)));
lblSize.Text = "空间使用情况:" + strSize + "MB/100MB|使用率为" + strSize + "%";
}
}
}
/// <summary>
/// 为用户或者游客分配权限
/// </summary>
private void UserOrGuest()
{
if (strUserID == GetUserId)
{
imgUpPhoto.Visible = true;
pnlclass.Visible = true;
}
else
{
imgUpPhoto.Visible = false;
pnlclass.Visible = false;
}
}
private void BindlogClass()
{
dtPhoto.Clear();
dtPhoto = mp.BindlogClass(strUserID, 1);
if (dtPhoto.Rows.Count != 0)
{
rbClassList.DataSource = dtPhoto;
rbClassList.DataTextField = "BClass_Name";
rbClassList.DataBind();
}
gvRZClass.DataSource = dtPhoto;
gvRZClass.DataBind();
}
protected void btnAddClass_Click(object sender, EventArgs e)
{
bool bResult = mp.AddRZClass(BlogID, txtClassName.Text, 1);
if (bResult == true)
{
lblWarning.Visible = false;
BindlogClass();
txtClassName.Text = "";
}
else
{
lblWarning.Visible = true;
lblWarning.Text = "分类添加不成功";
return;
}
}
protected void gvRZClass_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
bool bExist = mp.ValDataExist(Convert.ToString(gvRZClass.Rows[e.RowIndex].Cells[1].Text).Trim(), BlogID);
if (bExist == true)
{
Int64 ClassID = Convert.ToInt64(gvRZClass.Rows[e.RowIndex].Cells[0].Text);
bool bResult = mp.DeleteRZClassByID(ClassID);
if (bResult == true)
{
lblDelete.Visible = false;
BindlogClass();
}
else
{
lblDelete.Visible = true;
lblDelete.Text = "删除不成功";
return;
}
}
else
{
lblDelete.Visible = true;
lblDelete.Text = "该类型暂时不可删除!";
return;
}
}
protected void lbMain_Click(object sender, EventArgs e)
{
Response.Redirect("MyPlace.aspx?UserID=" + GetUserId, false);
}
protected void lbLog_Click(object sender, EventArgs e)
{
Response.Redirect("MyPlaceLog.aspx?UserID=" + GetUserId, false);
}
protected void lbPhoto_Click(object sender, EventArgs e)
{
}
protected void lbPost_Click(object sender, EventArgs e)
{
Response.Redirect("../MyPlacePost.aspx?UserID=" + GetUserId, false);
}
protected void lbAbout_Click(object sender, EventArgs e)
{
Response.Redirect("MyPlaceDoc.aspx?UserID=" + GetUserId, false);
}
protected void lbAllRZ_Click(object sender, EventArgs e)
{
BindPhotos(null, null);
rbSort.Visible = true;
}
protected void rbClassList_SelectedIndexChanged(object sender, EventArgs e)
{
StartIndex = CurrentPage * PageSize;//设定导入的起终地址
dtPhoto.Clear();
dtPhoto = mp.SelectPhotos(strUserID, null, StartIndex, PageSize, rbClassList.SelectedItem.Text.Trim());
if (dtPhoto.Rows.Count != 0)
{
rbSort.Visible = false;
try
{
dlPhotos.DataSource = dtPhoto;
dlPhotos.DataBind();
this.PreviousLB.Enabled = true;
this.NextLB.Enabled = true;
if (CurrentPage == (PageCount - 1)) this.NextLB.Enabled = false;//当为最后一页时,下一页链接按钮不可用
if (CurrentPage == 0) this.PreviousLB.Enabled = false;//当为第一页时,上一页按钮不可用
this.CurrentLbl.Text = "当前第" + (CurrentPage + 1).ToString() + "页 跳到";//当前页数
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
else
{
dlPhotos.DataSource = null;
dlPhotos.DataBind();
lblRZWarning.Visible = true;
rbSort.Visible = true;
lblRZWarning.Text = "该分类没有照片!";
}
}
protected void imgUpPhoto_Click(object sender, ImageClickEventArgs e)//BlogID
{
Response.Redirect("MyPlaceUpPhoto.aspx?BlogID=" + BlogID, false);
}
protected void rbSort_SelectedIndexChanged(object sender, EventArgs e)
{
BindPhotos(rbSort.SelectedValue.ToString().Trim(), null);
}
}