How add sort direction icons next to the header text in DataGrid (ASP.NET 1.1)

This question belongs to FAQ for a long time, but for me as newest in ASP.NET it was not obviously.
So I’d like to present the solution which is mine transformation from ASP.NET 2.0 to ASP.NET 1.1 of found in the Internet.
Original solution can be found there.

public class MyPage: System.Web.UI.Page
{

        protected DataGrid myDataGrid;

        private void Page_Load(object sender, System.EventArgs e)
        {
                if (!Page.IsPostBack)
                {
                        DataBinder();
                }
        }

        // method bind source data to DataGrid control
        private void DataBinder()
        {                     
                DataView objDataView = new DataView(/* …get DateTable object here … */);
                objDataView.Sort = SortExpression;
                myDataGrid.DataSource = objDataView;
                myDataGrid.DataBind();
        }

        // property stores latest sort direction of DataGrid between states
        public string SortDirection
        {
                get
                {
                        return Session[“myDataGridSortDirection”] == null
                                ? “ASC”
                                : Session[“myDataGridSortDirection”].ToString();
                }
                set
                {
                        Session[“myDataGridSortDirection”] = value;
                }
        }

        // property stores latest sort experession of DateGrid between states
        public string SortExpression
        {
                get
                {
                        return Session[“myDataGridSortExpression”] == null
                                ? “ID”
                                : Session[“myDataGridSortExpression”].ToString();
                }
                set
                {
                        Session[“myDataGridSortExpression”] = value;
                }
        }

        // binding event handlers
        private void InitializeComponent()
        {
                this.myDataGrid.ItemCreated
                        += new DataGridItemEventHandler(this.myDataGrid_ItemCreated);
                this.myDataGrid.SortCommand += new DataGridSortCommandEventHandler(this.myDataGrid_SortCommand);
                this.Load += new System.EventHandler(this.Page_Load);

        }

        // below handler is invoked for render every output item of DataGrid
        // this is place and moment when we will add icon image which will show current sort direction
        private void myDataGrid_ItemCreated(object sender, DataGridItemEventArgs e)
        {
                if (e.Item.ItemType != ListItemType.Header) return;
                if (!e.Item.HasControls()) return;

                foreach (Control control in e.Item.Controls)
                {
                        if (!control.HasControls()) continue;
                        LinkButton button = (LinkButton)control.Controls[0];

                        if (button != null && SortExpression.IndexOf(button.CommandArgument) != -1)
                        {
                                HtmlImage image = new HtmlImage();
                                image.Src = “images/sort_”
                                        + ( SortExpression.IndexOf(“ASC”) != -1
                                                ? “asc”
                                                : “desc” )
                                        + “.gif”;
                                control.Controls.Add(image);
                                break;
                        }
                }
        }

        // sort event handler
        private void myDataGrid_SortCommand(object source, DataGridSortCommandEventArgs e)
        {
                SortExpression = e.SortExpression.ToString() + ” “ + SortDirection;
                SortDirection = SortDirection == “ASC” ? “DESC” : “ASC”;
                DataBinder();
        }

}

Comment about SortDirection and SortExpression properties:
I prefer use Session object here, because it gives possibility to restore value, when user left section and then goes back, instead of ViewState, which usually is used for this in almost of solution which I saw in the Internet.

One Response so far »

  1. 1

    AleXander S. said,

    October 23, 2007 @ 9:28 am

    Very interesting..
    Looks like beautiful and structured code. I didn’t write anything in asp.net before, so i cannot estimate your work like asp.net programmer. It would be interesting to see demo programs of this code.

Comment RSS · TrackBack URI

Say your words