.aspx页面
<table>
<tr>
<td style="width: 66px">
<asp:Label ID="Label2" runat="server" Text="当前页:"></asp:Label>
</td>
<td style="width: 13px">
<asp:Label ID="currentPage" runat="server" Text="1"></asp:Label>
</td>
<td style="width: 66px">
<asp:Label ID="Label3" runat="server" Text="总页数:"></asp:Label>
</td>
<td style="width: 13px">
<asp:Label ID="totalPage" runat="server"></asp:Label>
</td>
<td style="width:71px; text-align: right; font-size: 12px">
<asp:LinkButton ID="firstPage" runat="server" onclick="firstPage_Click" >首页</asp:LinkButton>
</td>
<td style="width:71px; text-align: right; font-size: 12px">
<asp:LinkButton ID="fontPage" runat="server" onclick="fontPage_Click">上一页</asp:LinkButton>
</td>
<td style="width:71px; text-align: right; font-size: 12px">
<asp:LinkButton ID="nextPage" runat="server" onclick="nextPage_Click" >下一页</asp:LinkButton>
</td>
<td style="width:71px; text-align: right; font-size: 12px">
<asp:LinkButton ID="lastPage" runat="server" onclick="lastPage_Click" >尾页</asp:LinkButton>
</td>
<td style="width: 71px">
转到第
<asp:DropDownList ID="DropDownList1" runat="server"
onselectedindexchanged="DropDownList1_SelectedIndexChanged">
</asp:DropDownList>
页
</td>
</tr>
</table>.cs页面
static PagedDataSource pds = new PagedDataSource();
public void Bind()
{
string s = Request.QueryString["keyword"];
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand=new SqlCommand ("select * from A where ATitle like '%" +s + "%' or AContent like '%" + s + "%' order by AId desc",conn);
DataSet ds = new DataSet();
try
{
conn.Open();
da.Fill(ds, "a");
int count = ds.Tables["a"].Rows.Count;
this.labMsg.Text = "搜索到:" + count + " 条记录!";
conn.Close();
}
catch (SqlException el)
{
Response.Write(el.ToString());
}
int cur = Convert.ToInt32(currentPage.Text);//当前页数,初始化为第一页
pds.DataSource = ds.Tables["a"].DefaultView;
pds.AllowPaging = true;
pds.PageSize = 5;
pds.CurrentPageIndex = cur - 1;
int countPage = int.Parse(pds.PageCount.ToString());
totalPage.Text = countPage.ToString(); //获取总页数
if(!IsPostBack)
{
for(int i=1;i<pds.PageCount+1;i++)
{
this.DropDownList1.Items.Add(i.ToString());
}
fontPage.Enabled=true;
nextPage.Enabled=true;
}
try{
DropDownList1.SelectedItem.Text=cur.ToString();
dlNews.DataSource=pds;
dlNews.DataBind();
}
catch(Exception ex)
{
Response.Write(ex.Message);
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)//跳转到指定页码
{
int page=Convert.ToInt16((DropDownList1.SelectedItem.Value));
currentPage.Text = page.ToString();
Bind();
}
protected void firstPage_Click(object sender, EventArgs e) //第一页
{
if (currentPage.Text != "1")
{
currentPage.Text = "1";
}
else
{
Response.Write("<script language=javascript>alert(\"已经是第一页\")" + "</script>");
}
Bind();
}
protected void fontPage_Click(object sender, EventArgs e) //上一页
{
try
{
if (Convert.ToInt16(currentPage.Text) > 1)
{
currentPage.Text = Convert.ToString(Convert.ToInt32(currentPage.Text) - 1);
DropDownList1.SelectedValue = currentPage.Text;
Bind();
}
else
{
Response.Write("<script language=javascript>alert(\"已经是第一页\")" + "</script>");
}
}
catch
{
Response.Write("<script language=javascript>alert(\"已经是第一页\")" + "</script>");
}
}
protected void nextPage_Click(object sender, EventArgs e) //下一页
{
try
{
currentPage.Text=Convert.ToString(Convert.ToInt32(currentPage.Text)+1);
DropDownList1.SelectedValue=currentPage.Text;
Bind();
}
catch{
Response.Write("<script language=javascript>alert(\"已经是最后一页\")" + "</script>");
currentPage.Text="1";
Bind();
}
}
protected void lastPage_Click(object sender, EventArgs e) //尾页
{
if (currentPage.Text.ToString() != totalPage.ToString())
{
currentPage.Text = totalPage.Text.ToString(); }
else
{
Response.Write("<script language=javascript>alert(\"已经是最后一页\")" + "</script>");
}
Bind();
}
这是一个搜索功能,搜索结果在控件datalist中显示,分页时出现下列错误:
比如:搜索结果一共两页,点击下一页(也就是到第二页)后,仍然可以继续点击下一页按钮,当前页显示为3。另外,dropdownlist1中选择页码也不能跳转到相应的页面中?不知道为什么,请教高手指点一下哪里有错误!急!!谢谢!
http://www.cnblogs.com/insus/articles/1418224.html