asp教程.net GridView多列排序及排序方向图标代码
public class WebGridView : GridView
{
/**/
///
/// 是否启用或者禁止多列排序
///
[
Description("是否启用多列排序功能"),
Category("排序"),
DefaultValue("false"),
]
public bool AllowMultiColumnSorting
{
get
{
object o = ViewState["EnableMultiColumnSorting"];
return (o != null ? (bool)o : false);
}
set
{
AllowSorting = true;
ViewState["EnableMultiColumnSorting"] = value;
}
}
/**/
///
/// 升序时显示图标
///
[
Description("升序时显示图标"),
Category("排序"),
Editor("System.Web.UI.Design.UrlEditor", typeof(System.Drawing.Design.UITypeEditor)),
DefaultValue(""),]
public string SortAscImageUrl
{
get
{
object o = ViewState["SortImageAsc"];
return (o != null ? o.ToString() : "");
}
set
{
ViewState["SortImageAsc"] = value;
}
}
/**/
///
/// 降序时显示图标
///
[
Description("降序时显示图标"),
Category("排序"),
Editor("System.Web.UI.Design.UrlEditor", typeof(System.Drawing.Design.UITypeEditor)),
DefaultValue(""),
]
public string SortDescImageUrl
{
get
{
object o = ViewState["SortImageDesc"];
return (o != null ? o.ToString() : "");
}
set
{
ViewState["SortImageDesc"] = value;
}
}
protected override void OnSorting(GridViewSortEventArgs e)
{
if (AllowMultiColumnSorting)
{
e.SortExpression = GetSortExpression(e);
}base.OnSorting(e);
}
protected override void OnRowCreated(GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
if (SortExpression != String.Empty)
{
DisplaySortOrderImages(SortExpression, e.Row);
this.CreateRow(0, 0, DataControlRowType.EmptyDataRow, DataControlRowState.Normal);
}
}
base.OnRowCreated(e);
}
/**/
///
/// 获取排序表达式
///
protected string GetSortExpression(GridViewSortEventArgs e)
{
string[] sortColumns = null;
string sortAttribute = SortExpression;if (sortAttribute != String.Empty)
{
sortColumns = sortAttribute.Split(",".ToCharArray());
}
if (sortAttribute.IndexOf(e.SortExpression) > 0 || sortAttribute.StartsWith(e.SortExpression))
{
sortAttribute = ModifySortExpression(sortColumns, e.SortExpression);
}
else
{
sortAttribute += String.Concat(",", e.SortExpression, " ASC ");
}
return sortAttribute.TrimStart(",".ToCharArray()).TrimEnd(",".ToCharArray());}
/**/
///
/// 修改排序顺序
///
protected string ModifySortExpression(string[] sortColumns, string sortExpression)
{
string ascSortExpression = String.Concat(sortExpression, " ASC ");
string descSortExpression = String.Concat(sortExpression, " DESC ");for (int i = 0; i < sortColumns.Length; i++)
{if (ascSortExpression.Equals(sortColumns[i]))
{
sortColumns[i] = descSortExpression;
}else if (descSortExpression.Equals(sortColumns[i]))
{
Array.Clear(sortColumns, i, 1);
}
}return String.Join(",", sortColumns).Replace(",,", ",").TrimStart(",".ToCharArray());
}
/**/
///
/// 获取当前的表达式对所选列进行排序
///
protected void SearchSortExpression(string[] sortColumns, string sortColumn, out string sortOrder, out int sortOrderNo)
{
sortOrder = "";
sortOrderNo = -1;
for (int i = 0; i < sortColumns.Length; i++)
{
if (sortColumns[i].StartsWith(sortColumn))
{
sortOrderNo = i + 1;
if (AllowMultiColumnSorting)
{
sortOrder = sortColumns[i].Substring(sortColumn.Length).Trim();
}
else
{
sortOrder = ((SortDirection == SortDirection.Ascending) ? "ASC" : "DESC");
}
}
}
}
/**/
///
/// 绘制升序降序的图片
///
protected void DisplaySortOrderImages(string sortExpression, GridViewRow dgItem)
{
string[] sortColumns = sortExpression.Split(",".ToCharArray());for (int i = 0; i < dgItem.Cells.Count; i++)
{
if (dgItem.Cells[i].Controls.Count > 0 && dgItem.Cells[i].Controls[0] is LinkButton)
{
string sortOrder;
int sortOrderNo;
string column = ((LinkButton)dgItem.Cells[i].Controls[0]).CommandArgument;
SearchSortExpression(sortColumns, column, out sortOrder, out sortOrderNo);
if (sortOrderNo > 0)
{
string sortImgLoc = (sortOrder.Equals("ASC") ? SortAscImageUrl : SortDescImageUrl);if (sortImgLoc != String.Empty)
{
Image imgSortDirection = new Image();
imgSortDirection.ImageUrl = sortImgLoc;
dgItem.Cells[i].Controls.Add(imgSortDirection);}
else
{if (AllowMultiColumnSorting)
{
Literal litSortSeq = new Literal();
litSortSeq.Text = sortOrderNo.ToString();
dgItem.Cells[i].Controls.Add(litSortSeq);}
}
}
}
}}
}
创造与魔法 安卓版v1.0.0750
创造与魔法是一款开放世界手游,在游戏中玩家可探索这个奇妙的世
创造与魔法修改版 最新版v1.0.0750
创造与魔法无限点券版是款探索冒险游戏,该款游戏的操作还是蛮自
战争与文明官方版本 安卓版v1.7.16
战争与文明是一款由上海邮通科技有限公司开发的战争策略游戏,这
迷你世界0元领皮肤无限迷你币版 最新安卓版v1.43.0
迷你世界0元购买皮肤版是这款开放沙盒冒险建造游戏的特殊破解版
创造与魔法无限经验版 安卓版v1.0.0750
创造与魔法无限经验版是款可以改造环境,整个游戏的自由度还是蛮