Go homepage(回首页)
Upload pictures (上传图片)
Write articles (发文字帖)

The author:(作者)delv
published in(发表于) 2014/1/16 9:33:39
扩展GridView(二)——给字段标题加上排序状态_[Asp.Net教程]

扩展GridView(二)——给字段标题加上排序状态_[Asp.Net教程]

GridView既强大又好用。为了让它更强大、更好用,我们来写一个继承自GridView的控件。
[源码下载]
http://files.cnblogs.com/webabcd/yycontrols.rar


扩展GridView(二)——给字段标题加上排序状态


介绍
在用GridView自带的排序功能排序时,无法直观的知道当前是通过哪个字段排序?是升序还是降序?所以扩展一下,用图片或文字的形式来提示一下当前是根据哪个字段排序,是升序还是降序。


控件开发
1、新建一个继承自GridView的类。
1、新建一个继承自GridView的类。
复制C#代码保存代码///


/// 继承自GridView
///

[
ToolboxData(@"<{0}:SmartGridView runat='server'>"),
ParseChildren(true),
PersistChildren(false)
]
public class SmartGridView : GridView
{
}
2、新建一个SortTip实体类,有4个属性,分别是降序提示图片、升序提示图片、降序提示文本和升序提示文本
复制C#代码保存代码using System;
using System.Collections.Generic;
using System.Text;


using System.ComponentModel;


namespace YYControls.SmartGridView
{
///


/// 排序提示类
///

[TypeConverter(typeof(ExpandableObjectConverter))]
public class SortTip
{
private string _sortDescImage;


///


/// 降序提示图片
///

[
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; }
}


private string _sortAscImage;


///


/// 升序提示图片
///

[
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; }
}


private string _sortDescText;


///


/// 降序提示文本
///

[
Description("降序提示文本"),
Category("扩展"),
DefaultValue(""),
NotifyParentProperty(true)
]
public string SortDescText
{
get { return _sortDescText; }
set { _sortDescText = value; }
}


private string _sortAscText;


///


/// 升序提示文本
///

[
Description("升序提示文本"),
Category("扩展"),
DefaultValue(""),
NotifyParentProperty(true)
]
public string SortAscText
{
get { return _sortAscText; }
set { _sortAscText = value; }
}


///


/// ToString()
///

///
public override string ToString()
{
return "SortTip";
}
}
}
3、在继承自GridView的那个类中加1个复杂对象属性,这个复杂对象就是第2步创建的那个SortTip
复制C#代码保存代码private SortTip _sortTip;


///


/// 排序提示信息
///

[
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#代码保存代码///
/// 构造函数
///

public SmartGridView()
: base()
{
// 新增“SmartGridView_RowDataBound”事件处理
this.RowDataBound += new GridViewRowEventHandler(SmartGridView_RowDataBound);
}



///


/// RowDataBound事件
///

/// sender
/// e
protected void SmartGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
SmartGridView sgv = (SmartGridView) sender;


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;


// 升序
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;
}
}


// TableCell里加上图片
if (img != null)
{
e.Row.Cells[i].Controls.Add(img);
}
// TableCell里加上文字
if (lbl != null)
{
e.Row.Cells[i].Controls.Add(lbl);
}
}
}
}
}
}


控件使用
添加这个控件到工具箱里,然后拖拽到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;


using System.ComponentModel;


///


/// OjbData 的摘要说明
///

public class OjbData
{
public OjbData()
{
//
// TOD 在此处添加构造函数逻辑
//
}


[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 < 30; 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;
}
}
Default.aspx
复制ASPX代码保存代码<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>





无标题页





AllowSorting="True" DataSourceID="ObjectDataSource1">








TypeName="OjbData">




转自【webabcd-.NET】







If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)





QQ:154298438
QQ:417480759