用按钮实现类似百度谷歌分页(交流)
用按钮实现类似百度谷歌分页-----------------------------------源代码中的代码--------------------
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="False"
HorizontalAlign="Center" Width="100%" EnableTheming="True">
<PagerSettings FirstPageText="[首页]" LastPageText="[尾页]" NextPageText="[下一页]" PageButtonCount="5"
PreviousPageText="[上一页]" />
<FooterStyle VerticalAlign="Middle" />
<Columns>
<asp:BoundField HeaderText="会员昵称" DataField="UF_OWNERCOMM_USER" DataFormatString="{0:d}" />
<asp:TemplateField HeaderText="头像">
<ItemTemplate>
<img height="120" src="" width="120" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField HeaderText="楼号" DataField="UF_OWNERCOMM_FLORID" DataFormatString="{0:d}" />
<asp:BoundField HeaderText="房间号" DataField="UF_OWNERCOMM_ROOMID" DataFormatString="{0:d}" />
<asp:BoundField HeaderText="QQ号" DataField="UF_OWNERCOMM_QQ" DataFormatString="{0:d}" />
<asp:BoundField HeaderText="MSN" DataField="UF_OWNERCOMM_MSN" DataFormatString="{0:d}" />
<asp:BoundField HeaderText="电话" DataField="UF_OWNERCOMM_TEL" DataFormatString="{0:d}" />
</Columns>
<PagerTemplate>
<asp:LinkButton ID="LinkButton1" runat="server">[首页]</asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server">[上一页]</asp:LinkButton>
<asp:LinkButton ID="yi" runat="server">1</asp:LinkButton>
<asp:LinkButton ID="er" runat="server">2</asp:LinkButton>
<asp:LinkButton ID="san" runat="server">3</asp:LinkButton>
<asp:LinkButton ID="si" runat="server">4</asp:LinkButton>
<asp:LinkButton ID="wu" runat="server">5</asp:LinkButton>
……
<asp:LinkButton ID="zong" runat="server">7</asp:LinkButton>
<asp:LinkButton ID="LinkButton5" runat="server">>></asp:LinkButton>
<asp:LinkButton ID="LinkButton3" runat="server">[下一页]</asp:LinkButton>
<asp:LinkButton ID="LinkButton4" runat="server">[尾页]</asp:LinkButton>
</PagerTemplate>
<PagerStyle HorizontalAlign="Right" VerticalAlign="Middle" Height="30px" />
<HeaderStyle BackColor="White" />
</asp:GridView>
<asp:GridView ID="GridView2" runat="server" AllowPaging="True" AutoGenerateColumns="False"
HorizontalAlign="Center" Width="100%" EnableTheming="True">
<PagerSettings FirstPageText="[首页]" LastPageText="[尾页]" NextPageText="[下一页]" PageButtonCount="5"
PreviousPageText="[上一页]" />
<FooterStyle VerticalAlign="Middle" />
<Columns>
<asp:BoundField HeaderText="会员昵称" DataField="UF_OWNERCOMM_USER" DataFormatString="{0:d}" />
<asp:TemplateField HeaderText="头像">
<ItemTemplate>
<img height="120" src="" width="120" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField HeaderText="楼号" DataField="UF_OWNERCOMM_FLORID" DataFormatString="{0:d}" />
<asp:BoundField HeaderText="房间号" DataField="UF_OWNERCOMM_ROOMID" DataFormatString="{0:d}" />
<asp:BoundField HeaderText="QQ号" DataField="UF_OWNERCOMM_QQ" DataFormatString="{0:d}" />
<asp:BoundField HeaderText="MSN" DataField="UF_OWNERCOMM_MSN" DataFormatString="{0:d}" />
<asp:BoundField HeaderText="电话" DataField="UF_OWNERCOMM_TEL" DataFormatString="{0:d}" />
</Columns>
<PagerTemplate>
<asp:LinkButton ID="shou" runat="server">[首页]</asp:LinkButton>
<asp:LinkButton ID="shang" runat="server">[上一页]</asp:LinkButton>
<asp:LinkButton ID="yi1" runat="server">1</asp:LinkButton>
<asp:LinkButton ID="er1" runat="server">2</asp:LinkButton>
<asp:LinkButton ID="san1" runat="server">3</asp:LinkButton>
<asp:LinkButton ID="si1" runat="server">4</asp:LinkButton>
<asp:LinkButton ID="wu1" runat="server">5</asp:LinkButton>
……
<asp:LinkButton ID="zong1" runat="server">7</asp:LinkButton>
<asp:LinkButton ID="zong" runat="server">>></asp:LinkButton>
<asp:LinkButton ID="xia" runat="server">[下一页]</asp:LinkButton>
<asp:LinkButton ID="wei" runat="server">[尾页]</asp:LinkButton>
</PagerTemplate>
<PagerStyle HorizontalAlign="Right" VerticalAlign="Middle" Height="30px" />
<HeaderStyle BackColor="White" />
</asp:GridView>
--------------------------------类里的代码----------------------------------------------
//分页绑定gridview
public static DataSet binggv(string sqlcomm,string bm,GridView gv)
{
SqlConnection myconn = new SqlConnection (getconnstr());
myconn.Open();
SqlDataAdapter sda = new SqlDataAdapter(sqlcomm, myconn);
DataSet ds = new DataSet();
sda.Fill(ds,bm);
gv.DataSource = ds;
gv.DataBind();
myconn.Close();
return ds;
}
------------------------------.cs里的代码------------------------------------
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;
public partial class index_txl : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bindgv1();
bindgv2();
}
}
protected void bindgv1() //绑定gridview1
{
try
{
string sqlcomm = "select * from UF_OWNERCOMM where UF_OWNERCOMM_CHECK = 1";
bindgridview(GridView1, sqlcomm, "zong", "yi", "er", "san", "si", "wu");
Label1
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}
protected void bindgv2() //绑定gridview2
{
try
{
string sqlcomm = "select * from UF_OWNERCOMM where UF_OWNERCOMM_CHECK = 0";
bindgridview(GridView2, sqlcomm, "zong1", "yi1", "er1", "san1", "si1", "wu1");
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}
protected void bindgridview(GridView g,string s,string zong,string yi,string er,string san,string si,string wu)
{
try
{
publicclass.binggv(s, "UF_OWNERCOMM", g);
GridViewRow gvr = g.BottomPagerRow;
((LinkButton)gvr.Cells[0].FindControl(zong)).Text = Convert.ToString(g.PageCount);
if (g.PageIndex <= 2) //当点击的那个编码小于或等于2时 5个Button显示的内容
{
((LinkButton)gvr.Cells[0].FindControl(yi)).Text = "1";
((LinkButton)gvr.Cells[0].FindControl(er)).Text = "2";
((LinkButton)gvr.Cells[0].FindControl(san)).Text = "3";
((LinkButton)gvr.Cells[0].FindControl(si)).Text = "4";
((LinkButton)gvr.Cells[0].FindControl(wu)).Text = "5";
}
else if ((g.PageIndex + 2) >= g.PageCount) //当点击的那个编码大于或等于总页数时 5个Button显示的内容
{
((LinkButton)gvr.Cells[0].FindControl(yi)).Text = Convert.ToString(g.PageCount - 4);
((LinkButton)gvr.Cells[0].FindControl(er)).Text = Convert.ToString(g.PageCount - 3);
((LinkButton)gvr.Cells[0].FindControl(san)).Text = Convert.ToString(g.PageCount - 2);
((LinkButton)gvr.Cells[0].FindControl(si)).Text = Convert.ToString(g.PageCount - 1);
((LinkButton)gvr.Cells[0].FindControl(wu)).Text = Convert.ToString(g.PageCount);
}
else
{
((LinkButton)gvr.Cells[0].FindControl(yi)).Text = Convert.ToString(g.PageIndex - 2);
((LinkButton)gvr.Cells[0].FindControl(er)).Text = Convert.ToString(g.PageIndex - 1);
((LinkButton)gvr.Cells[0].FindControl(san)).Text = Convert.ToString(g.PageIndex);
((LinkButton)gvr.Cells[0].FindControl(si)).Text = Convert.ToString(g.PageIndex + 1);
((LinkButton)gvr.Cells[0].FindControl(wu)).Text = Convert.ToString(g.PageIndex + 2);
}
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}
//-------------1表示审核通过 0表示审核不通过
//1下一页
protected void LinkButton3_Click(object sender, EventArgs e)
{
GridView1.PageIndex = GridView1.PageIndex + 1;
bindgv1();
}
//1尾页
protected void LinkButton4_Click(object sender, EventArgs e)
{
GridView1.PageIndex = GridView1.PageCount;
bindgv1();
}
//1上一页
protected void LinkButton2_Click(object sender, EventArgs e)
{
GridView1.PageIndex = GridView1.PageIndex - 1;
bindgv1();
}
//1首页
protected void LinkButton1_Click(object sender, EventArgs e)
{
GridView1.PageIndex = 0;
bindgv1();
}
//1下一页 >>
protected void LinkButton5_Click(object sender, EventArgs e)
{
GridView1.PageIndex = GridView1.PageIndex + 1;
bindgv1();
}
//1尾页 Button
protected void zong_Click(object sender, EventArgs e)
{
GridView1.PageIndex = GridView1.PageCount;
bindgv1();
}
//1按钮1
protected void yi_Click(object sender, EventArgs e)
{
GridViewRow gvr = GridView1.BottomPagerRow;
GridView1.PageIndex = Convert.ToInt32(((LinkButton)gvr.Cells[0].FindControl("yi")).Text);
bindgv1();
}
//1按钮2
protected void er_Click(object sender, EventArgs e)
{
GridViewRow gvr = GridView1.BottomPagerRow;
GridView1.PageIndex = Convert.ToInt32(((LinkButton)gvr.Cells[0].FindControl("er")).Text);
bindgv1();
}
//1按钮3
protected void san_Click(object sender, EventArgs e)
{
GridViewRow gvr = GridView1.BottomPagerRow;
GridView1.PageIndex = Convert.ToInt32(((LinkButton)gvr.Cells[0].FindControl("san")).Text);
bindgv1();
}
//1按钮4
protected void si_Click(object sender, EventArgs e)
{
GridViewRow gvr = GridView1.BottomPagerRow;
GridView1.PageIndex = Convert.ToInt32(((LinkButton)gvr.Cells[0].FindControl("si")).Text);
bindgv1();
}
//1按钮5
protected void wu_Click(object sender, EventArgs e)
{
GridViewRow gvr = GridView1.BottomPagerRow;
GridView1.PageIndex = Convert.ToInt32(((LinkButton)gvr.Cells[0].FindControl("si")).Text);
bindgv1();
}
//0尾页
protected void wei_Click(object sender, EventArgs e)
{
GridView2.PageIndex = GridView2.PageCount;
bindgv2();
}
//0下一页
protected void xia_Click(object sender, EventArgs e)
{
GridView2.PageIndex = GridView2.PageIndex + 1;
bindgv2();
}
//0总页 >>
protected void zong_Click1(object sender, EventArgs e)
{
GridView2.PageIndex = GridView2.PageIndex + 1;
bindgv2();
}
//0总页
protected void zong1_Click(object sender, EventArgs e)
{
GridView2.PageIndex = GridView2.PageCount;
bindgv2();
}
//0按钮5
protected void wu1_Click(object sender, EventArgs e)
{
GridViewRow gvr = GridView2.BottomPagerRow;
GridView2.PageIndex = Convert.ToInt32(((LinkButton)gvr.Cells[0].FindControl("wu1")).Text);
bindgv2();
}
//0按钮4
protected void si1_Click(object sender, EventArgs e)
{
GridViewRow gvr = GridView2.BottomPagerRow;
GridView2.PageIndex = Convert.ToInt32(((LinkButton)gvr.Cells[0].FindControl("si1")).Text);
bindgv2();
}
//0按钮3
protected void san1_Click(object sender, EventArgs e)
{
GridViewRow gvr = GridView2.BottomPagerRow;
GridView2.PageIndex = Convert.ToInt32(((LinkButton)gvr.Cells[0].FindControl("san1")).Text);
bindgv2();
}
//0按钮2
protected void er1_Click(object sender, EventArgs e)
{
GridViewRow gvr = GridView2.BottomPagerRow;
GridView2.PageIndex = Convert.ToInt32(((LinkButton)gvr.Cells[0].FindControl("er1")).Text);
bindgv2();
}
//0按钮1
protected void yi1_Click(object sender, EventArgs e)
{
GridViewRow gvr = GridView2.BottomPagerRow;
GridView2.PageIndex = Convert.ToInt32(((LinkButton)gvr.Cells[0].FindControl("yi1")).Text);
bindgv2();
}
//0上一页
protected void shang_Click(object sender, EventArgs e)
{
GridView2.PageIndex = GridView2.PageIndex - 1;
bindgv2();
}
//0首页
protected void shou_Click(object sender, EventArgs e)
{
GridView2.PageIndex = 0;
bindgv2();
}
}