Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7522319
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T02:29:18+00:00 2026-05-30T02:29:18+00:00

I have a GridView with two columns that have capability to be sorted. After

  • 0

I have a GridView with two columns that have capability to be sorted. After its sorted I want to display an image next to a column with Arrow pointing up or down for Asc and Desc sort.

I cannot figure out how to reference an ImageButton object so I can set the ImageButton.ImageUrl to an actual image based on if its Asc and Desc.

Here is my .aspx code:

          <Columns>
            <asp:TemplateField>
              <HeaderTemplate>
                <asp:LinkButton ID="Name_SortLnkBtn" runat="server" Text="Name:" ToolTip="Click to Sort Column" CommandName="Sort" CommandArgument="Name" CausesValidation="false" />
                <asp:ImageButton ID="Name_SortImgBtn" runat="server" Visible="false" ToolTip="Click to Sort Column" CommandName="Sort" CommandArgument="Name" CausesValidation="false" />
              </HeaderTemplate>                    
              <ItemTemplate>
                 <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# "~/TestResults/Diabetes.aspx?ID="+Eval("ID") %>'><%#Eval("Name")%></asp:HyperLink>                                    
              </ItemTemplate>
            </asp:TemplateField>

            <asp:TemplateField>
              <HeaderTemplate>
                <asp:LinkButton ID="HouseName_SortLnkBtn" runat="server" Text="House Name:" ToolTip="Click to Sort Column" CommandName="Sort" CommandArgument="House" CausesValidation="false" />
                <asp:ImageButton ID="HouseName_SortImgBtn" runat="server" Visible="false" ToolTip="Click to Sort Column" CommandName="Sort" CommandArgument="House" CausesValidation="false" />
              </HeaderTemplate>                  
              <ItemTemplate><%#Eval("House")%></ItemTemplate>
            </asp:TemplateField>                 
          </Columns>

Help would be great appreciated.

Updated .aspx.cs file:

public partial class Home : System.Web.UI.Page
{
    protected _code.SearchSelection _SearchSelection = new _code.SearchSelection();
    protected _code.Utils _utils = new _code.Utils();
    protected ImageButton sortImage = new ImageButton();
    protected void Page_Load(object sender, EventArgs e) {
        //if (!IsPostBack) {
        Master.FindControl("Home").ID = "active";
        GridView1_DataBind();
        //Guid ID = new Guid(_SearchSelection.getUserID().Tables[0].Rows[0]["u_ID"].ToString());             
        //}
    }

    protected void GridView1_DataBind() {
        string selection = string.Empty;
        TreeView treeMain = (TreeView)tree.FindControl("treeMain");
        if (treeMain.SelectedNode != null)
            selection = treeMain.SelectedNode.Text;
        else
            selection = Session["Selection"].ToString();
        DataSet mainData = _utils.getStoreProcedure(new SqlParameter[] { new SqlParameter("@Selection", selection) }, "sp_getTenantsWithDiabetes", ConfigurationManager.ConnectionStrings["TIPS4"].ConnectionString);
        Session["MainData"] = mainData.Tables[0];
        GridView1.DataSource = mainData.Tables[0];
        GridView1.DataBind();
    }

    protected void GridView1_Sorting(object sender, GridViewSortEventArgs e) {

        //Retrieve the table from the session object.
        DataTable dt = Session["MainData"] as DataTable;
        ImageButton imageButton = new ImageButton();
        if (dt != null) {
            //Sort the data.
            dt.DefaultView.Sort = e.SortExpression + " " + GetSortDirection(e.SortExpression);
            //imageButton.ImageUrl = "~/App_Themes/Sugar2006/Images/arrow_up.gif";
            //imageButton.Visible = true;
            this.GridView1.DataSource = Session["MainData"];
            this.GridView1.DataBind();
        }
    }
    private string GetSortDirection(string column) {

        // By default, set the sort direction to ascending.
        string sortDirection = "ASC";

        // Retrieve the last column that was sorted.
        string sortExpression = ViewState["SortExpression"] as string;

        if (sortExpression != null) {
            // Check if the same column is being sorted.
            // Otherwise, the default value can be returned.
            if (sortExpression == column) {
                string lastDirection = ViewState["SortDirection"] as string;
                if ((lastDirection != null) && (lastDirection == "ASC")) {
                    sortDirection = "DESC";
                }
            }
        }

        // Save new values in ViewState.
        ViewState["SortDirection"] = sortDirection;
        ViewState["SortExpression"] = column;

        return sortDirection;
    }

