Dropdown Selection Lost on Listbox Sort

6 posts, 1 answers
  1. Scott
    Scott avatar
    38 posts
    Member since:
    Sep 2012

    Posted 23 Aug 2011 Link to this post

    I'm using 2 listboxes and transferring items between the two.  I have an item template on the destination listbox with a label and a databound dropdownlist.  I move items to the destination LB, then choose a selection for each dropdown item.  If I then transfer another item from source LB to destination LB, the dropdown selections are all lost (selected index becomes 0 again).
    The issue lies in the sort itself, because if I comment out the sort method, the dropdowns retain their selection after a transfer.
    Here is the listbox item transferred event code I'm using.  If I comment out the first 4 lines of the method, the selections are retained.  The foreach statement is apparently required in order to make the items in the itemtemplate display properly.

    protected void lbAdv_Rosters_Transferred(object sender, RadListBoxTransferredEventArgs e)
        {
            lbAdv_ChosenRosters.Sort = RadListBoxSort.Ascending;
            lbAdv_ChosenRosters.SortItems();
            lbAdv_Rosters.Sort = RadListBoxSort.Ascending;
            lbAdv_Rosters.SortItems();

            foreach (RadListBoxItem item in e.Items)
            {
                item.DataBind();
            }
        }

    I'm using version 2010.3.1317.35.  Thanks in advance!
  2. Peter Filipov
    Admin
    Peter Filipov avatar
    1028 posts

    Posted 26 Aug 2011 Link to this post

    Hello Scott,

    I wasn't able to create a sample project from the description below. Could you please provide a working sample project that reproduces your issue to investigate it locally.

    All the best,
    Peter Filipov
    the Telerik team

    Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's DevProConnections Awards. We are competing in mind-blowing 20 categories and every vote counts! VOTE for Telerik NOW >>

  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Scott
    Scott avatar
    38 posts
    Member since:
    Sep 2012

    Posted 29 Aug 2011 Link to this post

    I was able to reproduce it in a small sample project.  I'll just post the code here since there's not much to it.  If you try this code, the dropdown selections don't persist after postback but if you comment out the sorting code and try it again, the dropdown selections are retained after postback.

    .ASPX:
    <body>
        <form id="form1" runat="server">
        <telerik:RadScriptManager ID="RadScriptManager1" runat="server">
        </telerik:RadScriptManager>
        <div>
            Rosters:<br />
            <telerik:RadListBox ID="lbAdv_Rosters" runat="server" AllowTransfer="True" Skin="WebBlue"
                SelectionMode="Multiple" TransferToID="lbAdv_ChosenRosters" EnableDragAndDrop="False"
                ButtonSettings-Position="Bottom" Height="140px" Width="335px" EnableTheming="False"
                CssClass="block" AutoPostBackOnTransfer="True" DataTextField="Name" 
                DataValueField="ID" ontransferred="lbAdv_Rosters_Transferred">
                <ButtonSettings TransferButtons="All" HorizontalAlign="Center" VerticalAlign="Middle"
                    Position="Bottom" ShowTransferAll="False" />
            </telerik:RadListBox>
            <br />
            <telerik:RadListBox ID="lbAdv_ChosenRosters" runat="server" Skin="WebBlue" Height="120px"
                Width="335px" EnableTheming="False" EnableDragAndDrop="False" AutoPostBackOnTransfer="True"
                SelectionMode="Multiple" EmptyMessage="No rosters selected" OnItemCreated="lbAdv_ChosenRosters_ItemCreated">
                <ButtonSettings TransferButtons="All" />
                <ItemTemplate>
                    <asp:Label ID="lblAdv_ChosenRost" runat="server" Text='<%# DataBinder.Eval(Container, "Text") %>'
                        Width="174px" Style="white-space: nowrap;"></asp:Label>
                    <asp:DropDownList ID="ddlAdv_IndFlag" runat="server" Width="129px" AppendDataBoundItems="True">
                        <asp:ListItem Value="-1">--Select Flag--</asp:ListItem>
                    </asp:DropDownList>
                </ItemTemplate>
            </telerik:RadListBox>
        </div>
        </form>
    </body>

    .CS
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                DataTable dt = new DataTable();
                dt.Columns.Add("id");
                dt.Columns.Add("name");
                for (int i = 0; i < 10; i++)
                {
                    DataRow row = dt.NewRow();
                    row[0] = i.ToString();
                    row[1] = "Some Data " + i.ToString();
                    dt.Rows.Add(row);
                }
                lbAdv_Rosters.DataSource = dt;
                lbAdv_Rosters.DataBind();
            }
        }

        protected void lbAdv_Rosters_Transferred(object sender, Telerik.Web.UI.RadListBoxTransferredEventArgs e)
        {
            lbAdv_ChosenRosters.Sort = RadListBoxSort.Ascending;
            lbAdv_ChosenRosters.SortItems();
            lbAdv_Rosters.Sort = RadListBoxSort.Ascending;
            lbAdv_Rosters.SortItems();
            foreach (RadListBoxItem item in e.Items)
            {
                item.DataBind();
            }
        }

        protected void lbAdv_ChosenRosters_ItemCreated(object sender, RadListBoxItemEventArgs e)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("id");
            dt.Columns.Add("name");
            for (int i = 0; i < 10; i++)
            {
                DataRow row = dt.NewRow();
                row[0] = i.ToString();
                row[1] = "Some Dropdown " + i.ToString();
                dt.Rows.Add(row);
            }
            DropDownList indFlagDDL = (DropDownList)e.Item.FindControl("ddlAdv_IndFlag");
            indFlagDDL.Items.Clear();
            if (dt.Rows.Count > 0)
            {
                indFlagDDL.DataSource = dt;
                indFlagDDL.DataValueField = dt.Columns[0].ColumnName;
                indFlagDDL.DataTextField = dt.Columns[1].ColumnName;
                indFlagDDL.DataBind();
            }
            indFlagDDL.Items.Insert(0, new ListItem("Please choose", "-1"));
            indFlagDDL.SelectedIndex = 0;
        }
    }


  5. Answer
    Peter Filipov
    Admin
    Peter Filipov avatar
    1028 posts

    Posted 01 Sep 2011 Link to this post

    Hello Scott,

    Thank you for the provided sample code. I created a sample project based on it. Also I was able to reproduce your issue. It happens, because the sort method is called too early in the page life cycle.

    Please find attached the fixed files.

    All the best,
    Peter Filipov
    the Telerik team

    Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's DevProConnections Awards. We are competing in mind-blowing 20 categories and every vote counts! VOTE for Telerik NOW >>

  6. Scott
    Scott avatar
    38 posts
    Member since:
    Sep 2012

    Posted 26 Sep 2011 Link to this post

    This corrected the issue when I transfer items to the source listbox, however, when I do any other postback the selections are still lost.  I have 3 source/destination listboxes on this page, and all 3 post back on transfer.  When I transfer one of the other listboxes, the dropdown selections are still lost on this one.
  7. Scott
    Scott avatar
    38 posts
    Member since:
    Sep 2012

    Posted 26 Sep 2011 Link to this post

    Nevermind, I figured it out.  It's kind of sloppy, but I checked to see if the transfer button for this listbox was clicked in the page load.  If it was, then I set a flag to true.  I then added a check for this flag before going into the sorting loop.  So basically it only sorts the list if the associated transfer button was clicked.  Thanks again.

    // In my page_load:
    if (Request.Form["__EVENTTARGET"] != null)
            {
                if (Request.Form["__EVENTTARGET"].ToString().Contains("lbAdv_Rosters"))   // checking that the lbAdv_Rosters button was pressed
                {
                    rosterTransfer = true;
                }
            }

    // and the sorting loop:
    protected void Page_PreRender(object sender, EventArgs e)
        {
            if (rosterTransfer)
            {
                lbAdv_ChosenRosters.Sort = RadListBoxSort.Ascending;
                lbAdv_ChosenRosters.SortItems();
                rosterTransfer = false;
            }
        }
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017