DropDownLists, ListBoxs, and CheckBoxLists

P

Pranav Shah

I have a few DropDownLists, ListBoxs, and CheckBoxLists and assume for
the sake of argument that I want to put the same data in all of these.
Is there a way that I can just pass the name of the controls and
populate them in a common method.
 
R

Ravikanth[MVP]

Hi

You can add items programmatically at run time to all
list Web server controls: ListBox, DropDownList,
CheckBoxList, and RadioButtonList.

You can add items to a list Web server control in these
ways:

To add items programmatically

Create a new object of type ListItem and set its Text and
Value properties.

Call the Add method of the control's Items collection and
pass it the new object.

The following example shows how to add ListItem objects
to a ListBox control, but the procedure is identical for
all list Web server controls.

' Visual Basic
Private Sub Button1_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles Button1.Click
Me.ListBox1.Items.Add(New ListItem("Carbon", "C"))
Me.ListBox1.Items.Add(New ListItem("Oxygen", "O"))
End Sub

// C#
public void Button1_Click (object sender,
System.EventArgs e)
{
this.ListBox1.Items.Add(new ListItem("Carbon", "C"));
this.ListBox1.Items.Add(new ListItem("Oxygen", "O"));
}


HTH
Ravikanth
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top