Live Chat Icon For mobile
Live Chat Icon

How to add items dynamically to a ListBox using an ArrayList

Platform: ASP.NET| Category: ListBox

<asp:ListBox id='ListBox1' runat='server' AutoPostBack='True'></asp:ListBox>

VB.NET


Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
	’Put user code to initialize the page here
	If Not IsPostBack Then
		Dim arrList As New ArrayList
		arrList.Add('One')
		arrList.Add('Two')
		arrList.Add('Three')
		arrList.Add('Four')
		ListBox1.DataSource = arrList
		ListBox1.DataBind()
	End If
End Sub

Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
	Response.Write(ListBox1.SelectedItem.Text)
End Sub

C#


private void Page_Load(object sender, System.EventArgs e)
{
	// Put user code to initialize the page here
	if (!Page.IsPostBack )
	{
		ArrayList arrList = new ArrayList();
		arrList.Add('One');
		arrList.Add('Two');
		arrList.Add('Three');
		arrList.Add('Four');
		ListBox1.DataSource = arrList;
		ListBox1.DataBind();
	}
}

private void ListBox1_SelectedIndexChanged(object sender, System.EventArgs e)
{
	Response.Write (ListBox1.SelectedItem.Text   );
}

Share with

Share on twitter
Share on facebook
Share on linkedin

Related FAQs

Couldn't find the FAQs you're looking for?

Please submit your question and answer.