DropDownList control in ASP.NET using VB.NET

In this article we will learn how to use DropDownList control in ASP. NET.
  • 3702

In this article we will learn how to use DropDownList control in ASP. NET.

DropDownList control:

-DropDownList Allows to select only one item from many items.
-Use Items collection to add the items.
-Each item is an object of ListItem class ListItem(string text, string value).

Properties:

Items - The collection of items in the list.

ID - Programmatic name of the control.

AutoPostBack - Automatically postback to the server after selection is changed.

AccessKey - keyboard shortcut used by the control.

DropDownList event:

Protected Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles DropDownList1.SelectedIndexChanged

End Sub

For example:

Drag a DropDownList, Button and TextBox control on the form. when we select the items in the DropDownList and click on the button its should be display on the Textbox control.

The form looks like this:

dd1.gif
 

Figure 1.

Now double click on the form and add the below code.

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

   If Not IsPostBack Then

            For i As Integer = 1950 To DateTime.Now.Year

                DropDownList1.Items.Add(New ListItem(i.ToString()))

            Next

        End If

    End Sub

 

Use Page.IsPostBack property to check the post backs.

 

Now double click on the button control and add the following code.

 

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click

 

        TextBox1.Text = DropDownList1.SelectedItem.Text.ToString()

 

    End Sub

 

Now save and run the application And click the DropDownList than DropDownList items will be display.
 

 

dd2.gif
 

Figure 2.

 

 

Now select the items from the DropDownList then click on the button the selected items will be display on the TextBox.


 

dd3.gif
 

Figure 3.

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.