datalist控件分页,外观超过网上流行的飞刀的效果

 其实我是使用了PlaceHolder动态加入LinkButton,DbDataAdapter.Fill (DataSet, Int32, Int32, String) 的方法来实现,代码如下:
当然从性能上来说这样做并不一定最好,但是方便我认为是最重要的。
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;
public partial class about_morephoto : System.Web.UI.Page
{

protected void Page_Load(object sender, EventArgs e)
{

if (!Page.IsPostBack)
{
ViewState["pageindex"] = 0;
databind();
int PageCount,PageSize;
PageSize=10;
SqlConnection mycon = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["001eduConnectionString"].ConnectionString);
SqlCommand mycom = new SqlCommand("select count(*) from [membertupian]", mycon);
mycon.Open();
double dPageCount = Convert.ToDouble(mycom.ExecuteScalar()) / PageSize;
PageCount = (int)Math.Ceiling(dPageCount);
mycon.Close();
ViewState["PageCount"] = PageCount;

}
LinkButton Button1;

for (int inti = 1; inti <= (int)ViewState["PageCount"]; inti++)
{
Button1 = new LinkButton();
Button1.Width = 10;
Button1.Text = inti.ToString();
PlaceHolder1.Controls.Add(Button1);
Button1.Click += new EventHandler(LinkButton1_Click);
Button1.ForeColor = LinkButton1.ForeColor;
Button1.Font.Underline = LinkButton1.Font.Underline;
}


}

protected void databind()
{
int PageSize,CurrentPage;
PageSize = 10;
SqlConnection mycon = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["001eduConnectionString"].ConnectionString);
SqlDataAdapter mycom = new SqlDataAdapter("Select [descript], [id], [fileurl] FROM [membertupian] orDER BY [id] DESC", mycon);
DataSet ds = new DataSet();
CurrentPage = (int)ViewState["pageindex"];
mycon.Open();
mycom.Fill(ds, CurrentPage * PageSize, PageSize, "membertupian");
mycon.Close();
DataList1.DataSource = ds;
DataList1.DataBind();
}

protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
if ((int)ViewState["pageindex"] > 0)
{
ViewState["pageindex"] = (int)ViewState["pageindex"] - 1;
databind();
}


}
protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
{
if ((int)ViewState["pageindex"] < (int)ViewState["PageCount"]-1)
{
ViewState["pageindex"] = (int)ViewState["pageindex"] + 1;
databind();
}

}
protected void LinkButton1_Click(object sender, EventArgs e)
{
ViewState["pageindex"] = Convert.ToInt32(((LinkButton)sender).Text) - 1;
databind();
((LinkButton)sender).ForeColor = System.Drawing.Color.FromName("OrangeRed");
((LinkButton)sender).Font.Underline = true;
}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值