Dynamically adding items to asp:ListBox

Hello again,

What I want to do is create a webpage containing a ListBox. This listbox will be dynamically populated once the page is loaded. I am programming in J# and I am writing a function that will populate this listbox. Is there a way to dynamically add items to the list

Right now I have the following html:

<asp:ListBox ID="listBoxUser" runat="server" OnLoad="getUsers">
<asp:ListItem Text="a" Value="a"></asp:ListItem>
<asp:ListItem Text="b" Value="b"></asp:ListItem>
<asp:ListItem Text="c" Value="c"></asp:ListItem>
<asp:ListItem Text="d" Value="d"></asp:ListItem>
<asp:ListItem Text="e" Value="e"></asp:ListItem>
<asp:ListItem Text="f" Value="f"></asp:ListItem>
<asp:ListItem Text="g" Value="g"></asp:ListItem>
</asp:ListBox>

My function is:

public void getUsers(Object sender, EventArgs e)
{

}// getUsers

How do I add more items to the listBoxUser For example, I want to add users x,y,z using the getUsers function.

Any Ideas

Thank you


Answer this question

Dynamically adding items to asp:ListBox

  • kodiya

    I think I found the answer. I am using an ArrayList to add items into the ListBox by usind DataBind(). Let me know if there is a more direct way to do this instead of using an ArrayList.

  • pneumahagion777

    Hi,

    If your scenario is to add one item at a time then you can directly use

    ListBox.get_Items().Add(); method. Otherwise what you are doing is correct.

    Thanks.



  • Dynamically adding items to asp:ListBox