前台 各种按钮
<div id="aaa" runat="server">
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr align="center">
<td style="width: 670px>
共<asp:Label ID="lblPageCount" runat="server" ForeColor="Red"></asp:Label>
页, 当前第<asp:Label ID="lblCurrPage" runat="server" ForeColor="Red"></asp:Label>
页 共<asp:Label ID="lblTotalCount" runat="server" ForeColor="Red"></asp:Label>
条记录, 每页显示<asp:Label ID="lblPageSize" runat="server" Text="5" ForeColor="Red"></asp:Label>条
</td>
</tr>
<tr align="center">
<td style="width: 670px>
<asp:LinkButton ID="btnFirst" runat="server" OnClick="btnFirst_Click1" >首页</asp:LinkButton>
<asp:LinkButton ID="btnPrev" runat="server" OnClick="btnPrev_Click1" >上一页</asp:LinkButton>
<asp:LinkButton ID="btnNext" runat="server" OnClick="btnNext_Click1" >下一页</asp:LinkButton>
<asp:LinkButton ID="btnLast" runat="server" OnClick="btnLast_Click1" >尾页</asp:LinkButton>
跳到<asp:DropDownList ID="ddlPager" runat="server" Width="50" OnSelectedIndexChanged="asd_SelectedIndexChanged" />
页
</td>
</tr>
</table>
</div>
后台
protected void Page_Load(object sender, EventArgs e)
{
this.NoLogin();
//GridViewBind();
if (!IsPostBack)
{
//GridView1.DataBind();
GridViewBind("1");
}
}
protected void btnFirst_Click1(object sender, EventArgs e)
{
GridViewBind("1");
}
protected void btnPrev_Click1(object sender, EventArgs e)
{
GridViewBind("-1");
}
protected void btnNext_Click1(object sender, EventArgs e)
{
GridViewBind("+1");
}
protected void btnLast_Click1(object sender, EventArgs e)
{
GridViewBind("0");
}
protected void asd_SelectedIndexChanged(object sender, EventArgs e)
{
GridViewBind(ddlPager.Text);
ddlPager.ClearSelection();
}
//绑定
public void GridViewBind(string page)
{
string sqlstr = "select *from D_HOUSE_SUBSCRIBE_REQUEST where D_HOUSE_SUBSCRIBE_REQUEST.AUDITING_STATE=0";
DataSet ds = SqlHelper.ExecuteDataSet(sqlstr);
int a = ds.Tables.Count;
int b = ds.Tables[0].Rows.Count;
if (ds == null || ds.Tables[0].Rows.Count <= 0)
{
int sr = 1;
return;
}
else
{
DataView dv = new DataView();
dv.Table = ds.Tables[0];
int totalCount = dv.Count;//总条数
int pageSize = 2;//每页显示条数
int pageCount = (totalCount % pageSize == 0) ? (totalCount / pageSize) : (totalCount / pageSize + 1);//总页数
int currPage; //当前页数
switch (page)
{
//首页
case "1":
currPage = 1;
break;
//下一页
case "+1":
currPage = int.Parse(lblCurrPage.Text) + 1;
break;
//上一页
case "-1":
currPage = int.Parse(lblCurrPage.Text) - 1;
break;
//尾页
case "0":
currPage = pageCount;
break;
//下拉列表框直接传值时
default:
currPage = int.Parse(page);
break;
}
if (!IsPostBack)
{
for (int i = 1; i <= pageCount; i++)
{
ddlPager.Items.Add(i.ToString());
}
}
//设置分页信息的显示
lblCurrPage.Text = currPage.ToString();
lblPageCount.Text = pageCount.ToString();
lblPageSize.Text = pageSize.ToString();
lblTotalCount.Text = totalCount.ToString();
//设置翻页按钮的可用性
btnFirst.Enabled = btnPrev.Enabled = btnNext.Enabled = btnLast.Enabled = true;
if (currPage == 1)
{
btnFirst.Enabled = btnPrev.Enabled = false;
}
if (currPage == pageCount)
{
btnNext.Enabled = btnLast.Enabled = false;
}
ddlPager.Text = currPage.ToString();
//从查询出的所有记录中筛选出即将呈现的当前页的数据集合
int loopCount = (currPage == pageCount) ? totalCount - (currPage - 1) * pageSize : pageSize; //显示的记录条数
DataRowView[] drwsToBind = new DataRowView[loopCount];
for (int i = 0; i < loopCount; i++)
{
drwsToBind[i] = dv[(currPage - 1) * pageSize + i];
}
GridView1.DataSource = drwsToBind;
GridView1.DataBind();
}
}