locked
Sorting a ListView with a Data Pager RRS feed

  • Question

  • User682008122 posted

    I have a listview that I bind using  custom code in the code behind:

    DataSet ds = jdh.GetDataSetFromDB(SQL);
    lv_positions_i_posted.DataSource = ds;
    lv_positions_i_posted.DataBind();

    The ListView has a DataPager

    <asp:DataPager ID="DataPager1" runat="server" PagedControlID="lv_positions_i_posted" PageSize="20">
    	<Fields>
    		<asp:NextPreviousPagerField ButtonType="Button" ShowFirstPageButton="True" ShowNextPageButton="False" ShowPreviousPageButton="False" />
                    <asp:NumericPagerField ButtonCount="10" />
                    <asp:NextPreviousPagerField ButtonType="Button" ShowLastPageButton="True" ShowNextPageButton="False" ShowPreviousPageButton="False" />
           </Fields>
    </asp:DataPager>

    I created a custom sort routine that successfully sorts the list view.

    <asp:UpdatePanel ID="UpdatePanel3" runat="server" UpdateMode="conditional">
    	<ContentTemplate>
            	<asp:ImageButton ID="SortPostingButton" runat="server" ImageUrl="~/images/Up2.png" Height="20px" Width="20px" ToolTip="Sort" OnClick="PostingSort1_Click" />        	</ContentTemplate>
      	<Triggers>
            	<asp:AsyncPostBackTrigger ControlID="SortPostingButton" />
       	</Triggers>
    </asp:UpdatePanel>

    The above snippet calls a routine that builds a SQL Statement which is applied to the fist code snippet above:

    DataSet ds = jdh.GetDataSetFromDB(SQL);
    lv_positions_i_posted.DataSource = ds;
    lv_positions_i_posted.DataBind();

    This ALL works ok. The issue is that if I change the page using the datapager, the sort is not maintained (i.e. page 2 and up are not sorted)

    Here is the code for the listview:

     <asp:ListView ID="lv_positions_i_posted" runat="server" DataKeyNames="postingid" OnPagePropertiesChanging="lv_positions_i_posted_paging" OnItemDataBound="lv_positions_i_posted_ItemDataBound">
    <LayoutTemplate>
        <table id="Table2" runat="server">
            <tr id="Tr1" runat="server">
                <td id="Td1" runat="server">
                    <table id="itemPlaceholderContainer" runat="server" border="1"
                        style="border-collapse: collapse; border-color: #999999; border-style: none; border-width: 1px; font-family: Verdana, Arial, Helvetica, sans-serif;">
                        <tr>
                            <th colspan="2" style="background-color: darkseagreen;">Posting</th>
                            <th colspan="1" style="background-color: sandybrown;">Applications</th>
                            <th colspan="4">Positions I Posted</th>
                        </tr>
                        <tr id="Tr2" runat="server" style="color: #000000;">
                            <th id="Th1" runat="server" style="background-color: darkseagreen;">View</th>
                            <th id="Th2" runat="server" style="background-color: darkseagreen;">Edit</th>
                            <th id="Th3" runat="server" style="background-color: sandybrown;">View</th>
                            <th id="Th4" runat="server">ID</th>
                            <th id="Th6" runat="server">Title</th>
                            <th id="Th8" runat="server">Posting Date
    
                                 <asp:UpdatePanel ID="UpdatePanel3" runat="server" UpdateMode="conditional">
                                     <ContentTemplate>
     <asp:ImageButton ID="SortPostingButton" runat="server" ImageUrl="~/images/Up2.png" Height="20px" Width="20px" ToolTip="Sort" OnClick="PostingSort1_Click" /> 
                                     </ContentTemplate>
                                     <Triggers>
     <asp:AsyncPostBackTrigger ControlID="SortPostingButton" />
                                     </Triggers>
                                 </asp:UpdatePanel>
    
                             </th>
                            <th id="Th9" runat="server">Status
                                <asp:ImageButton ID="SortStatusButton" runat="server" ImageUrl="~/images/Up2.png" Height="20px" Width="20px" ToolTip="Sort" OnClick="StatusSort1_Click" />
                            </th>
                        </tr>
                        <tr id="itemPlaceholder" runat="server">
                        </tr>
                    </table>
                </td>
            </tr>
            <tr id="Tr3" runat="server">
                <td id="Td2" runat="server"
                    style="text-align: center; background-color: #CCCCCC; font-family: Verdana, Arial, Helvetica, sans-serif; color: #000000;">
                    <asp:DataPager ID="DataPager1" runat="server" PagedControlID="lv_positions_i_posted" PageSize="20">
                        <Fields>
                            <asp:NextPreviousPagerField ButtonType="Button" ShowFirstPageButton="True"
                                ShowNextPageButton="False" ShowPreviousPageButton="False" />
                            <asp:NumericPagerField ButtonCount="10" />
                            <asp:NextPreviousPagerField ButtonType="Button" ShowLastPageButton="True"
                                ShowNextPageButton="False" ShowPreviousPageButton="False" />
                        </Fields>
                    </asp:DataPager>
                    &nbsp;
                </td>
            </tr>
        </table>
    </LayoutTemplate>
    <ItemTemplate>
        <tr style="background-color: #DCDCDC; color: #000000; font-weight: normal;">
            <td style="text-align: center">
                <asp:ImageButton ID="ViewPostingButton" runat="server" ToolTip="View Posting" ImageUrl="~/images/green-eye-icon-new-small.png" Width="30px" Height="18.75px" OnCommand="ViewPostingButton_Click" CommandArgument='<%#Eval("PostingID")%>' />
    
            </td>
            <td style="text-align: center">
                <asp:ImageButton ID="EditPostingButton" runat="server" ToolTip="Edit Posting" ImageUrl="~/images/pencil_small.png" Width="18.75px" Height="18.75px" OnCommand="EditPostingButton_Click" CommandArgument='<%#Eval("PostingID")%>' />
    
            </td>
    
            <td style="text-align: center">
                <asp:ImageButton ID="ViewPostingApplicantButton" runat="server" ToolTip="View Applicants" ImageUrl="~/images/green-eye-icon-new-small.png" Width="30px" Height="18.75px" ImageAlign="Middle" OnCommand="ViewPostingApplicantButton_Click" CommandArgument='<%#Eval("PostingID")%>' />
    
            </td>
            <td style="text-align: center">
                <asp:Label ID="PostingID" runat="server" Text='<%# Eval("PostingID") %>' Visible="true" />
            </td>
            <td>
                <asp:Label ID="PostingTitle" runat="server" Text='<%# Eval("PostingTitle") %>' Visible="true" ToolTip='<%# Eval("PostingTitleFull") %>' />
            </td>
            <td style="text-align: center">
                <asp:Label ID="PostingDate" runat="server" Text='<%# Eval("PostingDate","{0:MM/dd/yyyy}") %>' Visible="true" />
            </td>
    
    
            <td style="text-align: center">
                <asp:Label ID="PostingStatusID" runat="server" Text='<%# Eval("PostingStatusID") %>' Visible="true" />
            </td>
        </tr>
    </ItemTemplate>
    <AlternatingItemTemplate>
        <tr style="background-color: #FFF8DC; color: #000000; font-weight: normal;">
            <td style="text-align: center">
                <asp:ImageButton ID="ViewPostingButton" runat="server" ToolTip="View Posting" ImageUrl="~/images/green-eye-icon-new-small.png" Width="30px" Height="18.75px" OnCommand="ViewPostingButton_Click" CommandArgument='<%#Eval("PostingID")%>' />
    
            </td>
            <td style="text-align: center">
                <asp:ImageButton ID="EditPostingButton" runat="server" ToolTip="Edit Posting" ImageUrl="~/images/pencil_small.png" Width="18.75px" Height="18.75px" OnCommand="EditPostingButton_Click" CommandArgument='<%#Eval("PostingID")%>' />
    
            </td>
    
            <td style="text-align: center">
                <asp:ImageButton ID="ViewPostingApplicantButton" ToolTip="View Applicants" runat="server" ImageUrl="~/images/green-eye-icon-new-small.png" Width="30px" Height="18.75px" ImageAlign="Middle" OnCommand="ViewPostingApplicantButton_Click" CommandArgument='<%#Eval("PostingID")%>' />
    
            </td>
            <td style="text-align: center">
                <asp:Label ID="PostingID" runat="server" Text='<%# Eval("PostingID") %>' Visible="true" />
            </td>
            <td>
                <asp:Label ID="PostingTitle" runat="server" Text='<%# Eval("PostingTitle") %>' Visible="true" ToolTip='<%# Eval("PostingTitleFull") %>' />
            </td>
            <td style="text-align: center">
                <asp:Label ID="PostingDate" runat="server" Text='<%# Eval("PostingDate","{0:MM/dd/yyyy}") %>' Visible="true" />
            </td>
    
    
            <td style="text-align: center">
                <asp:Label ID="PostingStatusID" runat="server" Text='<%# Eval("PostingStatusID") %>' Visible="true" />
            </td>
        </tr>
    </AlternatingItemTemplate>
    <EmptyDataTemplate>
        <table id="Table1" runat="server"
            style="background-color: #FFFFFF; border-collapse: collapse; border-color: #999999; border-style: none; border-width: 1px;">
            <tr>
                <td>No data was returned.</td>
            </tr>
        </table>
    </EmptyDataTemplate>
     </asp:ListView>

    Here is how I Bind the ListView

    protected void BindListview(string selectedItem, string orderby)
        {
            string SQL = "";
            string direction = "";
                SQL = "Select  Statment Here";
                if (orderby != "")
                {
                    if (selectedItem == TheListView.Text)
                    {
                        if (orderby == LastSortField.Text)
                        {
                            if (LastSortDirection.Text == "asc")
                            {
                                direction = "desc";
                            }
                            else
                            {
                                direction = "asc";
                            }
                            SQL += " Order by " + orderby + " " + direction;
                            LastSortDirection.Text = direction;
                            LastSortField.Text = orderby;
                        }
                        else
                        {
                            SQL += " Order by " + orderby;
                            LastSortDirection.Text = "asc";
                            LastSortField.Text = orderby;
                        }
                    }
                    else
                    {
                        SQL += " Order by '" + orderby ;
                        LastSortDirection.Text = "asc";
                        LastSortField.Text = orderby;
                    }
                }
                else
                {
                        SQL += " Order by PostingID asc";
                        LastSortDirection.Text = "asc";
                        LastSortField.Text = "PostingID";
                }
                DataSet ds = jdh.GetDataSetFromDB(SQL);
                lv_positions_i_posted.DataSource = ds;
                lv_positions_i_posted.DataBind();
                lv_positions_i_posted.Visible = true;
                ds.Dispose();
            }

    Tuesday, January 15, 2019 7:20 PM

