datalist实现类似百度分页(有点不明显的小毛病&&等待解决)
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 bbs_photo : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
publicclass.pageintex = 0;
datelistbind();
}
}
protected void datelistbind()
{
string sqlcomm = "select * from UF_PICTURE";
SqlConnection myconn = new SqlConnection(publicclass.getconnstr());
myconn.Open();
SqlDataAdapter sdr = new SqlDataAdapter();
DataSet ds = new DataSet();
sdr.SelectCommand = new SqlCommand(sqlcomm, myconn);
sdr.Fill(ds, "UF_PICTURE");
PagedDataSource pds = new PagedDataSource(); //分页数据源
pds.DataSource = ds.Tables["UF_PICTURE"].DefaultView;
pds.AllowPaging = true;
pds.PageSize = 4;
pds.CurrentPageIndex = publicclass.pageintex;
Response.Write(publicclass.pageintex);
publicclass.count = pds.PageCount;
DataList1.DataSource = pds;
DataList1.DataBind();
Response.Write(publicclass.count);
}
protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Header)
{
Control objectcontorl5 = e.Item.FindControl("LinkButton5");
Control objectcontorl6 = e.Item.FindControl("LinkButton6");
Control objectcontorl9 = e.Item.FindControl("LinkButton9");
Control objectcontorl10 = e.Item.FindControl("LinkButton10");
Control objectcontorl11 = e.Item.FindControl("LinkButton11");
Control objectcontorl7 = e.Item.FindControl("LinkButton7");
Control objectcontorl8 = e.Item.FindControl("LinkButton8");
LinkButton bt5 = (LinkButton)objectcontorl5;
LinkButton bt6 = (LinkButton)objectcontorl6;
LinkButton bt9 = (LinkButton)objectcontorl9;
LinkButton bt10 = (LinkButton)objectcontorl10;
LinkButton bt11 = (LinkButton)objectcontorl11;
LinkButton bt7 = (LinkButton)objectcontorl7;
LinkButton bt8 = (LinkButton)objectcontorl8;
publicclass.yi1 = Convert.ToInt32(bt9.Text) -1;
publicclass.er1 = Convert.ToInt32(bt10.Text) -1;
publicclass.san1 = Convert.ToInt32(bt11.Text) -1;
if (publicclass.pageintex <= 1)
{
bt9.Text = "1";
bt10.Text = "2";
bt11.Text = "3";
}
else if (publicclass.pageintex >= publicclass.count - 2)
{
bt9.Text = Convert.ToString(publicclass.count - 2);
bt10.Text = Convert.ToString(publicclass.count - 1);
bt11.Text = Convert.ToString(publicclass.count);
}
else
{
bt9.Text = Convert.ToString(publicclass.pageintex - 1);
bt10.Text = Convert.ToString(publicclass.pageintex);
bt11.Text = Convert.ToString(publicclass.pageintex + 1);
}
if (publicclass.pageintex == 0)
{
bt5.Enabled = false;
bt6.Enabled = false;
}
if (publicclass.pageintex == publicclass.count)
{
bt7.Enabled = false;
bt8.Enabled = false;
}
}
if (e.Item.ItemType == ListItemType.Footer)
{
Control objectcontorl12 = e.Item.FindControl("LinkButton12");
Control objectcontorl13 = e.Item.FindControl("LinkButton13");
Control objectcontorl14 = e.Item.FindControl("LinkButton14");
Control objectcontorl15 = e.Item.FindControl("LinkButton15");
Control objectcontorl16 = e.Item.FindControl("LinkButton16");
Control objectcontorl17 = e.Item.FindControl("LinkButton17");
Control objectcontorl18 = e.Item.FindControl("LinkButton18");
LinkButton bt12 = (LinkButton)objectcontorl12;
LinkButton bt13 = (LinkButton)objectcontorl13;
LinkButton bt14 = (LinkButton)objectcontorl14;
LinkButton bt15 = (LinkButton)objectcontorl15;
LinkButton bt16 = (LinkButton)objectcontorl16;
LinkButton bt17 = (LinkButton)objectcontorl17;
LinkButton bt18 = (LinkButton)objectcontorl18;
publicclass.yi2 = Convert.ToInt32(bt14.Text) - 1;
publicclass.er2 = Convert.ToInt32(bt15.Text) - 1;
publicclass.san2 = Convert.ToInt32(bt16.Text) - 1;
if (publicclass.pageintex <= 1)
{
bt14.Text = "1";
bt15.Text = "2";
bt16.Text = "3";
}
else if (publicclass.pageintex >= publicclass.count - 2)
{
bt14.Text = Convert.ToString(publicclass.count - 2);
bt15.Text = Convert.ToString(publicclass.count - 1);
bt16.Text = Convert.ToString(publicclass.count);
}
else
{
bt14.Text = Convert.ToString(publicclass.pageintex - 1);
bt15.Text = Convert.ToString(publicclass.pageintex);
bt16.Text = Convert.ToString(publicclass.pageintex + 1);
}
if (publicclass.pageintex == 0)
{
bt12.Enabled = false;
bt13.Enabled = false;
}
if (publicclass.pageintex == publicclass.count)
{
bt17.Enabled = false;
bt18.Enabled = false;
}
}
}
protected void LinkButton5_Click(object sender, EventArgs e)
{
publicclass.pageintex = 0;
datelistbind();
}
protected void LinkButton6_Click(object sender, EventArgs e)
{
publicclass.pageintex = 0;
datelistbind();
}
protected void LinkButton9_Click(object sender, EventArgs e)
{
publicclass.pageintex = Convert.ToInt32(publicclass.yi1);
datelistbind();
}
protected void LinkButton10_Click(object sender, EventArgs e)
{
publicclass.pageintex = Convert.ToInt32(publicclass.er1);
datelistbind();
}
protected void LinkButton11_Click(object sender, EventArgs e)
{
publicclass.pageintex = Convert.ToInt32(publicclass.san1);
datelistbind();
}
protected void LinkButton7_Click(object sender, EventArgs e)
{
publicclass.pageintex = publicclass.pageintex + 1;
datelistbind();
}
protected void LinkButton8_Click(object sender, EventArgs e)
{
publicclass.pageintex = publicclass.count;
datelistbind();
}
protected void LinkButton12_Click(object sender, EventArgs e)
{
publicclass.pageintex = 0;
datelistbind();
}
protected void LinkButton13_Click(object sender, EventArgs e)
{
publicclass.pageintex = publicclass.pageintex - 1;
datelistbind();
}
protected void LinkButton14_Click(object sender, EventArgs e)
{
publicclass.pageintex = Convert.ToInt32(publicclass.yi2);
datelistbind();
}
protected void LinkButton15_Click(object sender, EventArgs e)
{
publicclass.pageintex = Convert.ToInt32(publicclass.er2);
datelistbind();
}
protected void LinkButton16_Click(object sender, EventArgs e)
{
publicclass.pageintex = Convert.ToInt32(publicclass.san2);
datelistbind();
}
protected void LinkButton17_Click(object sender, EventArgs e)
{
publicclass.pageintex = publicclass.pageintex + 1;
datelistbind();
}
protected void LinkButton18_Click(object sender, EventArgs e)
{
publicclass.pageintex = publicclass.count;
datelistbind();
}
}
//前台代码
--------------------------------------------------------
<asp:DataList ID="DataList1" runat="server" HorizontalAlign="Center" RepeatColumns="2">
<ItemTemplate>
<div class="right_tu_1 bor_r">
<h3>效果图12</h3>
<span> <asp:Image ID="Image1" runat="server" Height="160px" Width="258px" ImageUrl="~/"/><%# Eval("UF_PICTURE_ADDRESS")%><label>上 传 者:<asp:LinkButton ID="LinkButton2" runat="server"><%# Eval("UF_PICTURE_USER")%></asp:LinkButton>
上传时间:<asp:LinkButton ID="LinkButton3" runat="server"><%# Eval("UF_PICTURE_TIME")%></asp:LinkButton>
所属分类:<asp:LinkButton ID="LinkButton4" runat="server"><%# Eval("UF_PICTURE_TYPE")%></asp:LinkButton><a href="#"></a>
<a href="#" class="sq">
<asp:LinkButton ID="LinkButton1" runat="server">进入评论</asp:LinkButton></a>点击次数:<%# Eval("UF_PICTURE_VISITQUANTITY")%> 次</label>
</span>
</div>
</ItemTemplate>
<HeaderTemplate>
<div class="fenye">
<label>
<asp:LinkButton ID="LinkButton20" runat="server" BackColor="Transparent" BorderColor="Transparent"
BorderStyle="None">上传图片</asp:LinkButton><a href="#"></a></label>
<span style="width: 429px">共有<strong>28</strong>张图片 共3页
<asp:LinkButton ID="LinkButton5" runat="server" BackColor="Transparent" BorderColor="Transparent" BorderStyle="None">首页</asp:LinkButton><a href="#"></a>
<asp:LinkButton ID="LinkButton6" runat="server" BackColor="Transparent" BorderColor="Transparent" BorderStyle="None">< 上一页</asp:LinkButton><a href="#"> </a>
<asp:LinkButton ID="LinkButton9" runat="server" BackColor="Transparent" BorderColor="Transparent" BorderStyle="None">1</asp:LinkButton><a href="#"></a>
<asp:LinkButton ID="LinkButton10" runat="server" BackColor="Transparent" BorderColor="Transparent" BorderStyle="None">2</asp:LinkButton><a href="#"></a>
<asp:LinkButton ID="LinkButton11" runat="server" BackColor="Transparent" BorderColor="Transparent" BorderStyle="None">3</asp:LinkButton>
<asp:LinkButton ID="LinkButton7" runat="server" BackColor="Transparent" BorderColor="Transparent" BorderStyle="None">下一页 > </asp:LinkButton>
<asp:LinkButton ID="LinkButton8" runat="server" Width="24px" BackColor="Transparent" BorderColor="Transparent" BorderStyle="None">尾页</asp:LinkButton><a href="#"></a></span></div>
</HeaderTemplate>
<FooterTemplate>
<div class="fenye" id="fenye" style="width: 877px">
<label>
<asp:LinkButton ID="LinkButton19" runat="server" BackColor="Transparent" BorderColor="Transparent"
BorderStyle="None">上传图片</asp:LinkButton><a href="#"></a></label>
<span style="width: 416px">共有<strong>28</strong>张图片 共<strong>3</strong>页
<asp:LinkButton ID="LinkButton12" runat="server" BackColor="Transparent" BorderColor="Transparent" BorderStyle="None">首页</asp:LinkButton><a href="#"></a>
<asp:LinkButton ID="LinkButton13" runat="server" BackColor="Transparent" BorderColor="Transparent" BorderStyle="None">< 上一页</asp:LinkButton><a href="#">
</a>
<asp:LinkButton ID="LinkButton14" runat="server" BackColor="Transparent" BorderColor="Transparent" BorderStyle="None">1</asp:LinkButton><a href="#"></a>
<asp:LinkButton ID="LinkButton15" runat="server" BackColor="Transparent" BorderColor="Transparent" BorderStyle="None">2</asp:LinkButton>
<asp:LinkButton ID="LinkButton16" runat="server" BackColor="Transparent" BorderColor="Transparent" BorderStyle="None">3</asp:LinkButton>
<asp:LinkButton ID="LinkButton17" runat="server" BackColor="Transparent" BorderColor="Transparent" BorderStyle="None">上一页 > </asp:LinkButton>
<asp:LinkButton ID="LinkButton18" runat="server" BackColor="Transparent" BorderColor="Transparent" BorderStyle="None">尾页</asp:LinkButton><a href="#"></a></span></div>
</FooterTemplate>
</asp:DataList>" border="0" />