views:

59

answers:

1

ok i have a project which has many gridview in its pages... now i am sorting the fridveiw using the sorting function like this:

    protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
    {
        DataTable dt = Session["TaskTable2"] as DataTable;

        if (dt != null)
        {

            //Sort the data.
            dt.DefaultView.Sort = e.SortExpression + " " + GetSortDirection(e.SortExpression);
            GridView1.DataSource = Session["TaskTable2"];
            GridView1.DataBind();
        }

    }

    private string GetSortDirection(string column)
    {
        // By default, set the sort direction to ascending.
        string sortDirection2 = "ASC";

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

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

        // Save new values in ViewState.
        ViewState["SortDirection2"] = sortDirection2;
        ViewState["SortExpression2"] = column;

        return sortDirection2;
    }

but this code is being repeated in many pages so i tried to put this function in a C# class and try to call it but i get errors....

for starters i get the viewstate error saying :|

"viewstate does not exist in the current context"

so how do i go about doing this ....??

thanks

so this is what is there in my class:

public string GetSortDirection(string column)
    {
        // By default, set the sort direction to ascending.
        string sortDirection2 = "ASC";

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

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

        // Save new values in ViewState.
        ViewState["SortDirection2"] = sortDirection2;
        ViewState["SortExpression2"] = column;

        return sortDirection2;
    }

and i am calling it from my code like this:

 protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
    {
        DataTable dt = Session["TaskTable2"] as DataTable;

        if (dt != null)
        {

            //Sort the data.
            dt.DefaultView.Sort = e.SortExpression + " " + new impersonateClass().GetSortDirection(e.SortExpression);
            GridView1.DataSource = Session["TaskTable2"];
            GridView1.DataBind();
        }

    }

and i get view state error...

is here a way to put this entire thing in the class... because it is getting repeated everywhere...

+1  A: 

You will need to pass the ViewState in, as the ViewState object is a member of the Page class. Once you moved the code into a separate class, it no longer had access to the ViewState object.

public string GetSortDirection(string column, StateBag viewState) {
    // Your code here.
}
Jason Berkan
yes this is awesome man... ok so u understand what i want... is there a way i can put protected void GridView1_Sorting(object sender, GridViewSortEventArgs e) also in that class and call the entire thing from my code behind>>>???
Sure. There's nothing stopping you from passing in the ViewState, the GridView and the GridViewSortEventArgs into your new common sort class.
Jason Berkan
i tried but i am not getting it as to how to call GridView1_Sorting(object sender, GridViewSortEventArgs e).. please if u could help..
I wouldn't move the actual Sorting method into your common class, as you need that method hooked up to the event on the GridView. However, you can make the contents of that method a single call to the common Sorting class.
Jason Berkan