Web Forms - Working with ASP.NET server controls

Adding New Items to List Boxes at Runtime

When you use any Web server control based on the ListControl class, you can use the Items collection's Add method to add items to it, which works for Web server list boxes as well. The Items collection is a collection of ListItem objects, and you can either pass a ListItem object to the Add method or the text you want to give to the new item. To see how this works, take a look at the AddItems example in the code for this book. You can see this example at work in Figure 14.6—when you click the Add New Item button, a new item is added to the list box (Item 3 is new in this figure).


Figure 14.6
Adding new items to a list box at runtime.

To add a new item to the list box, you can use the Add method like this, which adds a new item and set the correct caption using the Count property of the list box's Items collection:

Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
    ListBox1.Items.Add("Item " & ListBox1.Items.Count)

End Sub

When the list box appears the first time, the list box has three items in it:

<select name="ListBox1" size="4" onchange="__
doPostBack('ListBox1','')" language="javascript"
id="ListBox1" style="height:123px;width:142px;Z-INDEX:
102; LEFT: 153px; POSITION: absolute; TOP: 70px">
  <option value="Item 0">Item 0</option>
  <option value="Item 1">Item 1</option>
  <option value="Item 2">Item 2</option>

</select>

After you click the Add New Item button, the list box will come back from the server with four items in it:

<select name="ListBox1" size="4" onchange="__
doPostBack('ListBox1','')" language="javascript"
id="ListBox1" style="height:123px;width:142px;Z-INDEX:
102; LEFT: 153px; POSITION: absolute; TOP: 70px">
  <option value="Item 0">Item 0</option>
  <option value="Item 1">Item 1</option>
  <option value="Item 2">Item 2</option>
  <option value="Item 3">Item 3</option>

</select>

That's all there is to it; the next time you click the Add New Item button, another item will be added, and so on.

This example also adds code to the list box's SelectedIndexChanged event handler to display the selection the user makes in a text box, like this:

Private Sub ListBox1_SelectedIndexChanged(ByVal _
sender As System.Object, ByVal e As System.EventArgs) _
Handles ListBox1.SelectedIndexChanged
  TextBox1.Text = "You selected " & ListBox1.SelectedItem.Text

End Sub

As you can see, it's fairly easy to work with standard Web server list box controls. Another type of HTML control functions very similar to list controls—drop-down list controls—and they're coming up next.

You might also like...

Comments

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Walking on water and developing software from a specification are easy if both are frozen.” - Edward V Berard