Question

Hello Friends my Pagination works fine, but when sort any column, it gets sorted on first page but on next pages sorting gets lost.

Please any one can help me to figure this out.

My Markup Code for .aspx

                <asp:TemplateField HeaderText="Actions" ItemStyle-Width="20%">
                    <ItemTemplate>
                        <a href='#' onclick="javascript:view_data(<%#Eval("Substance_ID")%>)"><span class="glyphicon glyphicon-eye-open"></span>View</a>
                        &nbsp;
                     <a href='edit.aspx?id=<%#Eval("Substance_ID")%>'><span class="glyphicon glyphicon-pencil"></span>Edit</a>
                        &nbsp;
                     <a href='delete.aspx?id=<%#Eval("Substance_ID")%>'><span class="glyphicon glyphicon-trash"></span>Delete</a>
                    </ItemTemplate>
                </asp:TemplateField>

                <asp:BoundField DataField="Substance_ID" HeaderText="Substance ID" SortExpression="Substance_ID" />

                 <asp:TemplateField HeaderText="CAS Number" ItemStyle-Width="10%" SortExpression="CAS_Number">
                    <ItemTemplate>
                        <%# Eval("CAS_Number").ToString().Trim()%>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="EC Number" ItemStyle-Width="10%" SortExpression="EC_Number">
                    <ItemTemplate>
                        <%# Eval("EC_Number").ToString().Trim()%>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Substance Name" SortExpression="Substance_Name">
                    <ItemTemplate>
                        <%#Eval("Substance_Name").ToString().Trim()%>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Primary SG ID" ItemStyle-Width="10%" SortExpression="Primary_SG_ID">
                    <ItemTemplate>
                        <%#Eval("Primary_SG_ID")%>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Status" ItemStyle-Width="5%" SortExpression="Status">
                    <ItemTemplate>
                        <%#Eval("Status")%>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
            <PagerStyle CssClass="my_pagination" />
            <PagerSettings Mode="NumericFirstLast" FirstPageText="First" LastPageText="Last" PageButtonCount="10" />
            <EmptyDataTemplate>No Data</EmptyDataTemplate>

        </asp:GridView>
        <asp:Label runat="server" ID="lblCount"></asp:Label>

And My Code Behind:

    protected void Page_Load(object sender, EventArgs e)
    {
         if(!IsPostBack)
         {
             BindGridViewData();
         }
    }
    private void BindGridViewData()
    {
        List<Substance_Display> Substance_List = new List<Substance_Display>();
        Substance_List = Substance_MasterAccessLayer.GetAllSubstances("Substance_ID");
        Substance_Master.DataSource = Substance_List;
        Substance_Master.DataBind();
        lblCount.Text = "Total " + Substance_List.Count().ToString() + " Records found.";
    }
    protected void Substance_Master_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        Substance_Master.PageIndex = e.NewPageIndex;
        Substance_Master.DataBind();
    }
    private void SortGridView(GridView Substance_Master, GridViewSortEventArgs e, out SortDirection sortDirection, out string sortField)
    {
        sortField = e.SortExpression;
        sortDirection = e.SortDirection;
        if (Substance_Master.Attributes["CurrentSortField"] != null & Substance_Master.Attributes["CurrentSortDirection"] != null)
        {
            if (sortField == Substance_Master.Attributes["CurrentSortField"])
            {
                if (Substance_Master.Attributes["CurrentSortDirection"] == "ASC")
                {
                    sortDirection = SortDirection.Descending;
                }
                else
                {
                    sortDirection = SortDirection.Ascending;
                }

            }
            Substance_Master.Attributes["CurrentSortField"] = sortField;
            Substance_Master.Attributes["CurrentSortDirection"] = (sortDirection == SortDirection.Ascending ? "ASC" : "DESC");
        }
    }
    protected void Substance_Master_Sorting(object sender, GridViewSortEventArgs e)
    {
        SortDirection sortDirection = SortDirection.Ascending;
        string sortField = string.Empty;
        SortGridView(Substance_Master, e, out sortDirection, out sortField);
        string strSortDirection = sortDirection == SortDirection.Ascending ? "ASC" : "DESC";
        Substance_Master.DataSource = Substance_MasterAccessLayer.GetAllSubstances(e.SortExpression + " " + strSortDirection);
        Substance_Master.DataBind();
    }
Was it helpful?

Solution

EDIT: I would suggest the following to fix the issue:

Change your Page_Load method to this:

 protected void Page_Load(object sender, EventArgs e)
 {
     if(!IsPostBack)
     {
          Substance_Master.Attributes["CurrentSortField"] = "Substance_ID";
          Substance_Master.Attributes["CurrentSortDirection"] = "ASC";
          BindGridViewData();
     }
 }

And change the Substance_Master_PageIndexChanging method to this:

protected void Substance_Master_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    Substance_Master.PageIndex = e.NewPageIndex;
    var sortField = Substance_Master.Attributes["CurrentSortField"];
    var sortDirection = Substance_Master.Attributes["CurrentSortDirection"];

    if (sortField != null && sortDirection != null)
    {
        Substance_Master.DataSource = Substance_MasterAccessLayer.GetAllSubstances(sortField + "" + sortDirection);
    }

    Substance_Master.DataBind();
}

Explanation: You need to set the datasource of GridView before databind in PageIndexChanging event method. And you may need to sort the datasource depending on the current sort conditions.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top