加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net – 向GridView添加默认排序箭头

发布时间:2020-12-16 03:34:17 所属栏目:asp.Net 来源:网络整理
导读:我是 Asp.net的新手,我目前正在使用GridViews.我浏览了这个网站,其他人已经看到了如何向列标题添加排序箭头的提示. 到目前为止我已经这样做了: 设置这些GridView属性: SortedAscendingHeaderStyle-CssClass="sortasc"SortedDescendingHeaderStyle-CssClass
我是 Asp.net的新手,我目前正在使用GridViews.我浏览了这个网站,其他人已经看到了如何向列标题添加排序箭头的提示.

到目前为止我已经这样做了:

设置这些GridView属性:

SortedAscendingHeaderStyle-CssClass="sortasc"
SortedDescendingHeaderStyle-CssClass="sortdesc"

我的CSS有这个:

th.sortasc a  
{
   display:block; padding:0 4px 0 15px; 
   background:url("images/icons/ascArrow.png") no-repeat;
}

th.sortdesc a 
{
   display:block; padding:0 4px 0 15px; 
  background:url("images/icons/descArrow.png") no-repeat;
}

这非常适合在用户单击标题并对列进行排序后显示图像.

我现在遇到的问题是,我希望列默认显示图像,以便用户可以知道他们可以点击它们进行排序.有没有办法实现这个目标?

解决方法

您可以在RowCreated事件中显示用于排序gridview列行为的箭头,我通常这样做

protected void GridView1_RowCreated(object sender,GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.Header)
    {
        foreach (TableCell tc in e.Row.Cells)
        {
            if (tc.HasControls())
            {
                // search for the header link
                LinkButton lnk = (LinkButton)tc.Controls[0];
                if (lnk != null && GridView1.SortExpression == lnk.CommandArgument)
                {
                    // inizialize a new image
                    System.Web.UI.WebControls.Image img = new System.Web.UI.WebControls.Image();
                    // setting the dynamically URL of the image
                    img.ImageUrl = "~/img/ico_" + (GridView1.SortDirection == SortDirection.Ascending ? "asc" : "desc") + ".gif";
                    // adding a space and the image to the header link
                    tc.Controls.Add(new LiteralControl(" "));
                    tc.Controls.Add(img);

                }
            }
        }
    }
}

它还会在列的升序和降序排序中切换图像

代码实际上做的是它
循环遍历GridView标头以搜索LinkBut??ton(框架仅在设置了SortExpression属性时才创建它).然后,如果找到的LinkBut??ton是已排序的字段,那么它会将图像显示给输出,这就是全部

AnswerSource

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读