html代码:
<table border="0" cellpadding="0" cellspacing="0" style="width: 600px; font-size: 9pt; line-height: 20pt;">
<tr>
<td style="width: 300px; height: 4px" align="center">
一共<asp:Label ID="lblallcount" runat="server" Text='Label12'><%=lblallcount3%
></asp:Label>记录共<asp:Label
ID="lblpagecount" runat="server" Text="Label"><%=lblpagecount3%></asp:Label>页 第
<asp:Label ID="lblcurrentindex"
runat="server" Text="Label"><%= lblcurrentindex3%></asp:Label>页</td>
<td style="width: 450px; height: 4px" align="center"><asp:LinkButton ID="first"
runat="server" CommandArgument="first" OnClick="btnClick">first</asp:LinkButton>
<asp:LinkButton ID="prev" runat="server" CommandArgument="prev"
OnClick="btnClick">prev</asp:LinkButton>
<asp:LinkButton ID="next" runat="server" CommandArgument="next"
OnClick="btnClick">next</asp:LinkButton>
<asp:LinkButton ID="last" runat="server" CommandArgument="last"
OnClick="btnClick">last</asp:LinkButton>
跳转至第<asp:TextBox
ID="to" runat="server" Columns="1" Width="20px"></asp:TextBox>
页<asp:Button ID="go" runat="server" Text="go" Width="27px" OnClick="goClick" /></td>
</tr>
</table>
cs代码:
public int lblallcount3, lblpagecount3, lblcurrentindex3;
first.Text = "最前页";
prev.Text = "前一页";
next.Text = "下一页";
last.Text = "最后一页";
#region 用于显示分页的总记录条数及当前页
protected void showstate()
{
Page_size.Page_sum(s_Mzb, out lblallcount3, out lblpagecount3, out lblcurrentindex3, M_Gr);
//s_Mab为sql语句,M_Gr为datagrid的名称,其的三个函数,为前边定义的变量,在这儿调用了
}
#endregion
#region 用于上一页,下一页
protected void btnClick(object sender, EventArgs e)
{
string arg = ((LinkButton)sender).CommandArgument;//获取与其的事件参数
Page_size.switch_page(arg,M_Gr);
Mzb_DataBind();//这是一个函数(在这儿没有写),用于显示分页的内容,在这里重新绑定一次
showstate();//重新绑定上边的分页
}
#endregion
#region //用于分页的转向
protected void goClick(object sender, EventArgs e)
{
string s_text_name = to.Text.ToString();
if (Page_size.IsNumber(s_text_name))//IsNumber类用于转换成数字
{
Page_size.go_to(to, M_Gr);
to.Focus();
}
else
{
Jscript.Alert("您的输入有误,请重新输入");
}
Mzb_DataBind();//这是一个函数(在这儿没有写),用于显示分页的内容,在这里重新绑定一次
showstate();//重新绑定上边的分页
}
#endregion
//以下是用到的类
#region 用于获取页数
public static void Page_sum(string s_sql, out int lblallcount, out int lblpagecount1, out int lblcurrentindex1, DataGrid Gr)
{
SqlConnection mycon=new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["Str_sql"].ToString());
mycon.Open();
SqlDataAdapter mypter = new SqlDataAdapter(s_sql,mycon);
DataSet ds = new DataSet();
mypter.Fill(ds,"member_page");
DataTable dt = ds.Tables["member_page"];
int count = dt.Rows.Count;
lblallcount = Convert.ToInt32(count.ToString());//获得总条数;
lblpagecount1=Convert.ToInt32(Gr.PageCount.ToString());//获取总页数
lblcurrentindex1 = Convert.ToInt32(Gr.CurrentPageIndex + 1);//获取当前页的索引;
ds.Dispose();
mycon.Close();
}
#endregion
#region 用于上一页,下一页,首页,尾页
public static void switch_page(string s_arg,DataGrid Gr)
{
switch (s_arg)
{
case("first"):
Gr.CurrentPageIndex = 0;//获取当前页的显示索引
break;
case("prev"):
if (Gr.CurrentPageIndex > 0)//如果大于0,当前索引减1
Gr.CurrentPageIndex--;
break;
case("next"):
if (Gr.CurrentPageIndex < (Gr.PageCount - 1))//如果当前页小于总面数,当前页加1
Gr.CurrentPageIndex++;
break;
case("last"):
Gr.CurrentPageIndex = Gr.PageCount - 1;//如果当前页等于总页数,则退出,不执行,PageCount-1是因为索引从0开始的
break;
default:
Gr.CurrentPageIndex = Convert.ToInt32(s_arg);
break;
}
}
#endregion
#region 用于go按钮转向页
public static void go_to(TextBox goto_text, DataGrid Gr)
{
if (goto_text.Text.Trim() != "")
{
int index = Int32.Parse((goto_text.Text.Trim())) - 1;
if(index>=0&&index<Gr.PageCount)
{
Gr.CurrentPageIndex = index; //取出的goto_text的值,就等于当前页
}
}
}
#endregion
#region 判断是否为数字
public static bool IsNumber(string TextValue)
{
try
{
int n = Convert.ToInt32(TextValue);
return true;
}
catch
{
return false;
}
}
#endregion