All articles| All Pictures| All Softwares| All Video| Go home page| Write articles| Upload pictures

Reading number is top 10 articles
delphi浮动工具栏实例
ASP.NET应用技巧:正则学习之组的定义及引用方式_.net资料_编程技术
在GridView中实现数据并列显示_[Asp.Net教程]
详解ASP.NET数据读取与填充方式_[Asp.Net教程]
PHP实例:动态网页PHP+MYSQL如何插入记录到数据库_[PHP教程]
SQLSERVER2005的混和身份验证模式的设置以及SA登陆问题_[SQL Server教程]
SQL,Server,2005数据库产品线的扩展_[SQL,Server教程]
几种分页算法。翻页必备_[SQL,Server教程]
数据库绑定控件DataGridView属性、方法和事件
CView类的GetDocument()成员函数
Reading number is top 10 pictures
Beautiful Japanese beauty(漂亮的日本美女)2
星星命名法则
Sora aoi in China1
这张图有两句话,你看出来了吗?
The money of more than 100 countries and regions2
The money of more than 100 countries and regions4
2012 national geographic daily picture6
Cesarean section, bloody, silently into it!1
西游四格漫画(二)
The terra-cotta warriors1
Download software ranking
I'm come from Beijing1
天龙八部十二宫服务端
Tram sex maniac 2 (H) rar bag5
SP4 for SQL2000
少妇苏霞全本
Unix video tutorial18
Tram sex maniac 2 (H) rar bag3
Kung.Fu.Panda.2
jBuilder2006
asp.netWeb服务器高级编程
delv published in(发表于) 2014/1/16 9:33:39 Edit(编辑)
扩展GridView(二)——给字段标题加上排序状态_[Asp.Net教程]

扩展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】







添加到del.icio.us 添加到新浪ViVi 添加到百度搜藏 添加到POCO网摘 添加到天天网摘365Key 添加到和讯网摘 添加到天极网摘 添加到黑米书签 添加到QQ书签 添加到雅虎收藏 添加到奇客发现 diigo it 添加到饭否 添加到飞豆订阅 添加到抓虾收藏 添加到鲜果订阅 digg it 貼到funP 添加到有道阅读 Live Favorites 添加到Newsvine 打印本页 用Email发送本页 在Facebook上分享


Disclaimer Privacy Policy About us Site Map

If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.