Answers

  • User682008122 posted

    Jenifer - I actually figured it out about 20 min ago.

    I forgot that I captured the datapager page change and was not passing in the "orderby" variable. Once I fixed that it works correctly.

    protected void lv_positions_i_manage_paging(object sender, PagePropertiesChangingEventArgs e)
    {
        (lv_positions_i_manage.FindControl("DataPager1") as DataPager).SetPageProperties(e.StartRowIndex, e.MaximumRows, false);
        if (LastSortDirection.Text == "asc")
        {
            LastSortDirection.Text = "desc";
        }
        else
        {
            LastSortDirection.Text = "asc";
        }
    
            BindListview("2", LastSortField.Text);
    }
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 16, 2019 2:37 PM

All replies

  • User839733648 posted

    Hi Whalensdad,

    According to your description and code, I'm sorry that I could not reproduce your issue successfully.

    protected void BindListview(string selectedItem, string orderby)

    Some various like TheListView,LastSortField in the above event could not work well, and I could not find these keywords elsewhere.

    Also, I want to ask that where do you call the BindListview event. Maybe this has affected the issue.

    Best Regards,

    Jenifer

    Wednesday, January 16, 2019 2:34 PM
  • User682008122 posted

    Jenifer - I actually figured it out about 20 min ago.

    I forgot that I captured the datapager page change and was not passing in the "orderby" variable. Once I fixed that it works correctly.

    protected void lv_positions_i_manage_paging(object sender, PagePropertiesChangingEventArgs e)
    {
        (lv_positions_i_manage.FindControl("DataPager1") as DataPager).SetPageProperties(e.StartRowIndex, e.MaximumRows, false);
        if (LastSortDirection.Text == "asc")
        {
            LastSortDirection.Text = "desc";
        }
        else
        {
            LastSortDirection.Text = "asc";
        }
    
            BindListview("2", LastSortField.Text);
    }
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 16, 2019 2:37 PM
  • User839733648 posted

    Hi Whalensdad,

    I suggest that you coud mark your  solution as answer if it solved your problem.

    And this will be helpful for someone meets with the same problem. 

    Best Regards,

    Jenifer

    Thursday, January 17, 2019 1:48 AM