VB.net Tutorials with Examples

VB.Net Projects

VB.Net Project 1

adplus-dvertising
DropDownList in ASP .NET
Previous Home Next

A DropDownList is also commonly known as combo box. It can contain multiple data members, but unlike a normal list box the users can choose only one value from this control. Though the functionality of this DropDownList is much like a Single Row Select List Box, a DropDownList can save a lot of GUI space as it is rendered on a Single line and is expanded only when the user clicks on the Control.

This DropDownList is provided as a Server Control in ASP .Net like many other controls. This DropDownList can be used to add data manually or even for dynamic binding with data base.

A simple declaration for DropDownList can be done as below.

<asp:DropDownList ID="DropDownList1" 
runat="server" Height="47px"
Width="73px"></asp:DropDownList>

Properties Of DropDownList Control

Properties Discription
BorderColor enables to get or set the border color of the web server control. [Not Applicable]
BorderStyle enables to get or set the border style of the control. [Not Applicable]
BorderWidth enables to get or set the border width for the control. [Not Applicable]
SelectedIndex enables to get or set an Integer value to specify the zero-based index for the currently selected list item.
AppendDataBoundItems enables to get or set a Boolean value to indicate whether to clear the list items before binding the data to the control.
AutoPostBack enables to get or set a Boolean value that indicates whether to postback the web page when list item of DropDownList control is clicked.
DataTextField enables to get or set the name of the data source field that provides the text for the list items.
DataTextFormatString enables to get or set the string formatting to control the text of each list item that is to be displayed.
DataValueField enables to get or set the name of the data source field that provides the value for the list items.
Items enables to manage the list items of the DropDownList control.
SelectedItem enables to get the currently selected list item in DropDownList control.
SelectedValue enables to get the value property of selected list item that returns ListItem object
Text enables to get or set the text for the control.
ValidationGroup enables to get or set the name of the group of controls to which it belongs to and causes validation when posts back to the server.
DataMember enables to get or set the name of the list of data that the data-bound control binds to. The list of data items may have more than one data members.
DataSourceID enables to get or set the ID of the DataSource control from which the data-bound control retrieves its list of data items.
DataSourceObject enables to get an object that implements the IDataSource interface and provides the access to the data items.
DataSource enables to get or set the object from which the data-bound control retrieves its list of data items.
CausesValidation enables to get or set a Boolean value to indicate whether to perform the validation when any list item of the control is clicked.
Events

SelectedIndexChanged: Occurs when selected item of list control changes between postbacks to the server.

TextChanged: Occurs when Text and SelectedValue properties change between postbacks to the server.

Example

.vb Code:

Partial Class Default4
    Inherits System.Web.UI.Page
    Protected Sub Page_Load(ByVal sender As Object, 
	ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
DropDownList1.Items.Add("pototo")
DropDownList1.Items.Add("Tamato")
DropDownList1.Items.Add("brinjal")
DropDownList1.Items.Add("Lime")
DropDownList1.Items.Add("Pea")
DropDownList2.Items.Add("Jeans")
DropDownList2.Items.Add("Shirt")
DropDownList2.Items.Add("Cap")
DropDownList2.Items.Add("Blaser")
DropDownList2.Items.Add("Tie")
DropDownList3.Items.Add("Lather")
DropDownList3.Items.Add("Canvas")
DropDownList3.Items.Add("Sports")
DropDownList3.Items.Add("Sandle")
DropDownList3.Items.Add("Sleeper")
DropDownList4.Items.Add("Fan")
DropDownList4.Items.Add("TybeLight")
DropDownList4.Items.Add("Plug")
DropDownList4.Items.Add("Holder")
DropDownList4.Items.Add("Wire")
End If
    End Sub
    Protected Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, 
	ByVal e As System.EventArgs)
	 Handles DropDownList1.SelectedIndexChanged
ListBox1.Items.Add("Vegitables:" + DropDownList1.SelectedItem.Text)
    End Sub
    Protected Sub DropDownList2_SelectedIndexChanged(ByVal sender As Object, 
	ByVal e As System.EventArgs) Handles DropDownList2.SelectedIndexChanged
ListBox1.Items.Add("Cloths:" + DropDownList2.SelectedItem.Text)
    End Sub
    Protected Sub DropDownList3_SelectedIndexChanged(ByVal sender As Object, 
	ByVal e As System.EventArgs)
	  Handles DropDownList3.SelectedIndexChanged
ListBox1.Items.Add("Showes:" + DropDownList3.SelectedItem.Text)
    End Sub
    Protected Sub DropDownList4_SelectedIndexChanged(ByVal sender As Object, 
	ByVal e As System.EventArgs) Handles DropDownList4.SelectedIndexChanged
ListBox1.Items.Add("Electronics:" + DropDownList4.SelectedItem.Text)
    End Sub
End Class
Previous Home Next