This is a migrated thread and some comments may be shown as answers.

Add ListBox At First Position

4 Answers 183 Views
ListBox
This is a migrated thread and some comments may be shown as answers.
Tracy
Top achievements
Rank 1
Tracy asked on 19 Sep 2012, 02:31 AM
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

4 Answers, 1 is accepted

Sort by
0
Accepted
Ivana
Telerik team
answered on 21 Sep 2012, 01:38 PM
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.
0
Tracy
Top achievements
Rank 1
answered on 06 Oct 2012, 01:05 AM
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
0
Accepted
Ivana
Telerik team
answered on 09 Oct 2012, 02:44 PM
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.
0
Tracy
Top achievements
Rank 1
answered on 09 Oct 2012, 03:50 PM
That works.

Thank You
Tags
ListBox
Asked by
Tracy
Top achievements
Rank 1
Answers by
Ivana
Telerik team
Tracy
Top achievements
Rank 1
Share this question
or