    protected void gridView1_RowDataBound(object sender, GridViewRowEventArgs e) {
        if (e.Row.RowType == DataControlRowType.Header) {                               
            var imageButton = (ImageButton)e.Row.FindControl("Name_SortImgBtn");
            sortImage = imageButton;
            //imageButton.ImageUrl = "~/App_Themes/Sugar2006/Images/arrow_up.gif";
            //imageButton.Visible = true;
        }
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T02:29:20+00:00Added an answer on May 30, 2026 at 2:29 am

    To get a reference to the ImageButton defined in your HeaderTemplate, you can wire up the RowDataBound event of the GridView. In the event handler, check if the row is the header row by using the RowType property, and then use the FindControl method to get a reference to the control.

    protected void gridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if(e.Row.RowType == DataControlRowType.Header)
        {
            var imageButton = (ImageButton)e.Row.FindControl("Name_SortImgBtn");
            imageButton.ImageUrl = "~/myimage.gif";
        }
    }
    

    EDIT

    I think you’re on the right track. I would make the following changes:

    protected void GridView1_Sorting(object sender, GridViewSortEventArgs e) 
    {
        //Retrieve the table from the session object.
        DataTable dt = Session["MainData"] as DataTable;
        if (dt == null) return;
    
        //Sort the data
        dt.DefaultView.Sort = e.SortExpression + " " +
                              GetSortDirection(e.SortExpression);
        this.GridView1.DataSource = dt;
        this.GridView1.DataBind();
    }
    

    There’s no need to worry about the ImageButton in the Sorting event handler. The click of the LinkButton in the header will cause a post back, and the Sorting event handler will be called. It will run before the RowDataBound event is triggered (that won’t happen until the GridView1.DataBind method is called). Also, the GetSortDirection method will store the sort expression and the sort order in the ViewState. We’ll need those values later in the RowDataBound event handler (shown below).

    protected void gridView1_RowDataBound(object sender, GridViewRowEventArgs e) 
    {
        if (e.Row.RowType == DataControlRowType.Header) 
        {
            //Determine sort column and sort order
            var column = ViewState["SortExpression"] != null ? 
                         ViewState["SortExpression"].ToString() : string.Empty;
            var sortDirection = ViewState["SortDirection"] != null ? 
                         ViewState["SortDirection"].ToString() : string.Empty;
    
            //Find ImageButton based on sort column (return if not found)
            var imageButtonID = string.Concat(column, "_SortImgBtn");
            var imageButton = e.Row.FindControl(imageButtonID) as ImageButton;
            if(imageButton == null) return;
    
            //Determine sort image to display
            imageButton.ImageUrl = string.Equals("asc", sortDirection.ToLower()) ?
                                   "~/App_Themes/Sugar2006/Images/arrow_up.gif" :
                                   "~/App_Themes/Sugar2006/Images/arrow_down.gif";
            imageButton.Visible = true;
        }
    }
    

    In this event handler, we’ll retrieve the values stored in the ViewState to determine which ImageButton to make Visible and which image url to use based on the sort direction. I made the assumption that you have given the ImageButton controls an ID of the column name plus "_SortImgBtn" (if you do things in this manner you can avoid a switch statement to map the column to the control name). Just make sure that the ImageButton controls have Visible set to false in the front page and the sorting image should be displayed.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a GridView bound to an ICollection<UserAnswer> that needs to show two columns:
I have an ASP.NET GridView control with two asp:CommandField columns that are both using
I have a GridView that displays data on three columns. On the third column
I have a GridView populated from an ObjectDataSource with two items in its DataKeyNames
I have a gridview that displays items details, I added two template fields one
I have a asp:GridView and in there i have two columns , in one
I have an ASP.NET GridView which has columns that look like this: | Foo
I have a report page that can display two different reports. So I have
I have a gridview that binds its rows from an objects method: public DataSet
I have a GridView that has multiple columns. Let's say I have a business

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.