添加gridview头图像(asc / desc)onload

时间:2021-07-24 14:47:47

I have a gridview with filtering/sorting functions working properly. My problem is that onload, it does not show asc/desc image right after my header. Sorting image shows only after clicking any header. Any help would be appreciated.

我有一个gridview,过滤/排序功能正常工作。我的问题是onload,它在我的标题之后没有显示asc / desc图像。只有在单击任何标题后才会显示排序图像。任何帮助,将不胜感激。

Codebehind:

代码隐藏:

#region Sorting Arrows for Gridview Header
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.Header)
    {
        foreach (TableCell tc in e.Row.Cells)
        {
            // search for the header link
            LinkButton lnk = (LinkButton)tc.Controls[0];
            if (tc.HasControls() && 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 = "~/Contents/Images/" + (GridView1.SortDirection == SortDirection.Ascending ? "asc" : "desc") + ".png";
                // adding a space and the image to the header link
                tc.Controls.Add(new LiteralControl(" "));
                tc.Controls.Add(img);
                //
            }
        }
    }
}
#endregion

1 个解决方案

#1


2  

I just figured it out..

我只想出来..

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {       
        Image img = new Image();
        img.ImageUrl = "~/Contents/Images/asc.png";
        GridView1.HeaderRow.Cells[1].Controls.Add(new LiteralControl(" "));
        GridView1.HeaderRow.Cells[1].Controls.Add(img);
    }
}

Thanks..

谢谢..

#1


2  

I just figured it out..

我只想出来..

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {       
        Image img = new Image();
        img.ImageUrl = "~/Contents/Images/asc.png";
        GridView1.HeaderRow.Cells[1].Controls.Add(new LiteralControl(" "));
        GridView1.HeaderRow.Cells[1].Controls.Add(img);
    }
}

Thanks..

谢谢..