思路是GridView中相应功能进行重写,并重新封装成新控件。
一、新建类工程
SmartGridView.cs
Code
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Collections;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.ComponentModel;
namespace MIDI.CustmerControl
{
[ToolboxData(@"<{0}:CustGrid2 runat='server'></{0}:CustGrid2>")] //必须和类名相同
public class CustGrid2 : GridView
{
protected override void OnRowCreated(GridViewRowEventArgs e)
{
//判断是否是分页行且允许分页(AllowPaging属性)
if (e.Row.RowType == DataControlRowType.Pager && this.AllowPaging)
{
//定义四个按扭表示"上一页","下一页","首页","尾页"
LinkButton First = new LinkButton();
LinkButton Prev = new LinkButton();
LinkButton Next = new LinkButton();
LinkButton Last = new LinkButton();
//新建一行作为分页行
TableCell tc = new TableCell();
//分页行所跨列(具体视GridView列数来定,这里表示有3列数据)
tc.ColumnSpan = 2;
//清除分页行里的控件
e.Row.Controls.Clear();
//为自定义的按扭定义为GridView分页按扭
First.Text = PagerSettings.FirstPageText;
Prev.Text = PagerSettings.PreviousPageText;
Next.Text = PagerSettings.NextPageText;
Last.Text = PagerSettings.LastPageText;
First.CommandName = Prev.CommandName = Next.CommandName = Last.CommandName = "page";
First.CommandArgument = "first";
Prev.CommandArgument = "prev";
Next.CommandArgument = "next";
Last.CommandArgument = "last";
//当前页是第一页,则"上一页"按扭设为无效
if (this.PageIndex <= 0)
{
First.Enabled = Prev.Enabled = false;
}
else
{
First.Enabled = Prev.Enabled = true;
}
//当前页是尾页,则"下一页"按扭设为无效
if (this.PageIndex >= PageCount - 1)
{
Next.Enabled = Last.Enabled = false;
}
else
{
Next.Enabled = Last.Enabled = true;
}
tc.RowSpan = this.Columns.Count;
//将以上两个分页按扭添加到分页行内
tc.Controls.Add(First);
tc.Controls.Add(new LiteralControl(" "));
tc.Controls.Add(Prev);
tc.Controls.Add(new LiteralControl(" "));
tc.Controls.Add(Next);
tc.Controls.Add(new LiteralControl(" "));
tc.Controls.Add(Last);
tc.RowSpan = this.Columns.Count;
AddNumberList(tc);
e.Row.Controls.Add(tc);
//base.OnRowCreated(e);
}
}
private void AddNumberList(TableCell tc)
{
for (int i = 0; i < PageCount; i++)
{
//如果是当前页,则用红色标识,且不可用
if (i == PageIndex)
{
tc.Controls.Add(new LiteralControl("<span style='color:red;font-weight:bold'>" + (i + 1).ToString() + "</span>"));
}
else
{
//自定义LinkButton,并将其设置为GridView数字分页按扭
LinkButton lb = new LinkButton();
lb.Text = (i + 1).ToString();
lb.CommandName = "Page";
lb.CommandArgument = (i + 1).ToString();
tc.Controls.Add(lb);
}
}
tc.Controls.Add(new LiteralControl(" "));
}
}
编译后,在工具箱里,添加该工具Dll 出现自定义GridView图标。
测试.aspx
Code
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Grid自定义分页.aspx.cs" Inherits="DivMenusTest" %>
<%@ Register Assembly="UIClasLibrary" Namespace="MIDI.CustmerControl" TagPrefix="cc2" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>SmartGridView测试</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectMethod="Select" TypeName="ObjData"></asp:ObjectDataSource>
<cc2:SmartGridView ID="SmartGridView1" runat="server" AllowPaging="True" AllowSorting="True"
AutoGenerateColumns="False" PagingStyle="Default" DataSourceID="ObjectDataSource1">
<Columns>
<asp:BoundField DataField="no" HeaderText="序号" SortExpression="no" >
<itemstyle width="100px" />
</asp:BoundField>
<asp:BoundField DataField="name" HeaderText="名称" SortExpression="name" >
<itemstyle width="100px" />
</asp:BoundField>
</Columns>
<PagerSettings PageButtonCount="5" />
</cc2:SmartGridView>
</div>
</form>
</body>
</html
ObjectDataSoure数据源
.cs
Code
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.ComponentModel;
/**/
/// <summary>
/// ObjData 的摘要说明
/// </summary>
public class ObjData
{
public ObjData()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
[DataObjectMethod(DataObjectMethodType.Select, true)]
public DataTable Select()
{
DataTable dt = new DataTable();
dt.Columns.Add("no", typeof(string));
dt.Columns.Add("name", typeof(string));
for (int i = 0; i < 60; i++)
{
DataRow dr = dt.NewRow();
dr[0] = "no" + i.ToString().PadLeft(2, '0');
dr[1] = "name" + i.ToString().PadLeft(2, '0');
dt.Rows.Add(dr);
}
return dt;
}