Add ListBox Items in C#

Posted by Akiii under C# category on | Points: 40 | Views : 2632
First lets declare the asp list box control :-

<asp:ListBox ID="lstdemo" runat="server"></asp:ListBox>

We declare an add button to add items in the listbox :-

<asp:Button ID="btnAdd" runat="server" OnClick="btnAdd_Click" Text="Add " />

Now on the click of a button, we add the value to the listbox :-

protected void btnAdd_Click(object sender, EventArgs e)
int NewIndex = lstdemo.Items.Count + 1;

ListItem lstItems = new ListItem("Akiii-" + NewIndex.ToString(), NewIndex.ToString());

// Change the DataSource.
lstdemo.SelectedIndex = NewIndex-1;


To add an item in ListBox, first we incremented the count then we created a ListItem by passing the value and text and then add this item to our ListBox. After adding the item to the ListBox, we highlighted the just populated value.

Thanks and Regards

Comments or Responses

Login to post response