前台代码:
<asp:GridView ID="GridView1" runat="server" width="100%"
AllowPaging="True" PageSize="20"
onrowdatabound="GridView1_RowDataBound"
>
<HeaderStyle BackColor="LightGray" Font-Bold="True" ForeColor="Black" />
<RowStyle ForeColor="Black" Height="100" />
<Columns>
<asp:TemplateField>
<ItemTemplate>
<%# Container.DataItemIndex+1 %>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<PagerTemplate>
<table>
<tr>
<td style="text-align: right;color: Black;font-size: 10pt;font-family: 宋体;text-decoration: none;">
第<font style="font-family: Tahoma;color: Red"><%# ((GridView)Container.Parent.Parent).PageIndex + 1 %></font>页
共<font style="font-family: Tahoma;color: Red"><%# ((GridView)Container.Parent.Parent).PageCount %></font>页
<asp:LinkButton ID="btnFirst" runat="server" CssClass="font_color" CausesValidation="False" CommandArgument="first" CommandName="Page" OnClick="btnGridView_Click" Text="首页" />
<asp:LinkButton ID="btnPrev" runat="server" CssClass="font_color" CausesValidation="False" CommandArgument="prev" CommandName="Page" OnClick="btnGridView_Click" Text="上一页" />
<asp:LinkButton ID="btnNext" runat="server" CssClass="font_color" CausesValidation="False" CommandArgument="next" CommandName="Page" OnClick="btnGridView_Click" Text="下一页" />
<asp:LinkButton ID="btnLast" runat="server" CssClass="font_color" CausesValidation="False" CommandArgument="last" CommandName="Page" OnClick="btnGridView_Click" Text="尾页" />
<asp:TextBox ID="txtNewPageIndex" runat="server" Text="<%# ((GridView)Container.Parent.Parent).PageIndex + 1 %>" Width="50px" />
<asp:LinkButton ID="btnGo" runat="server" CssClass="font_color" CausesValidation="False" CommandArgument="go" CommandName="Page" OnClick="btnGridView_Click" Text="跳转" />
</td>
</tr>
</table>
</PagerTemplate>
</asp:GridView>
后台代码:
// 数据表绑定事件
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
switch(e.Row.RowType)
{
case DataControlRowType.Pager:// 标题头
for (int i = 0; i < gridViewTag.Length; i++)
{
try
{
GridView1.HeaderRow.Cells[i].Text = gridViewTag[i];
GridView1.HeaderRow.Cells[i].Wrap = true;
}
catch
{
}
}
break;
case DataControlRowType.DataRow:// 数据行
e.Row.Attributes.Add("onmouseover","this.style.backgroundColor='#E6F5FA'");
e.Row.Attributes.Add("onmouseout","this.style.backgroundColor='#FFFFFF'");
break;
}
}
// 导航条按钮事件
protected void btnGridView_Click(object sender, EventArgs e)
{
switch (((LinkButton)sender).CommandArgument.ToString())
{
case "first":
GridView1.PageIndex = 0;
break;
case "last":
GridView1.PageIndex = GridView1.PageCount - 1;
break;
case "prev":
GridView1.PageIndex -= 1;
break;
case "next":
GridView1.PageIndex += 1;
break;
case "go":
{
GridViewRow gvr = GridView1.BottomPagerRow;
TextBox tb = (TextBox)gvr.FindControl("txtNewPageIndex");
int res = Convert.ToInt32(tb.Text.ToString());
GridView1.PageIndex = res - 1;
}
break;
}
// BindTable();
}