第一步:在前台頁碼中定義翻頁的按鈕
<table><tr><td >
<ASP:LinkButton ID="lnkbtnFirst" runat="server" CSSClass="text_gg " Enabled="False"
Font-Bold="false" OnClick="PagerButtonClick">首頁</ASP:LinkButton>
<ASP:LinkButton ID="lnkbtnPre" runat="server" CSSClass="text_gg " Enabled="False"
OnClick="PagerButtonClick">上一頁</ASP:LinkButton>
<ASP:LinkButton ID="lnkbtnNext" runat="server" CSSClass="text_gg " Enabled="False"
OnClick="PagerButtonClick">下一頁</ASP:LinkButton>
<ASP:LinkButton ID="lnkbtnLast" runat="server" CSSClass="text_gg " Enabled="False"
OnClick="PagerButtonClick">末頁</ASP:LinkButton>
<asp:Label ID="lblPageCount" runat="server" Text="共?頁"></ASP:Label>
<asp:Label ID="lblCurrentIndex" runat="server" Text="第?頁"></ASP:Label>
<ASP:LinkButton ID="lnkbtnJumpPage" runat="server" CSSClass="text_gg " OnClick="lnkbtnJumpPage_Click"
OnClIEntClick="return checkGoPage()">轉到</asp:LinkButton><ASP:TextBox ID="txtJumpPage" runat="server" onkeypress="return InputLimit()"
onpaste="return false" Width="24px"></asp:TextBox><asp:Label ID="lblRecordpage" runat="server" Text="頁"></ASP:Label></td>
</tr>
</table>
第二步:在對應CS文件裡寫分頁按鈕觸發的事件
分頁方法#region 分頁方法
public void FillGridVIEw()
...{
this.SiteAreaGridVIEw.PageIndex = 0;
SiteAreaGridVIEw.DataSource = siteBaseInfoManager.GetSiteAreaList();
SiteAreaGridVIEw.DataBind();
this.currentPage.Value = (SiteAreaGridVIEw.PageIndex + 1).ToString();
lblCurrentIndex.Text = "第 " + (SiteAreaGridVIEw.PageIndex + 1).ToString() + " 頁";
//lblPageCount.Text = "共 <font face='黑體'><strong>" + SiteAreaGridVIEw.PageCount.ToString() + "</strong></font> 頁";
lblPageCount.Text = "共 " + SiteAreaGridVIEw.PageCount.ToString() + " 頁";
lnkbtnFirst.Enabled = false;
lnkbtnPre.Enabled = false;
lnkbtnNext.Enabled = false;
lnkbtnLast.Enabled = false;
if (SiteAreaGridVIEw.PageCount != 1)
...{
lnkbtnNext.Enabled = true;
lnkbtnLast.Enabled = true;
}
// 設置LinkButton此時對應的參數
lnkbtnFirst.CommandArgument = "1";
lnkbtnPre.CommandArgument = (SiteAreaGridView.PageIndex == 0 ? "1" : SiteAreaGridVIEw.PageIndex.ToString());
lnkbtnNext.CommandArgument = (SiteAreaGridView.PageCount == 1 ? SiteAreaGridView.PageCount.ToString() : (SiteAreaGridVIEw.PageIndex + 2).ToString());
lnkbtnLast.CommandArgument = SiteAreaGridVIEw.PageCount.ToString();
}
protected void PagerButtonClick(object sender, EventArgs e)
...{
// 給頁面中GRIDVIEW綁定數據源
SiteAreaGridVIEw.DataSource = siteBaseInfoManager.GetSiteAreaList();
SiteAreaGridVIEw.PageIndex = Convert.ToInt32(((LinkButton)sender).CommandArgument) - 1;
SiteAreaGridVIEw.DataBind();
// 更新當前頁面顯示值
lblCurrentIndex.Text = "第 " + (SiteAreaGridVIEw.PageIndex + 1).ToString() + " 頁";
this.currentPage.Value = (SiteAreaGridVIEw.PageIndex + 1).ToString();
// 更新參數
lnkbtnPre.CommandArgument = (SiteAreaGridView.PageIndex == 0 ? "1" : SiteAreaGridVIEw.PageIndex.ToString());
lnkbtnNext.CommandArgument = (SiteAreaGridView.PageCount == 1 ? SiteAreaGridView.PageCount.ToString() : (SiteAreaGridVIEw.PageIndex + 2).ToString());
SetLinkButtonEnabled();
}
/**//// <summary>
/// 用於設置按鈕的狀態
/// </summary>
public void SetLinkButtonEnabled()
...{
if (SiteAreaGridView.PageCount == SiteAreaGridVIEw.PageIndex + 1)
...{
lnkbtnNext.Enabled = false;
lnkbtnLast.Enabled = false;
}
else
...{
lnkbtnNext.Enabled = true;
lnkbtnLast.Enabled = true;
}
if (SiteAreaGridVIEw.PageIndex == 0)
...{
lnkbtnPre.Enabled = false;
lnkbtnFirst.Enabled = false;
}
else
...{
lnkbtnPre.Enabled = true;
lnkbtnFirst.Enabled = true;
}
}
/**//// <summary>
/// 點擊”轉到“按鈕時候觸發的事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void lnkbtnJumpPage_Click(object sender, EventArgs e)
...{
// 重新綁定數據源
SiteAreaGridVIEw.DataSource = siteBaseInfoManager.GetSiteAreaList();
SiteAreaGridVIEw.PageIndex = int.Parse(txtJumpPage.Text) - 1;
SiteAreaGridVIEw.DataBind();
// 更新當前頁面顯示值
lblCurrentIndex.Text = "第 " + (SiteAreaGridVIEw.PageIndex + 1).ToString() + " 頁";
this.currentPage.Value = (SiteAreaGridVIEw.PageIndex + 1).ToString();
// 更新參數
lnkbtnPre.CommandArgument = (SiteAreaGridView.PageIndex == 0 ? "1" : SiteAreaGridVIEw.PageIndex.ToString());
lnkbtnNext.CommandArgument = (SiteAreaGridView.PageCount == 1 ? SiteAreaGridView.PageCount.ToString() : (SiteAreaGridVIEw.PageIndex + 2).ToString());
SetLinkButtonEnabled();
this.txtJumpPage.Text = "";
}
#endregion
在PAGE_LOAD中加入
this.txtJumpPage.Attributes.Add("style", "ime-mode:disabled");//屏蔽輸入跳轉頁碼的文本框的輸入法
步驟3: 寫JS文件,檢查輸入限制和在輸入回車鍵後自動跳轉頁面
//限制用戶只能輸入數字
function InputLimit()
...{
//如果輸入的是“回車鍵”,則直接跳轉
if(event.keyCode==13)
...{
document.getElementById("lnkbtnJumpPage").click();
}
if(event.keyCode>=48&&event.keyCode<=57)
...{
return true;
}
return false;
}
//檢查在輸入框的頁碼
function checkGoPage()
...{
var currentPage=document.getElementById("currentPage").value;
var value=Trim(document.getElementById("txtJumpPage").value);
var re = /^0*(0|1)$/;
if(value=="")
...{
// alert("請輸入要跳轉的頁數!");
document.getElementById("txtJumpPage").value="";
document.getElementById("txtJumpPage").focus();
return false;
}
else
if(currentPage==1&&re.exec(value)!=null)
...{
document.getElementById("txtJumpPage").value="";
document.getElementById("txtJumpPage").focus();
return false;
}
else if(currentPage!=1&&re.exec(value)!=null)
...{
document.getElementById("txtJumpPage").value="1";
return true;
}
}