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.
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

Adding items

Thread ID:

Created:

Updated:

Platform:

Replies:

123453 Mar 18,2016 12:02 PM UTC Mar 21,2016 05:46 PM UTC ASP.NET Web Forms 1
loading
Tags: ListBox
Jorge Pampin
Asked On March 18, 2016 12:02 PM UTC

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!

Kalpana Kandasamy [Syncfusion]
Replied On 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


CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

;