当前位置: 移动技术网 > IT编程>开发语言>.net > ASP.NET4 GridView的四种排序样式详解

ASP.NET4 GridView的四种排序样式详解

2017年12月12日  | 移动技术网IT编程  | 我要评论

ccv5影院,上海港湾学校学分制,关牧林

与asp.net 的其他web控件一能够,gridview控件拥有很多不同的css样式属性设置,包括象cssclass,font字体,forecolor,backcolor,backcolor, width, height等等。gridview还包括了一些应用在表格的行上的样式属性,比如rowstyle, alternatingrowstyle, headerstyle,和pagerstyle,它们都提供了象cssclass和font这些基本的属性设置。 

在 asp.net 4.0中的gridview控件中,新增加了四个样式属性:sortedascendingheaderstlye,sortedascendingcellstlye,sorteddescendingheaderstyle 和sorteddescendingcellstyle。这四个属性有点像以前的rowstyle和headerstyle样式属性,但它们是应用在 gridview的列的,而不是行。当gridview需要排序的时候,这些属性才起作用,如果当gridview需要按升序排序的话,那么 sortedascendingheaderstyle和sortedascendingcellstyle属性定义了数据排序时的样式风格。如果 gridview是降序排序时,sorteddescendingheaderstyle和sorteddescendingcellstyle属性则定义了排序时的样式风格。 

这四个新的特性使在排序的时候,更容易定制数据排序时列的外观样式。这些属性与css样式搭配使用的话,可以在表格排序时增加向上的箭头和向下的箭头,以表明当前是按升序还是降序排序。本文将介绍如何使用这四个新的属性的样式。 

gridview中的排序回顾 

在gridview中默认是文本的方式显示每一列的列头的。要排序的话,必须首先设置gridview的allowsorting属性,这将使 gridview将要排序的列以链接的方式展现,当用户点击时,就会触发排序的事件。如果gridview绑定到数据源控件了,则你不必编写任何代码去处理排序,一切都是自动完成的。 

从用户的角度来看,点击标题行中的排序列一下,则会让表格中的该列数据以升序排序,同样再点击一下,则以降序排序。遗憾的是,在以往asp.net 中的gridview不提供任何的方法以显示给用户看,当前的排序列是按升序排序还是以降序排序。在asp.net 4.0之前,要实现的唯一方法只有编写一些代码了,使用gridview增加sortascendingstyle 和sortdescendingstyle两个属性,并且使用了css去模拟画出两个上下的箭头。 

而在asp.net 4.0中,已经内置了这样的功能了,下面讲解下。 

新的排序相关样式属性 

asp.net 4.0中新增的4个排序相关的属性如下: 

• sortedascendingheaderstyle 当gridview以升序排列时,定义了排序列的表头样式。 

• sortedascendingcellstyle 当gridview以升序排列时,定义了要排序的数据列的样式。 

• sorteddescendingheaderstyle 当gridview以降序排列时,定义了排序列的表头样式。 

• sorteddescendingcellstyle 当gridview以降序排列时,定义了要排序的数据列的样式。 

有了这些属性,在排序时,只需要简单对它们进行设置就可以了,比如下面的例子中,简单设置了sortedascendingcellstyle属性和sorteddescendingcellstyle的子样式背景颜色为黄色,马上就可以看到效果了:

<asp:gridview id="..." runat="server" autogeneratecolumns="false" allowsorting="true" 
   ... 
   sortedascendingcellstyle-backcolor="yellow" 
   sorteddescendingcellstyle-backcolor="yellow"> 
  ... 
</asp:gridview>

 
 
当然,为了观察方便,可以设置 sortedascendingcellstyle-backcolor和sorteddescendingcellstyle-backcolor为不同的颜色则可看到更清晰的效果。 
为排序列加上箭头 

使用的css配合sortedascendingheaderstyle和sorteddescendingheaderstyle两个属性,则为排序列增加向上和向下箭头的表示排序状态是很容易的。首先,你需要找一些向上箭头或者向下箭头的图片,在本文的代码下载中是有这样的图片了。接者需要创建两个 css类,比如下文中的sortasc—header和sortdesc-header,在这两个css类中需要指定上下箭头图片所在的位置,同时我们要在排序列的右边定义一个适当的间隔位置,以便让向上和向下箭头不被排序列所在的表头的文本所覆盖。如下所示: 

.sortasc-header a 
{ 
  background:url(url to up arrow image) right center no-repeat; 
} 

.sortdesc-header a 
{ 
  background:url(url to down arrow image) right center no-repeat; 
} 

th a 
{ 
  padding-right: 20px; 
}

 之后我们就可以利用这些样式了: 

<asp:gridview id="..." runat="server" autogeneratecolumns="false" allowsorting="true" 
   ... 
   sortedascendingheaderstyle-cssclass="sortasc-header" 
   sorteddescendingheaderstyle-cssclass="sortdesc-header" 
   sortedascendingcellstyle-backcolor="yellow" 
   sorteddescendingcellstyle-backcolor="yellow"> 
  ... 
</asp:gridview>

 

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网