ASP.NET FAQ - Dropdownlist

How to apply css style to each item of the dropdownlist or listbox control

The dropDownList has a bug which prevent us from assigning the style property to each item in the DropDownList.
This bug confirmed by Microsoft in Microsoft Knowledge Base Article – 309338
For the workaround use a HTML dropdownlist with runat=server tag

<SELECT id='DropDownList1' runat='server' >
</SELECT>

Use namespace System.Reflection
VB.NET

If Not Page.IsPostBack Then
Dim col As FieldInfo
For Each col In GetType(KnownColor).GetFields
	If col.FieldType Is GetType(Drawing.KnownColor) Then
		DropDownList1.Items.Add(New ListItem(col.Name, col.Name))
	End If
Next
End If

Dim i As Integer
For i = 0 To DropDownList1.Items.Count - 1
	DropDownList1.Items(i).Attributes.Add('style', 'background-color:' + DropDownList1.Items(i).Text)
Next

C#

if (!IsPostBack)
{
	foreach(FieldInfo col in typeof(KnownColor).GetFields() )
	{
		if (col.FieldType == typeof(KnownColor) )
		{
		DropDownList1.Items.Add(new ListItem(col.Name ,col.Name));
		}
	}  
}
for (int i= 0 ;i < DropDownList1.Items.Count;i++)
{
	DropDownList1.Items[i].Attributes.Add('style', 'background-color:' + DropDownList1.Items[i].Text);
}
Permalink
How can I display a Tool tip for a dropdownlist

The ToolTip property is inherited from the WebControl class and is not applicable to the DropDownList control. This implementation of the ToolTip property does not allow you to set a value and returns String. In a word, if we can make this (a dropdown with ToolTip) work with plain HTML and client side script, we should be able to emit the necessary code from ASP.NET.

More Details at DropDownList.ToolTip Property

Permalink
Can I use a dropdownlist to allow user to select multiple items from a list of available items

No. Dropdownlist Web Control allows user to select only one item from a list of items.

Permalink
I have a page with a Dropdownlist and some simple code for the SelectedIndexChanged but it does not do a postback

If you want the dropdownlist to cause a postback when selection changes set AutoPostBack property to ‘True’ for the dropdownlist control.

Permalink
How to select a specific Item in a DropDownList

Method 1:
VB.NET

DropDownList1.SelectedIndex = DropDownList1.Items.IndexOf(DropDownList1.Items.FindByValue(<value>))
’DropDownList1.SelectedIndex = DropDownList1.Items.IndexOf(DropDownList1.Items.FindByText(<Text>))

C#

DropDownList1.SelectedIndex = DropDownList1.Items.IndexOf(DropDownList1.Items.FindByValue(<value>));
//DropDownList1.SelectedIndex = DropDownList1.Items.IndexOf(DropDownList1.Items.FindByText(<Text>));

Method 2:
VB.NET

DropDownList1.Items.FindByText('<Text>').Selected = true
’DropDownList1.Items.FindByValue('<value>').Selected = true

C#

DropDownList1.Items.FindByText('<Text>').Selected = true ;
//DropDownList1.Items.FindByValue('<value>').Selected = true ;
Permalink
How to add an extra item to the DropDownList filled with data from a database?

VB.NET

’DropDownList1.Items.Insert(0, 'Please Select')
DropDownList1.Items.Insert(0, new ListItem('<text>', '<value>'))

C#

//DropDownList1.Items.Insert(0, 'Please Select');
DropDownList1.Items.Insert(0, new ListItem('<text>', '<value>'));

This should be done after .DataBind of dropdownlist

Permalink
How to display multiple spaces in a dropdownlist
<asp:DropDownList id='DropDownList1' runat='server'></asp:DropDownList>

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
	Dim i As Integer
	For i = 0 To 10
		DropDownList1.Items.Add('Item Number' + SpaceDDL(10) + i.ToString)
	Next
End Sub
  
Private Function SpaceDDL(ByVal numberOfSpaces As Integer) As String
  	Dim Spaces As String
	Dim i As Integer
	For i = 0 To numberOfSpaces
	            Spaces &= ' '
	Next
	Return Server.HtmlDecode(Spaces)
End Function

C#

private void Page_Load(object sender, System.EventArgs e)
{
	// Put user code to initialize the page here
  	for(int i = 0 ;i<=10;i++)
	{
		DropDownList1.Items.Add('Item Number' + SpaceDDL(10) + i.ToString());
	}
}

string SpaceDDL(int numberOfSpaces )  
{
	string Spaces=''; 
	for(int i = 0 ;i<=numberOfSpaces;i++)
	{
		Spaces += ' ';
	}
	return Server.HtmlDecode(Spaces);
}
Permalink
Why do I get System.Data.DataRowView/System.Data.Common.DbDataRecord as the item in a dropdownlist

This is probably because you have not set the DataTextField/ DatavalueField property of the dropdownlist.

VB.NET

’..
DropDownList1.DataSource = ds.Tables(0)
DropDownList1.DataTextField = 'CategoryName'
DropDownList1.DataValueField = 'CategoryId'
DropDownList1.DataBind()

C#

//..
DropDownList1.DataSource =ds.Tables[0];
DropDownList1.DataTextField = 'CategoryName';
DropDownList1.DataValueField = 'CategoryId';
DropDownList1.DataBind();
Permalink
How to concatenate and display 2 fields in a dropdownlist

Method1

'Select FirstName + ’ ’ + LastName as fullname from Employees'

then use ’fullname’ as the datatextfield for the dropdownlist

Method2

Dim newColumn As New DataColumn()
With newColumn
 	.ColumnName = 'fullname'
	.DataType = System.Type.GetType('System.String')
	.Expression = 'LastName+’ ’+FirstName'
End With
ds.Tables(0).Columns.Add(newColumn)
ddlName.DataTextField = 'FullName'
ddlName.DataValueField = 'employeeid'
ddlName.DataSource = ds.Tables(0).DefaultView
ddlName.DataBind()

C#

DataColumn newColumn =new DataColumn();
newColumn.ColumnName = 'fullname';
newColumn.DataType = System.Type.GetType('System.String');
newColumn.Expression = 'LastName+’ ’+FirstName';
ds.Tables[0].Columns.Add(newColumn);
DropDownList1.DataTextField = 'FullName';
DropDownList1.DataValueField = 'employeeid';
DropDownList1.DataSource = ds.Tables[0].DefaultView;
DropDownList1.DataBind();
Permalink
I have 3 DropDownLists on the page : one for a Month, second one for a Day, and the last one for a Year. How can I let the user choose the data and enter it to ‘datetime’ column in the database.

VB.NET

Dim dt as DateTime = DateTime.Parse(ddlMonth.SelectedItem.Text & '/' & ddlDay.SelectedItem.Text & '/' & ddlYear.SelectedItem.Text )

C#

DateTime dt = DateTime.Parse(ddlMonth.SelectedItem.Text & '/' & ddlDay.SelectedItem.Text & '/' & ddlYear.SelectedItem.Text );
Permalink