ASP.NET GridView 分页模板及标题定制和加序号

前台代码:

<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();
    }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值