GridView既强大又好用。为了让它更强大、更好用,我们来写一个继承自GridView的控件。
[源码下载]
http://files.cnblogs.com/webabcd/yycontrols.rar
\n
扩展GridView(二)——给字段标题加上排序状态
\n
介绍
在用GridView自带的排序功能排序时,无法直观的知道当前是通过哪个字段排序?是升序还是降序?所以扩展一下,用图片或文字的形式来提示一下当前是根据哪个字段排序,是升序还是降序。
\n
控件开发
1、新建一个继承自GridView的类。
1、新建一个继承自GridView的类。
复制C#代码保存代码/// <summary>
/// 继承自GridView
/// </summary>
[
ToolboxData(@"<{0}:SmartGridView runat=\'server\'></{0}:SmartGridView>"),
ParseChildren(true),
PersistChildren(false)
]
public class SmartGridView : GridView
{
}
2、新建一个SortTip实体类,有4个属性,分别是降序提示图片、升序提示图片、降序提示文本和升序提示文本
复制C#代码保存代码using System;
using System.Collections.Generic;
using System.Text;
\n
using System.ComponentModel;
\n
namespace YYControls.SmartGridView
{
/// <summary>
/// 排序提示类
/// </summary>
[TypeConverter(typeof(ExpandableObjectConverter))]
public class SortTip
{
private string _sortDescImage;
\n
/// <summary>
/// 降序提示图片
/// </summary>
[
Description("降序提示图片"),
Category("扩展"),
Editor("System.Web.UI.Design.UrlEditor", typeof(System.Drawing.Design.UITypeEditor)),
DefaultValue(""),
NotifyParentProperty(true)
]
public string SortDescImage
{
get { return _sortDescImage; }
set { _sortDescImage = value; }
}
\n
private string _sortAscImage;
\n
/// <summary>
/// 升序提示图片
/// </summary>
[
Description("升序提示图片"),
Category("扩展"),
Editor("System.Web.UI.Design.UrlEditor", typeof(System.Drawing.Design.UITypeEditor)),
DefaultValue(""),
NotifyParentProperty(true)
]
public string SortAscImage
{
get { return _sortAscImage; }
set { _sortAscImage = value; }
}
\n
private string _sortDescText;
\n
/// <summary>
/// 降序提示文本
/// </summary>
[
Description("降序提示文本"),
Category("扩展"),
DefaultValue(""),
NotifyParentProperty(true)
]
public string SortDescText
{
get { return _sortDescText; }
set { _sortDescText = value; }
}
\n
private string _sortAscText;
\n
/// <summary>
/// 升序提示文本
/// </summary>
[
Description("升序提示文本"),
Category("扩展"),
DefaultValue(""),
NotifyParentProperty(true)
]
public string SortAscText
{
get { return _sortAscText; }
set { _sortAscText = value; }
}
\n
/// <summary>
/// ToString()
/// </summary>
/// <returns></returns>
public override string ToString()
{
return “SortTip”;
}
}
}
3、在继承自GridView的那个类中加1个复杂对象属性,这个复杂对象就是第2步创建的那个SortTip
复制C#代码保存代码private SortTip _sortTip;
\n
/// <summary>
/// 排序提示信息
/// </summary>
[
Description("排序提示信息"),
Category("扩展"),
DesignerSerializationVisibility(DesignerSerializationVisibility.Content),
PersistenceMode(PersistenceMode.InnerProperty)
]
public virtual SortTip SortTip
{
get
{
if (_sortTip == null)
{
_sortTip = new SortTip();
}
return _sortTip;
}
}
4、在继承自GridView的那个类的构造函数内加一个RowDataBound事件的处理,在该事件内实现给字段标题加上排序状态的功能。
复制C#代码保存代码/// <summary>
/// 构造函数
/// </summary>
public SmartGridView()
: base()
{
// 新增“SmartGridView_RowDataBound”事件处理
this.RowDataBound += new GridViewRowEventHandler(SmartGridView_RowDataBound);
}
\n
/// <summary>
/// RowDataBound事件
/// </summary>
/// <param name=”sender”>sender</param>
/// <param name=”e”>e</param>
protected void SmartGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
SmartGridView sgv = (SmartGridView) sender;
\n
if (e.Row.RowType == DataControlRowType.Header)
{
// GridViewRow的每个TableCell
for (int i = 0; i < e.Row.Cells.Count; i++)
{
// TableCell里有一个Control并且是LinkButton
if (e.Row.Cells[i].Controls.Count == 1 && e.Row.Cells[i].Controls[0] is LinkButton)
{
// LinkButton的命令参数等于排序字段
if (((LinkButton) e.Row.Cells[i].Controls[0]).CommandArgument == this.SortExpression)
{
Image img = null;
Label lbl = null;
\n
// 升序
if (this.SortDirection == SortDirection.Ascending)
{
// 升序图片
if (!String.IsNullOrEmpty(_sortTip.SortAscImage))
{
img = new Image();
img.ImageUrl = base.ResolveUrl(_sortTip.SortAscImage);
}
// 升序文字
if (!String.IsNullOrEmpty(_sortTip.SortAscText))
{
lbl = new Label();
lbl.Text = _sortTip.SortAscText;
}
}
// 降序
else if (this.SortDirection == SortDirection.Descending)
{
// 降序图片
if (!String.IsNullOrEmpty(_sortTip.SortDescImage))
{
img = new Image();
img.ImageUrl = base.ResolveUrl(_sortTip.SortDescImage);
}
// 降序文字
if (!String.IsNullOrEmpty(_sortTip.SortDescText))
{
lbl = new Label();
lbl.Text = _sortTip.SortDescText;
}
}
\n
// TableCell里加上图片
if (img != null)
{
e.Row.Cells[i].Controls.Add(img);
}
// TableCell里加上文字
if (lbl != null)
{
e.Row.Cells[i].Controls.Add(lbl);
}
}
}
}
}
}
\n
控件使用
添加这个控件到工具箱里,然后拖拽到webform上,设置其SortTip下的4个属性即可。SortAscImage是升序提示图片;SortAscText是升序提示文本;SortDescImage是降序提示图片;SortDescText是降序提示文本
ObjData.cs
复制C#代码保存代码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;
\n
using System.ComponentModel;
\n
/// <summary>
/// OjbData 的摘要说明
/// </summary>
public class OjbData
{
public OjbData()
{
//
// TOD 在此处添加构造函数逻辑
//
}
\n
[DataObjectMethod(DataObjectMethodType.Select, true)]
public DataTable Select()
{
DataTable dt = new DataTable();
dt.Columns.Add(“no”, typeof(string));
dt.Columns.Add(“name”, typeof(string));
\n
for (int i = 0; i < 30; i++)
{
DataRow dr = dt.NewRow();
dr[0] = “no” + i.ToString().PadLeft(2, ’0′);
dr[1] = “name” + i.ToString().PadLeft(2, ’0′);
\n
dt.Rows.Add(dr);
}
\n
return dt;
}
}
Default.aspx
复制ASPX代码保存代码<%@ Page Language=”C#” AutoEventWireup=”true” CodeFile=”Default.aspx.cs” Inherits=”_Default” %>
\n
<!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 runat=”server”>
<title>无标题页</title>
</head>
<body>
<form id=”form1″ runat=”server”>
<div>
<yyc:SmartGridView ID=”SmartGridView1″ runat=”server” AutoGenerateColumns=”True”
AllowSorting=”True” DataSourceID=”ObjectDataSource1″>
<Columns>
<asp:ButtonField CommandName=”ConfirmTest” Text=”确认按钮测试” />
</Columns>
<ConfirmButtons>
<yyc:ConfirmButton ConfirmMessage=”确认删除吗?” CommandName=”ConfirmTest”></yyc:ConfirmButton>
</ConfirmButtons>
<SortTip SortAscImage=”~/Images/asc.gif” SortDescImage=”~/Images/desc.gif” />
</yyc:SmartGridView>
<asp:ObjectDataSource ID=”ObjectDataSource1″ runat=”server” SelectMethod=”Select”
TypeName=”OjbData”></asp:ObjectDataSource>
</div>
</form>
</body>
</html>
\n
转自【webabcd-.NET】