關於GridView分頁頁碼的討論
在GridView中實現分頁的效果方法很簡單,只需要在“GridView任務”對話框中進行設置就可以了。在“GridView任務”對話框中,選擇“啓用分頁”命令,這樣建立起簡單的分頁效果。
在GridView中實現分頁的效果方法很簡單,只需要在“GridView任務”對話框中進行設置就可以了。在“GridView任務”對話框中,選擇“啓用分頁”命令,這樣建立起簡單的分頁效果。
在使用“啓用分頁”命令的時候要注意兩點。
(1) 是否允許分頁
GridView的AllowPaging屬性。AllowPaging:是否允許分頁。如果AllowPaging=“true”就是允許分頁。否則就是不允許使用分頁。
(2) 每頁記錄數
GridView的PageSize屬性。在GridView控件的屬性中可以設置每頁顯示的數據記錄的個數。默認情況下PageSize的值是10,也可以根據需要進行設置。
如果想要對分頁編碼進行設置的話,可以在HTML代碼中爲GridView控件添加分頁導航條形式代碼。也就是啓用GridView的PagerSettings屬性,在PagerSettings屬性中可以設置根據需要設置Mode的值,來實現分頁編碼的顯示效果。
<PagerSettings
Mode = "NextPreviousFirstLast"
FirstPageText = "第一頁"
LastPageText = "末頁">
</PagerSettings>
注意:
PagerSettings屬性的Mode:Numeric,NextPrevious,NextPreviousFirstLast,NumericFirstLast。有這四種,可以根據不同需要進行不同的選擇設置。
自動設置分頁效果
現在想要在GridView控件上顯示如下頁碼信息:總頁數、當前頁、首頁、上一頁、下一頁、尾頁。
現在想要在GridView控件上顯示如下頁碼信息:總頁數、當前頁、首頁、上一頁、下一頁、尾頁。
創建總頁數
<asp:Label ID="Lab_PageCount" runat="server" Text="<%# ((GridView)Container.NamingContainer).PageCount %>"></asp:Label>
創建但前頁
<asp:Label ID="Lab_CurrentPage" runat="server" Text="<%# ((GridView)Container.NamingContainer).PageIndex + 1 %>"></asp:Label>
創建首頁
<asp:LinkButton ID="LBtn_FirstPage" runat="server" CommandArgument="First" CommandName="Page"
Visible="<%# ((GridView)Container.NamingContainer).PageIndex != 0 %>">首頁</asp:LinkButton>
創建上一頁
<asp:LinkButton ID="LBtn_PreviousPage" runat="server" CommandArgument="Prev" CommandName="Page"
Visible="<%# ((GridView)Container.NamingContainer).PageIndex != 0 %>">上一頁</asp:LinkButton>
創建下一頁
<asp:LinkButton ID="LBtn_NextPage" runat="server" CommandArgument="Next" CommandName="Page"
Visible="<%# ((GridView)Container.NamingContainer).PageIndex != ((GridView)Container.NamingContainer).PageCount - 1 %>">下一頁</asp:LinkButton>
創建尾頁
<asp:LinkButton ID="LBtn_LastPage" runat="server" CommandArgument="Last" CommandName="Page"
Visible="<%# ((GridView)Container.NamingContainer).PageIndex != ((GridView)Container.NamingContainer).PageCount - 1 %>">尾頁</asp:LinkButton>
對應後臺代碼
public void GetDataSet()
{
string zhuangtmc;
zhuangtmc = Convert.ToString(DropDownList1.Text);
string sql;
sql = "Select L.LunWBH,L.LunWBT,Z.ZhuangTMC,L.ZhuCYHM,B.BianJM,L.TouGRQ From T_LunWXX L ,T_LunWZhT Z,T_BianJPL B ";
sql += "Where L.ZhuangTBH=Z.ZhuangTBH AND B.LunWBH=L.LunWBH AND";
sql += " B.BianJM='" + zhucyhm + "'";
if (zhuangtmc != "")
{
sql += " And Z.ZhuangTMC='" + zhuangtmc + "'";
}
sql += " Order By TouGRQ";
CommonDB = new Common();
GridView1.DataSource = CommonDB.DataSource(sql);
GridView1.DataBind();
{
string zhuangtmc;
zhuangtmc = Convert.ToString(DropDownList1.Text);
string sql;
sql = "Select L.LunWBH,L.LunWBT,Z.ZhuangTMC,L.ZhuCYHM,B.BianJM,L.TouGRQ From T_LunWXX L ,T_LunWZhT Z,T_BianJPL B ";
sql += "Where L.ZhuangTBH=Z.ZhuangTBH AND B.LunWBH=L.LunWBH AND";
sql += " B.BianJM='" + zhucyhm + "'";
if (zhuangtmc != "")
{
sql += " And Z.ZhuangTMC='" + zhuangtmc + "'";
}
sql += " Order By TouGRQ";
CommonDB = new Common();
GridView1.DataSource = CommonDB.DataSource(sql);
GridView1.DataBind();
//用lblCurrentIndex來顯示當前頁的頁數。
LabelCurrentPage.Text = "第 " + (GridView1.PageIndex + 1).ToString() + " 頁";
//用LblPageCount來顯示當前數據的總頁數。
LabelPageCount.Text = "共 " + GridView1.PageCount.ToString() + " 頁";
//用LblrecordCount來顯示數據的總條數。
LabelRecordCount.Text = "總共 " + CommonDB.DataSets(sql).Tables[0].Rows.Count.ToString() + " 條";
LabelCurrentPage.Text = "第 " + (GridView1.PageIndex + 1).ToString() + " 頁";
//用LblPageCount來顯示當前數據的總頁數。
LabelPageCount.Text = "共 " + GridView1.PageCount.ToString() + " 頁";
//用LblrecordCount來顯示數據的總條數。
LabelRecordCount.Text = "總共 " + CommonDB.DataSets(sql).Tables[0].Rows.Count.ToString() + " 條";
// 計算生成分頁頁碼,分別爲:"首 頁" "上一頁" "下一頁" "尾 頁"
//點擊首頁設定的值爲1。
LinkButtonFirstPage.CommandName = "1";
//點擊‘上一頁’
LinkButtonPreviousPage.CommandName = (GridView1.PageIndex == 0 ? "1" : GridView1.PageIndex.ToString());
//點擊‘下一頁’
LinkButtonNextPage.CommandName = (GridView1.PageCount == 1 ? GridView1.PageCount.ToString() : (GridView1.PageIndex + 2).ToString());
//點擊‘尾頁’
LinkButtonLastPage.CommandName = GridView1.PageCount.ToString();
}
//點擊首頁設定的值爲1。
LinkButtonFirstPage.CommandName = "1";
//點擊‘上一頁’
LinkButtonPreviousPage.CommandName = (GridView1.PageIndex == 0 ? "1" : GridView1.PageIndex.ToString());
//點擊‘下一頁’
LinkButtonNextPage.CommandName = (GridView1.PageCount == 1 ? GridView1.PageCount.ToString() : (GridView1.PageIndex + 2).ToString());
//點擊‘尾頁’
LinkButtonLastPage.CommandName = GridView1.PageCount.ToString();
}