Live Chat Icon For mobile
Live Chat Icon

ASP.NET FAQ - Dropdownlist

Find answers for the most frequently asked questions
Expand All Collapse All

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

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

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

<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

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

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

Share with

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

Please submit your question and answer.