老狗——GridView實現分頁
文章目錄
一、GridView分頁不顯示處理方法
① 檢查基礎屬性 AllowPaging、PageSize
正確:AllowPaging=“True” PageSize=“2”
錯誤:AllowPaging=“False” PageSize=“2”
② 檢查 OnPageIndexChanging
正確:OnPageIndexChanging=“GridView_PageIndexChanging”
還需要檢查後臺的函數
③ 檢查是否存在 PagerSettings 錯誤
<PagerSettings Visible="false" />
④ 在缺省情況下,當僅有一頁數據時, GridView 不顯示 Pager 分頁行
protected override void OnDataBound(EventArgs e)
{
if (this.PageCount == 1)
{
this.BottomPagerRow.Visible = true;
}
base.OnDataBound(e);
}
注:數據源的記錄行數等於0時。Gridview 的 BottomPageRow 爲 null 運行時出現異常(這是在網上看到的,我沒遇見這個錯誤~)
二、利用 PagerTemplate 實現分頁
① 前端
<%-- GridView Start --%>
<asp:GridView ID="GridView1" runat="server" DataKeyNames="BianHao" CssClass="GridView1"
AllowPaging="True" PageSize="8" AutoGenerateColumns="False" ShowFooter="false"
OnRowDataBound="GridView1_RowDataBound"
OnPageIndexChanging="GridView1_PageIndexChanging"
OnSelectedIndexChanging="GridView1_SelectedIndexChanging"
OnRowDeleting="GridView1_RowDeleting">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="cbSelect" runat="server" />
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" Width="30px" />
</asp:TemplateField>
<asp:BoundField DataField="BianHao" HeaderText="編號" />
<asp:BoundField DataField="XingMing" HeaderText="姓名" />
<asp:BoundField DataField="XingBie" HeaderText="性別" />
<asp:BoundField DataField="ShenFenZhengHao" HeaderText="身份證號" />
<asp:BoundField DataField="RenYuanXingZhi" HeaderText="人員性質" />
<asp:BoundField DataField="GLCSMingCheng" HeaderText="管理措施" />
<asp:BoundField DataField="FWCSMingCheng" HeaderText="服務措施" />
<asp:BoundField DataField="JingDu" HeaderText="經度" />
<asp:BoundField DataField="WeiDu" HeaderText="緯度" />
<asp:TemplateField ShowHeader="false">
<ItemTemplate>
<asp:HyperLink runat="server" Text="編輯" CssClass="layui-btn layui-btn-sm layui-btn-primary"
NavigateUrl='<%# String.Format("~/xxx.aspx?BianHao={0}", Eval("BianHao")) %>'></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ShowHeader="false">
<ItemTemplate>
<asp:HyperLink runat="server" Text="詳情" CssClass="layui-btn layui-btn-sm layui-btn-primary"
NavigateUrl='<%# String.Format("~/xxx.aspx?BianHao={0}&status={1}", Eval("BianHao"),"111") %>'></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ShowHeader="false">
<ItemTemplate>
<asp:LinkButton ID="DingWei" OnClick="DingWei_Click" Text="定位"
CommandArgument='<%# Eval("BianHao")%>'
CommandName="jwd"
CssClass="layui-btn layui-btn-sm layui-btn-primary" runat="server"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ShowHeader="false">
<ItemTemplate>
<asp:LinkButton ID="GridView_Delete" runat="server" CausesValidation="False"
CommandName="Delete" OnClientClick="return confirm('確定要刪除嗎?')"
Text="刪除" CssClass="layui-btn layui-btn-sm layui-btn-primary"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle"></HeaderStyle>
<RowStyle Height="20px" Font-Size="Large"></RowStyle>
<SelectedRowStyle CssClass="GridView_Row_Select" />
<PagerTemplate>
當前第:
<%--((GridView)Container.NamingContainer)就是爲了得到當前的控件 --%>
<asp:Label ID="LabelCurrentPage"
runat="server" Text="<%#((GridView)Container.NamingContainer).PageIndex + 1 %>"></asp:Label>
頁/共:
<%--得到分頁頁面的總數 --%>
<asp:Label ID="LabelPageCount" runat="server" Text="<%# ((GridView)Container.NamingContainer).PageCount %>"></asp:Label>
頁
<%--如果該分頁是首分頁,那麼該連接就不會顯示了.同時對應了自帶識別的命令參數CommandArgument --%>
<asp:LinkButton ID="LinkButtonFirstPage" runat="server" CommandArgument="First" CommandName="Page"
Visible='<%#((GridView)Container.NamingContainer).PageIndex != 0 %>'>首頁</asp:LinkButton>
<asp:LinkButton ID="LinkButtonPreviousPage" runat="server" CommandArgument="Prev" CommandName="Page"
Visible='<%# ((GridView)Container.NamingContainer).PageIndex != 0 %>'>上一頁</asp:LinkButton>
<%--如果該分頁是尾頁,那麼該連接就不會顯示了 --%>
<asp:LinkButton ID="LinkButtonNextPage" runat="server" CommandArgument="Next" CommandName="Page"
Visible='<%# ((GridView)Container.NamingContainer).PageIndex != ((GridView)Container.NamingContainer).PageCount - 1 %>'>下一頁</asp:LinkButton>
<asp:LinkButton ID="LinkButtonLastPage" runat="server" CommandArgument="Last" CommandName="Page"
Visible='<%# ((GridView)Container.NamingContainer).PageIndex != ((GridView)Container.NamingContainer).PageCount - 1 %>'>尾頁</asp:LinkButton>
轉到第
<asp:TextBox ID="txtNewPageIndex" runat="server" Width="20px"
Text='<%# ((GridView)Container.Parent.Parent).PageIndex + 1 %>' />
頁
<%--這裏將CommandArgument即使點擊該按鈕e.newIndex 值爲3 --%>
<asp:LinkButton ID="btnGo" runat="server" CausesValidation="False" CommandArgument="-2" CommandName="Page" Text="GO" />
</PagerTemplate>
</asp:GridView>
② 後臺
/// <summary>
/// 三層框架開發下,獲取數據以及綁定
/// </summary>
protected void GetData()
{
BLL.TSRQJiBenXinXi tb = new BLL.TSRQJiBenXinXi();
string strWhere = " 1=1 ";
if (!string.IsNullOrEmpty(this.findText.Text))
{
strWhere += " and (" + "XingMing like '%" + this.findText.Text
+ "%' or RenYuanXingZhi like '%" + this.findText.Text
+ "%' or BianHao like '%" + this.findText.Text
+ "%' or XingBie like '%" + this.findText.Text
+ "%' or ZhengZhiMianMao like '%" + this.findText.Text
+ "%' or ShenFenZhengHao like '%" + this.findText.Text
+ "%' or GLCSMingCheng like '%" + this.findText.Text
+ "%' or FWCSMingCheng like '%" + this.findText.Text
+ "%')";
}
DataTable dt = tb.GetList(strWhere).Tables[0];
int count = dt.Rows.Count;
GridView1.DataSource = dt;//設置gridview控件的數據源爲創建的數據集ds
GridView1.DataKeyNames = new string[] { "BianHao" };//主鍵
GridView1.DataBind(); //綁定數據庫表中數據
}
/// <summary>
/// 分頁
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
//GridView1.PageIndex = e.NewPageIndex;
// 得到該控件
GridView theGrid = this.GridView1;
int newPageIndex = 0;
if (e.NewPageIndex == -3)
{
//點擊了Go按鈕
TextBox txtNewPageIndex =null;
//GridView較DataGrid提供了更多的API,獲取分頁塊可以使用BottomPagerRow 或者TopPagerRow,當然還增加了HeaderRow和FooterRow
GridViewRow pagerRow = theGrid.BottomPagerRow;
if (pagerRow != null)
{
//得到text控件
txtNewPageIndex = pagerRow.FindControl("txtNewPageIndex")as TextBox;
}
if (txtNewPageIndex != null)
{
//得到索引
newPageIndex =int.Parse(txtNewPageIndex.Text)-1;
}
}
else
{
//點擊了其他的按鈕
newPageIndex = e.NewPageIndex;
}
//防止新索引溢出
newPageIndex = newPageIndex<0?0: newPageIndex;
newPageIndex = newPageIndex >= theGrid.PageCount ?
theGrid.PageCount - 1 : newPageIndex;
//得到新的值
theGrid.PageIndex = newPageIndex;
//重新綁定
GetData();
}
protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
GridView1.SelectedIndex = e.NewSelectedIndex; //實現選擇功能
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
try
{
string bianhao = GridView1.DataKeys[e.RowIndex][0].ToString();//取出修改行的索引
if (!string.IsNullOrEmpty(bianhao))
{
BLL.TSRQJiBenXinXi tb = new BLL.TSRQJiBenXinXi();
tb.Delete(Convert.ToInt32(bianhao));
GetData();
}
}
catch (Exception ee)
{
Console.WriteLine("{0}", ee.Message);
}
}
protected void DingWei_Click(object sender, EventArgs e)
{
int row = ((GridViewRow)((LinkButton)sender).NamingContainer).RowIndex;
index = row + 1;
//註冊前端JS
//this.Page.ClientScript.RegisterClientScriptBlock(this.Page.GetType(), "DW", "DW()",true);
//註冊並調用執行前端JS
ClientScript.RegisterStartupScript(this.Page.GetType(), "DW", "DW()", true);
}
/// <summary>
/// 奇偶數行變色
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
switch (e.Row.RowType)
{
case DataControlRowType.Header:
e.Row.BackColor = Color.White;
e.Row.ForeColor = Color.Black;
break;
case DataControlRowType.DataRow:
//建立奇數行與偶數行的onmouseover及onmouseout的顏色變換
if (Convert.ToInt16(ViewState["LineNo"]) == 0)
{
e.Row.BackColor = Color.AliceBlue;
ViewState["LineNo"] = 1;
}
else
{
e.Row.BackColor = Color.White;
ViewState["LineNo"] = 0;
}
break;
}
//鼠標滑過顏色變換
e.Row.Attributes.Add("onMouseOver", "Color=this.style.backgroundColor;this.style.backgroundColor='#CDC5BF';this.style.cursor='hand'");
e.Row.Attributes.Add("onMouseOut", "this.style.backgroundColor=Color;");
//e.Row.HorizontalAlign = HorizontalAlign.Center;
}
貼一個非常實用的GridView.PagerTemplate 屬性
https://blog.csdn.net/dzz1001/article/details/2031381
作者聯繫方式:QQ:846113076 歡迎交流