How to programmatically add a Drop down list in asp.net with specific pre-selected item

I've worked out how to create a DropDownList using the following code:

<select id="salesPersonDropList" runat="server"></select>

In my .aspx page, then my code behind loops through database output running:

Dim newListItem As ListItem
newListItem = New ListItem("Title", "Value")
salesPersonDropList.Items.Add(newListItem )

What I can't figure out is how to programatically set which of the List Items created is the one to be pre-selected in the rendered DropDownList, ie, how to create what I'd write in HTML as:

<select>
     <option value="1">1</option>
     <option selected value="2">2</option>
</select>

Based on database output. As the code behind loops through the database output it should compare the output to a session variable and if they values match, the ListItem should be the item selected in the rendered DropDown.

Answers


Set your Selected property of the ListItem to true:

Dim newListItem As ListItem
newListItem = New ListItem("Title", "Value")
newListItem.Selected = True
salesPersonDropList.Items.Add(newListItem )

Need Your Help

Use stored procedure return value in both select and where clause, without having to call it twice?

sql postgresql stored-procedures

I have a stored procedure that returns a single value. I want to do a SELECT query that both selects the stored procedure's return value, as well as use that value in the WHERE clause.

how show the diff from my current working directory and my last commit?

git staging git-diff working-directory

I'm using git and need included in the diff result untracked files. So what command I must execute to get all the difference between my current working directory and the HEAD, even part of the diff...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.