Sorting listbox by number

4 posts, 0 answers
  1. Dan
    Dan avatar
    17 posts
    Member since:
    Oct 2010

    Posted 01 Feb 2011 Link to this post

    We are transferring items between two RadListBox controls but we're finding that SortItems() does a text sort instead of a numeric sort so that all the 900s are listing before the 1000s (Sort="Descending").  I've tried setting the DataSortField, which says it needs to be a numeric field, but that doesn't help.

    The markup and codebehind are posted below.  Thanks for your help.
    Dan Norton

    <div style="float: left; width: 350px; vertical-align: middle; margin-left:30px; margin-top: 15px;">
        <asp:Label runat="server" id="Label1"
                    AssociatedControlId="radWorkOrderList"
                    Text="Work Orders:" />
        <telerik:RadListBox runat="server" ID="radWorkOrderList"
                            Width="350px" Height="200px"
                            SelectionMode="Multiple"
                            AllowTransfer="true"
                            TransferToID="radAssignedWorkOrderList"
                            AutoPostBackOnTransfer="true"
                            AllowReorder="false" 
                            OnTransferring="radWorkOrderList_Transferring"
                            EnableDragAndDrop="true"
                            Sort="Descending"
                            DataSortField="Number"
                            DataTextField="Number" 
                            DataValueField="Id">
            <ItemTemplate>
                <asp:HyperLink 
                    runat="server" 
                    Width="50"
                    Text='<%# DataBinder.Eval(Container.DataItem, "Number")%>' 
                    ID="lnkWorkOrderEdit" 
                    style="vertical-align:text-top;"
                    NavigateUrl='<%# GetWorkOrderNavigationURL(DataBinder.Eval(Container.DataItem, "Id")) %>'
                    ToolTip='<%# DataBinder.Eval(Container.DataItem, "Description") %>'
                />
                <asp:Label 
                    ID="lblWorkOrderDescription" 
                    Width="225" 
                    runat="server" 
                    Text='<%# GetShortDescription(DataBinder.Eval(Container.DataItem, "Description")) %>' 
                />
            </ItemTemplate>
        </telerik:RadListBox>
    </div>
    <div style="float: left; width: 375px; margin: 0px; margin-top: 15px;">
        <asp:Label runat="server" id="Label2"
                    AssociatedControlId="radAssignedWorkOrderList"
                    Text="Work Orders assigned to this Project:" />
        <telerik:RadListBox runat="server" ID="radAssignedWorkOrderList"
                            Width="375px" Height="200px"
                            SelectionMode="Multiple"
                            AllowReorder="false"
                            EnableDragAndDrop="true" 
                            Sort="Descending" 
                            DataSortField="WorkOrderNumber"
                            DataTextField="WorkOrderNumber"
                            DataValueField="WorkOrderId" >
            <ItemTemplate>
                <asp:HyperLink 
                    runat="server" 
                    Width="50"
                    Text='<%# DataBinder.Eval(Container.DataItem, "WorkOrderNumber")%>' 
                    ID="lnkAssignedWorkOrderEdit" 
                    style="vertical-align:text-top;"
                    NavigateUrl='<%# GetWorkOrderNavigationURL(DataBinder.Eval(Container.DataItem, "WorkOrderId")) %>'
                    ToolTip='<%# DataBinder.Eval(Container.DataItem, "WorkOrderDescription") %>'
                />
                <asp:Label 
                    ID="lblAssignedWorkOrderDescription" 
                    Width="225" runat="server" 
                    Text='<%# GetShortDescription(DataBinder.Eval(Container.DataItem, "WorkOrderDescription")) %>' 
                />
                <asp:Label 
                    ID="lblAssignedWorkOrderStatus" 
                    Width="75" runat="server" 
                    Text='<%# DataBinder.Eval(Container.DataItem, "WorkOrderStatus") %>' 
                />
            </ItemTemplate>
        </telerik:RadListBox>
    </div>

    protected void radWorkOrderList_Transferring(object sender, RadListBoxTransferringEventArgs e)
    {
            RadListBoxItem destinationItem = new RadListBoxItem();
            foreach (RadListBoxItem sourceItem in e.Items)
            {
                destinationItem = new RadListBoxItem();
                if (e.SourceListBox == radWorkOrderList)
                {
                    radAssignedWorkOrderList.Items.Add(destinationItem);
                    destinationItem.Value = sourceItem.Value;
                    destinationItem.Text = sourceItem.Text;
                    HyperLink lnkSourceWorkOrderNumber = sourceItem.FindControl("lnkWorkOrderEdit") as HyperLink;
                    HyperLink lnkDestinationWorkOrderNumber = radAssignedWorkOrderList.Items.Last().FindControl("lnkAssignedWorkOrderEdit") as HyperLink;
                    lnkDestinationWorkOrderNumber.NavigateUrl = lnkSourceWorkOrderNumber.NavigateUrl;
                    lnkDestinationWorkOrderNumber.Text = lnkSourceWorkOrderNumber.Text;
                    lnkDestinationWorkOrderNumber.ToolTip = lnkSourceWorkOrderNumber.ToolTip;
                    Label lblSourceWorkOrderDescription = sourceItem.FindControl("lblWorkOrderDescription") as Label;
                    Label lblDestinationWorkOrderDescription = radAssignedWorkOrderList.Items.Last().FindControl("lblAssignedWorkOrderDescription") as Label;
                    lblDestinationWorkOrderDescription.Text = lblSourceWorkOrderDescription.Text;
                    WorkOrder destinationWorkOrder = new WorkOrder(AppSession, int.Parse(destinationItem.Value));
                    Status destinationWorkOrderStatus = new Status(AppSession, destinationWorkOrder.StatusId);
                    Label lblDestinationWorkOrderStatus = radAssignedWorkOrderList.Items.Last().FindControl("lblAssignedWorkOrderStatus") as Label;
                    lblDestinationWorkOrderStatus.ID = "lblAssignedWorkOrderStatus";
                    lblDestinationWorkOrderStatus.Text = destinationWorkOrderStatus.Name;
                    radWorkOrderList.Items.Remove(sourceItem);
                }
                else
                {
                    radWorkOrderList.Items.Add(destinationItem);
                    radWorkOrderList.Items.Last().Value = sourceItem.Value;
                    radWorkOrderList.Items.Last().Text = sourceItem.Text;
                    HyperLink lnkSourceWorkOrderNumber = sourceItem.FindControl("lnkAssignedWorkOrderEdit") as HyperLink;
                    HyperLink lnkDestinationWorkOrderNumber = radWorkOrderList.Items.Last().FindControl("lnkWorkOrderEdit") as HyperLink;
                    lnkDestinationWorkOrderNumber.NavigateUrl = lnkSourceWorkOrderNumber.NavigateUrl;
                    lnkDestinationWorkOrderNumber.Text = lnkSourceWorkOrderNumber.Text;
                    lnkDestinationWorkOrderNumber.ToolTip = lnkSourceWorkOrderNumber.ToolTip;
                    Label lblSourceWorkOrderDescription = sourceItem.FindControl("lblAssignedWorkOrderDescription") as Label;
                    Label lblDestinationWorkOrderDescription = radWorkOrderList.Items.Last().FindControl("lblWorkOrderDescription") as Label;
                    lblDestinationWorkOrderDescription.Text = lblSourceWorkOrderDescription.Text;
                    radAssignedWorkOrderList.Items.Remove(sourceItem);
                }
            }
            if (e.DestinationListBox == radWorkOrderList)
            {
                radWorkOrderList.SortItems();
            }
            else
            {
                radAssignedWorkOrderList.SortItems();
            }
            e.Cancel = true;
    }

     

  2. Genady Sergeev
    Admin
    Genady Sergeev avatar
    1596 posts

    Posted 07 Feb 2011 Link to this post

    Hello Dan,

    RadListBox does not support automatic reordering should the reorder field is not a string. I suggest that you do such ordering on a DataSource level. For example, If you use SqlDataSource you can implement this in the SELECT Query. Here is short example from the AdventureWorks database:

    SELECT ContactID, Title, FirstName, EmailPromotion
    FROM Person.Contact
    WHERE (ContactID < 20)
    ORDER BY EmailPromotion

    This will filter the top 20 Contacts based on the EmailPromotion number. As for the DataSortField, it is required for automatic data source updates during reorder.

    Greetings,
    Genady Sergeev
    the Telerik team
    Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Becky
    Becky avatar
    3 posts
    Member since:
    May 2012

    Posted 28 May 2012 Link to this post

    Hi,

    I was just wondering whether this had been resolved? My display text is a numeric value and when it is initially bound to my stored procedure it is in the correct order (as my sp orders it).

    However, when I transfer an item and call the SortItems() method, it changes it to alphabetical sorting which means the ordering changes to 1, 1000, 1001, 2, 3 etc.

    Thanks

    Becky
  5. jumpstart
    jumpstart avatar
    479 posts
    Member since:
    Nov 2011

    Posted 29 May 2012 Link to this post

    Becky,

    As Genedy noted below, RadListBox does not support automatic reordering should the reorder field be something other than a string value. It's not a bug that needed to be resolved. It would be a feature request to enhance the product.

    In the meantime, you'd have to implement custom sorting logic in the same manner that is covered in the Implement custom sorting documentation page of the RadComboBox control.

    I hope this helps!
Back to Top