We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
close icon

Adding items

Hello,

How to add item by code (c#). I cannot do it, the ListBox doesn't add the items this way:

ListBox_1.Items.Add(new ListBoxItems() {Text = "Item 1"});

Thansk!

1 Reply

KK Kalpana Kandasamy Syncfusion Team March 21, 2016 05:46 PM UTC

Hi Jorge ,

Thank you for contacting Syncfusion support.

Query: Form the ListBox items from the code behind.

We have analyzed your query that you have added the items to the ListBox component in dynamically from the code behind. To add the multiple items to the ListBox component, initially need to declare the variable with List of ListBoxItems type. Then add the new data’s to this declared variable.

After initialization of new data’s to the items can be assigned to the ListBox component datasoure property and its corresponding field need to map.

We have achieved your requirement of add the new items to the ListBox components by refer the below code snippet.

[ASPX]

 

           <ej:ListBox ID="Listbox1" runat="server" Width="200px"></ej:ListBox>

 

[CS]Code Behind

                      

           protected void Page_Load(object sender, EventArgs e)

          {

            //initilize the List of ListBoxItems to the variable

            List<ListBoxItems> list = new List<ListBoxItems>();

            //then add the custom data's to the list of items.

            list.Add(new ListBoxItems() { Text = "Item 1"});

            list.Add(new ListBoxItems() { Text = "Item 2" });

            list.Add(new ListBoxItems() { Text = "Item 3" });

            list.Add(new ListBoxItems() { Text = "Item 4" });

            list.Add(new ListBoxItems() { Text = "Item 5" });

            list.Add(new ListBoxItems() { Text = "Item 6" });

            list.Add(new ListBoxItems() { Text = "Item 7" });

            list.Add(new ListBoxItems() { Text = "Item 8" });

            list.Add(new ListBoxItems() { Text = "Item 9" });

            list.Add(new ListBoxItems() { Text = "Item 10" });

            //map the corresponding field

            this.Listbox1.DataTextField = "Text";

            //assign the collectiones to the datasource property.

            this.Listbox1.DataSource = list;

    }

 

 

Please let us know if you need any further assistance.

Regards,

Kalpana K


Loader.
Live Chat Icon For mobile
Up arrow icon