Skip to main content
DropdownList Selected Value in ASP.NET

How to Set DropdownList Selected Value in ASP.NET?

DropdownList is one of the standard controls available in asp.net toolbox in visual studio.

DropdownList Control

To Set DropdownList Selected Value in ASP.NET we have two different methods:

  • Based on value
  • Based on text
How to Set DropdownList Selected Value in ASP.NET? #aspnet #dropdownlist Click To Tweet

Based on Value

With FindByValue property we can set dropdownlist selected value as shown below:

// set dropdownlist selected based on value
ddlusers.Items.FindByValue("2").Selected = true;

Based on Text

using FindByText property we can set dropdownlist selected value as shown below:

// set dropdownlist selected based on text
ddlusers.Items.FindByText("Delhi").Selected = true;

Try yourself with the help of a complete example. Open your aspx page and write the following code:

ASPX Page

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>set dropdownlist default value in asp.net</title>
</head>
<body>

<form id="form1" runat="server">

<div>
            <asp:DropDownList ID="ddlCities" runat="server">
                <asp:ListItem Value="1">Mumbai</asp:ListItem>
                <asp:ListItem Value="2">Delhi</asp:ListItem>
                <asp:ListItem Value="3">Kolkata</asp:ListItem>
                <asp:ListItem Value="4">Jaipur</asp:ListItem>
                <asp:ListItem Value="5">Bangalore</asp:ListItem>
            </asp:DropDownList>
        </div>

    </form>

</body>
</html>

Code Behind – C# Code

In code-behind file of aspx page, you can write the code on Page_Load event as shown below:

using System;
namespace DropdownListExample
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                // set dropdownlist selected based on text
                ddlCities.Items.FindByText("Delhi").Selected = true;
            }
        }
    }
}

Same code in VB.NET Code

Namespace DropdownListExample
	Public Partial Class [Default]
		Inherits System.Web.UI.Page
		Protected Sub Page_Load(sender As Object, e As EventArgs)
			If Not IsPostBack Then
				' set dropdownlist selected based on text
				ddlCities.Items.FindByText("Delhi").Selected = True
			End If
		End Sub
	End Class
End Namespace

Same can be achieved using FindByValue property in case you want to work with values rather than text.

Output

“Delhi” will be the selected text when you run this example.

DropdownList Selected Value in ASP.NET

Download

you can download the above explained example.

[sociallocker]DropdownList Example[/sociallocker]

What do you think?

Dear Readers,
Your questions or suggestions are always welcome. Feel free to email us or put your thoughts as comments below. In addition to that, if you found this post useful then please share along with your friends and help them to learn.

Keep Learning!

DotNetCrunch

DotNetCrunch is a popular blog for latest Microsoft® technologies which is aimed for beginners and intermediate level professionals.