gridview中實現分頁
前臺:
Code
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--><asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="REGIONCODE" AllowPaging="True"
onrowdatabound="GridView1_RowDataBound" ondatabound="GridView1_DataBound"
onpageindexchanging="GridView1_PageIndexChanging1" AllowSorting="True"
onsorting="GridView1_Sorting" PageSize="15">
<Columns>
<asp:HyperLinkField DataNavigateUrlFields="ID"
DataNavigateUrlFormatString="~/notice/noticechakan.aspx?ID={0}"
DataTextField="TITLE" HeaderText="通知標題" Target="_blank" Text="{0}" >
<HeaderStyle HorizontalAlign="Center" Width="80%" />
<ItemStyle HorizontalAlign="left" />
asp:HyperLinkField>
<asp:BoundField DataField="SENDTIME" HeaderText="傳送日期"
HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center" DataFormatString="{0:yyyy-MM-dd}" HtmlEncode="False">
<HeaderStyle HorizontalAlign="Center">HeaderStyle>
<ItemStyle HorizontalAlign="Center">ItemStyle>
asp:BoundField>
<asp:BoundField DataField="REGIONCODE" HeaderText="使用者"
HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center">
<HeaderStyle HorizontalAlign="Center">HeaderStyle>
<ItemStyle HorizontalAlign="Center">ItemStyle>
asp:BoundField>
Columns>
<PagerTemplate>
<table width="100%">
<tr>
<td width="70%">
<asp:Label ID="lb_articlenum" runat="server" />
<asp:LinkButton CommandName="Page" CommandArgument="First" ID="linkBtnFirst" runat="server">首頁asp:LinkButton>
<asp:LinkButton CommandName="Page" CommandArgument="Prev" ID="linkBtnPrev" runat="server">上一頁asp:LinkButton>
<asp:LinkButton CommandName="Page" CommandArgument="Next" ID="linkBtnNext" runat="server">下一頁asp:LinkButton>
<asp:LinkButton CommandName="Page" CommandArgument="Last" ID="linkBtnLast" runat="server">末頁asp:LinkButton>
<asp:Label ID="MessageLabel" ForeColor="Blue" Text="頁碼:" runat="server" />
<asp:DropDownList ID="PageDropDownList" AutoPostBack="true" OnSelectedIndexChanged="PageDropDownList_SelectedIndexChanged"
runat="server" />
td>
<td width="30%" align="right">
<asp:Label ID="CurrentPageLabel" ForeColor="Blue" runat="server" />
td>
tr>
table>
PagerTemplate>
asp:GridView>
<asp:Label ID="lb_sql" runat="server" Visible="False">asp:Label>
<asp:Label ID="lb_count" runat="server" Visible="False">asp:Label>
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--><asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="REGIONCODE" AllowPaging="True"
onrowdatabound="GridView1_RowDataBound" ondatabound="GridView1_DataBound"
onpageindexchanging="GridView1_PageIndexChanging1" AllowSorting="True"
onsorting="GridView1_Sorting" PageSize="15">
<Columns>
<asp:HyperLinkField DataNavigateUrlFields="ID"
DataNavigateUrlFormatString="~/notice/noticechakan.aspx?ID={0}"
DataTextField="TITLE" HeaderText="通知標題" Target="_blank" Text="{0}" >
<HeaderStyle HorizontalAlign="Center" Width="80%" />
<ItemStyle HorizontalAlign="left" />
asp:HyperLinkField>
<asp:BoundField DataField="SENDTIME" HeaderText="傳送日期"
HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center" DataFormatString="{0:yyyy-MM-dd}" HtmlEncode="False">
<HeaderStyle HorizontalAlign="Center">HeaderStyle>
<ItemStyle HorizontalAlign="Center">ItemStyle>
asp:BoundField>
<asp:BoundField DataField="REGIONCODE" HeaderText="使用者"
HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center">
<HeaderStyle HorizontalAlign="Center">HeaderStyle>
<ItemStyle HorizontalAlign="Center">ItemStyle>
asp:BoundField>
Columns>
<PagerTemplate>
<table width="100%">
<tr>
<td width="70%">
<asp:Label ID="lb_articlenum" runat="server" />
<asp:LinkButton CommandName="Page" CommandArgument="First" ID="linkBtnFirst" runat="server">首頁asp:LinkButton>
<asp:LinkButton CommandName="Page" CommandArgument="Prev" ID="linkBtnPrev" runat="server">上一頁asp:LinkButton>
<asp:LinkButton CommandName="Page" CommandArgument="Next" ID="linkBtnNext" runat="server">下一頁asp:LinkButton>
<asp:LinkButton CommandName="Page" CommandArgument="Last" ID="linkBtnLast" runat="server">末頁asp:LinkButton>
<asp:Label ID="MessageLabel" ForeColor="Blue" Text="頁碼:" runat="server" />
<asp:DropDownList ID="PageDropDownList" AutoPostBack="true" OnSelectedIndexChanged="PageDropDownList_SelectedIndexChanged"
runat="server" />
td>
<td width="30%" align="right">
<asp:Label ID="CurrentPageLabel" ForeColor="Blue" runat="server" />
td>
tr>
table>
PagerTemplate>
asp:GridView>
<asp:Label ID="lb_sql" runat="server" Visible="False">asp:Label>
<asp:Label ID="lb_count" runat="server" Visible="False">asp:Label>
後臺程式碼:
Code
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--> public void gridview_databind(string sql)
{
BLLibrary.Bbase_notice noticedal = new BLLibrary.Bbase_notice();
IList<Model.base_noticeEntity> noticeEntitys = noticedal.Getbase_noticesbyCondition(sql+"order by sendtime desc");
lb_count.Text = noticeEntitys.Count.ToString();
GridView1.DataSource = noticeEntitys;
GridView1.DataBind();
}//gridview的資料繫結
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
//重新繫結資料
gridview_databind(lb_sql.Text);
}
protected void GridView1_DataBound(object sender, EventArgs e)
{
GridViewRow pagerRow = GridView1.BottomPagerRow;
if (pagerRow != null)
{
LinkButton linkBtnFirst = (LinkButton)pagerRow.Cells[0].FindControl("linkBtnFirst");
LinkButton linkBtnPrev = (LinkButton)pagerRow.Cells[0].FindControl("linkBtnPrev");
LinkButton linkBtnNext = (LinkButton)pagerRow.Cells[0].FindControl("linkBtnNext");
LinkButton linkBtnLast = (LinkButton)pagerRow.Cells[0].FindControl("linkBtnLast");
if (GridView1.PageIndex == 0)
{
linkBtnFirst.Enabled = false;
linkBtnPrev.Enabled = false;
}
else if (GridView1.PageIndex == GridView1.PageCount - 1)
{
linkBtnLast.Enabled = false;
linkBtnNext.Enabled = false;
}
else if (GridView1.PageCount <= 0)
{
linkBtnFirst.Enabled = false;
linkBtnPrev.Enabled = false;
linkBtnNext.Enabled = false;
linkBtnLast.Enabled = false;
}
DropDownList pageList = (DropDownList)pagerRow.Cells[0].FindControl("PageDropDownList");
Label pageLabel = (Label)pagerRow.Cells[0].FindControl("CurrentPageLabel");
Label articlenum = (Label)pagerRow.Cells[0].FindControl("lb_articlenum");
articlenum.Text = "共有" + lb_count.Text + "條資料,本頁" + GridView1.Rows.Count.ToString() + "條資料 ";
if (pageList != null)
{
for (int i = 0; i < GridView1.PageCount; i++)
{
int pageNumber = i + 1;
ListItem item = new ListItem("第" + pageNumber.ToString() + "頁", pageNumber.ToString());
if (i == GridView1.PageIndex)
{
item.Selected = true;
}
pageList.Items.Add(item);
}
}
if (pageLabel != null)
{
int currentPage = GridView1.PageIndex + 1;
pageLabel.Text = "當前頁: " + currentPage.ToString() +
" / " + GridView1.PageCount.ToString();
}
}
}
protected void PageDropDownList_SelectedIndexChanged(object sender, EventArgs e)
{
GridViewRow pagerRow = GridView1.BottomPagerRow;
DropDownList pageList = (DropDownList)pagerRow.Cells[0].FindControl("PageDropDownList");
GridView1.PageIndex = pageList.SelectedIndex;
gridview_databind(lb_sql.Text);
}
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--> public void gridview_databind(string sql)
{
BLLibrary.Bbase_notice noticedal = new BLLibrary.Bbase_notice();
IList<Model.base_noticeEntity> noticeEntitys = noticedal.Getbase_noticesbyCondition(sql+"order by sendtime desc");
lb_count.Text = noticeEntitys.Count.ToString();
GridView1.DataSource = noticeEntitys;
GridView1.DataBind();
}//gridview的資料繫結
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
//重新繫結資料
gridview_databind(lb_sql.Text);
}
protected void GridView1_DataBound(object sender, EventArgs e)
{
GridViewRow pagerRow = GridView1.BottomPagerRow;
if (pagerRow != null)
{
LinkButton linkBtnFirst = (LinkButton)pagerRow.Cells[0].FindControl("linkBtnFirst");
LinkButton linkBtnPrev = (LinkButton)pagerRow.Cells[0].FindControl("linkBtnPrev");
LinkButton linkBtnNext = (LinkButton)pagerRow.Cells[0].FindControl("linkBtnNext");
LinkButton linkBtnLast = (LinkButton)pagerRow.Cells[0].FindControl("linkBtnLast");
if (GridView1.PageIndex == 0)
{
linkBtnFirst.Enabled = false;
linkBtnPrev.Enabled = false;
}
else if (GridView1.PageIndex == GridView1.PageCount - 1)
{
linkBtnLast.Enabled = false;
linkBtnNext.Enabled = false;
}
else if (GridView1.PageCount <= 0)
{
linkBtnFirst.Enabled = false;
linkBtnPrev.Enabled = false;
linkBtnNext.Enabled = false;
linkBtnLast.Enabled = false;
}
DropDownList pageList = (DropDownList)pagerRow.Cells[0].FindControl("PageDropDownList");
Label pageLabel = (Label)pagerRow.Cells[0].FindControl("CurrentPageLabel");
Label articlenum = (Label)pagerRow.Cells[0].FindControl("lb_articlenum");
articlenum.Text = "共有" + lb_count.Text + "條資料,本頁" + GridView1.Rows.Count.ToString() + "條資料 ";
if (pageList != null)
{
for (int i = 0; i < GridView1.PageCount; i++)
{
int pageNumber = i + 1;
ListItem item = new ListItem("第" + pageNumber.ToString() + "頁", pageNumber.ToString());
if (i == GridView1.PageIndex)
{
item.Selected = true;
}
pageList.Items.Add(item);
}
}
if (pageLabel != null)
{
int currentPage = GridView1.PageIndex + 1;
pageLabel.Text = "當前頁: " + currentPage.ToString() +
" / " + GridView1.PageCount.ToString();
}
}
}
protected void PageDropDownList_SelectedIndexChanged(object sender, EventArgs e)
{
GridViewRow pagerRow = GridView1.BottomPagerRow;
DropDownList pageList = (DropDownList)pagerRow.Cells[0].FindControl("PageDropDownList");
GridView1.PageIndex = pageList.SelectedIndex;
gridview_databind(lb_sql.Text);
}
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/12639172/viewspace-617607/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 使用ObjectDataSource實現GridView分頁ObjectView
- Gridview分頁,清空,跳轉到View
- C# GridView 分頁顯示C#View
- gridview分頁變成上一頁,下一頁模式View模式
- elementUI實現分頁UI
- 分頁的實現
- 用下拉選單控制gridview的分頁View
- asyUI分頁中,如何實現頁面跳轉,再返回時,...UI
- Java中Elasticsearch 實現分頁方式(三種方式)JavaElasticsearch
- 分頁功能的實現
- Java Web 分頁實現JavaWeb
- Mybatis分頁實現流程MyBatis
- GridView實現分割線View
- GridView 自定義模版自定義修改,刪除,分頁View
- 使用 jQuery 實現分頁功能jQuery
- web 實現分頁列印功能Web
- PHP 使用 Redis 實現分頁PHPRedis
- indexdb實現分頁查詢Index
- 使用LayUI實現AJAX分頁UI
- 47.DRF實現分頁
- Django 前端BootCSS 實現分頁Django前端bootCSS
- Oralce 分頁 三種實現
- Hibernate 的分頁實現
- Java實現後端分頁Java後端
- MyBatis實現分頁的方式MyBatis
- ASP.NET中Repeater控制元件實現分頁功能ASP.NET控制元件
- 分頁實現前臺後臺不同效果,分頁類引入
- 分頁實現前五後五ajax區域性重新整理方式分頁實現
- gridview中帶多引數超連結的實現View
- uni-app之實現分頁APP
- 用AutoLayout實現分頁滾動
- 文章內容分頁功能實現
- Tp框架實現好看分頁效果框架
- 滑動視窗式分頁的實現
- jsp分頁功能的實現JS
- DataGridView分頁功能的實現View
- 如何 用hibernate 實現分頁
- [SQL Server]分頁功能的實現SQLServer