Add ListBox At First Position

5 posts, 2 answers
  1. Tracy
    Tracy avatar
    118 posts
    Member since:
    Nov 2010

    Posted 18 Sep 2012 Link to this post

    Hi,

    I am binding a listbox using a SQLDatasource and then adding an item ("All")  to the list on the item databound event.  The problem is that it is adding it in the second position and I need it to be the first item in the list.  Is there a way to add an item at a specific position?

    Thanks for your help.

    Private Sub rlbCompanyName_ItemDataBound(ByVal sender As Object, ByVal e As Telerik.Web.UI.RadListBoxItemEventArgs) Handles rlbCompanyName.ItemDataBound
     
            Dim drv As DataRowView = CType(e.Item.DataItem, DataRowView)
            e.Item.Attributes.Add("CompanyNumber", drv("CompanyNumber").ToString())
            If Me.rlbCompanyName.FindItemByValue("All") Is Nothing Then
                Dim item As New RadListBoxItem("All", "All")
                item.Attributes.Add("CompanyNumber", "00")
                Me.rlbCompanyName.Items.Add(item)
            End If
            If Not Me.rlbCompanyName.FindItemByText("Unknown") Is Nothing Then
                Dim itemToRemove As RadListBoxItem = rlbCompanyName.FindItemByText("Unknown")
                Me.rlbCompanyName.Items.Remove(itemToRemove)
            End If
     
        End Sub
  2. Answer
    Ivana
    Admin
    Ivana avatar
    657 posts

    Posted 21 Sep 2012 Link to this post

    Hello Tracy,

    You can try the AppendDataBoundItems property of RadListBox.
    Here is an example:
    <telerik:RadListBox runat="server" ID="RadListBox1" AppendDataBoundItems="true" DataTextField="Name"
        DataValueField="ID" DataSourceID="SqlDataSource1">
        <Items>
            <telerik:RadListBoxItem Text="All" />
        </Items>
    </telerik:RadListBox>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:TelerikConnectionString %>"
        SelectCommand="SELECT [ID], [NAME] FROM [Cities]"></asp:SqlDataSource>

    I hope it helps.

    Kind regards,
    Ivana
    the Telerik team
    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Tracy
    Tracy avatar
    118 posts
    Member since:
    Nov 2010

    Posted 05 Oct 2012 Link to this post

    Hi Ivana,

    Although you solution works, I would like to know how to add this item in the first position from code behind.  This value is only added if a certain property is set.

    Thank You
    Tracy
  5. Answer
    Ivana
    Admin
    Ivana avatar
    657 posts

    Posted 09 Oct 2012 Link to this post

    Hello Tracy,

    The following example shows how to insert an item in the Items collection on the server.
    <telerik:RadListBox runat="server" ID="RadListBox1" DataTextField="Name"
        DataValueField="ID" DataSourceID="SqlDataSource1" OnDataBound="RadListBox1_DataBound">
    </telerik:RadListBox>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:TelerikConnectionString %>"
        SelectCommand="SELECT [ID], [NAME] FROM [Cities]"></asp:SqlDataSource>
    protected void RadListBox1_DataBound(object sender, EventArgs e)
    {
        RadListBox1.Items.Insert(0, new Telerik.Web.UI.RadListBoxItem("1"));
    }

    I hope this is helpful.

    All the best,
    Ivana
    the Telerik team
    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
  6. Tracy
    Tracy avatar
    118 posts
    Member since:
    Nov 2010

    Posted 09 Oct 2012 Link to this post

    That works.

    Thank You
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017