C# 在ASP.Net中,图像不显示在Gridview的标题中

C# 在ASP.Net中,图像不显示在Gridview的标题中,c#,asp.net,sorting,gridview,C#,Asp.net,Sorting,Gridview,我使用的是Visual Studio 2012,我使用的是具有排序功能的GridView,排序工作非常完美,但我无法在标题部分显示图像(对图像进行升序或降序排序)。我的代码是: <asp:GridView ID="gv" runat="server" Width="50%" CssClass= "GridStyle-DarkGray" AllowSorting="True" AutoGenerateColumns="False" OnRowCreated=

我使用的是Visual Studio 2012,我使用的是具有排序功能的GridView,排序工作非常完美,但我无法在标题部分显示图像(对图像进行升序或降序排序)。我的代码是:

<asp:GridView ID="gv" runat="server" Width="50%" CssClass= "GridStyle-DarkGray"                  AllowSorting="True" AutoGenerateColumns="False" OnRowCreated="gv_RowCreated"    OnSorting="gv_Sorting">
     <Columns>
        <asp:BoundField DataField="CompanyID" HeaderText="ID"         SortExpression="CompanyID" />
        <asp:BoundField DataField="CompanyName" HeaderText="Company Name"  SortExpression="CompanyName" />
        <asp:BoundField DataField="PhoneNo" HeaderText="Phone No" SortExpression="PhoneNo" />
    </Columns>

</asp:GridView>
事件:

public SortDirection GridViewSortDirection
{
    get
    {
        if (ViewState["sortDirection"] == null)
            ViewState["sortDirection"] = SortDirection.Ascending;

        return (SortDirection)ViewState["sortDirection"];
    }
    set { ViewState["sortDirection"] = value; }
}
protected void gv_Sorting(object sender, GridViewSortEventArgs e)
{
    string sortExpression = e.SortExpression;

    if (GridViewSortDirection == SortDirection.Ascending)
    {
        GridViewSortDirection = SortDirection.Descending;
        SortGridView(sortExpression, DESCENDING);
    }
    else
    {
        GridViewSortDirection = SortDirection.Ascending;
        SortGridView(sortExpression, ASCENDING);
    }
}

private void SortGridView(string sortExpression, string direction)
{
    string conn = "Data Source=localhost\\SQLEXPRESS;Initial Catalog=IMS;Integrated Security=True;";
    string qry = "Select * from Company";
    DataTable dt = new DataTable();
    SqlDataAdapter DA = new SqlDataAdapter(qry, conn);
    DA.Fill(dt);
    gv.DataSource = dt;
    gv.DataBind();
    if (dt != null)
    {
        DataView dv = new DataView(dt);
        dv.Sort = sortExpression + direction;
        gv.DataSource = dv;
        gv.DataBind();
    }
}

protected void gv_RowCreated(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.Header)
    {
        //Call the GetSortColumnIndex helper method to determine
        //the index of the column being sorted.

        int sortColumnIndex = GetSortColumnIndex();

        if (sortColumnIndex != -1)
        {
            // Call the AddSortImage helper method to add
            // a sort direction image to the appropriate
            // column header. 
            AddSortImage(sortColumnIndex, e.Row);
        }
    }
}

int GetSortColumnIndex()
{
    // Iterate through the Columns collection to determine the index
    // of the column being sorted.
    foreach (DataControlField field in gv.Columns)
    {
        if (field.SortExpression == gv.SortExpression)
        {
            return gv.Columns.IndexOf(field);
        }
    }

    return -1;
}

// This is a helper method used to add a sort direction
// image to the header of the column being sorted.
void AddSortImage(int columnIndex, GridViewRow headerRow)
{
    // Create the sorting image based on the sort direction.
    Image sortImage = new Image();
    if (gv.SortDirection == SortDirection.Ascending)
    {
        sortImage.ImageUrl = "~/Img/asc.gif";
        Image1.ImageUrl = "~/img/asc.gif";
        sortImage.AlternateText = "Ascending Order";
        lab.Text = "Ascending";
    }
    else
    {
        sortImage.ImageUrl = "~/img/desc.gif";
        Image1.ImageUrl = "~/img/desc.gif";
        lab.Text = "Descending";
        sortImage.AlternateText = "Descending Order";
    }

    // Add the image to the appropriate header cell.
    headerRow.Cells[columnIndex].Controls.Add(sortImage); 
}

我的代码没有在标题中显示图像有什么问题…?

将此添加到后面的代码中

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);
    }
}

使用图像单元格索引更改单元格索引

使用一个css类,在该类中提供图像作为背景,并将该css类应用于
标记。我没有收到任何错误,但当我运行程序时,它仍然不会在标题中显示任何图像。图像路径也是正确的。加载页面时,它会在列索引1处显示图像。但是当我分类的时候。平均点击标题进行排序它不显示任何排序图像我的有问题:/