Solved

How to programmatically select dropdownlist item (asp.net / web)

Posted on 2013-05-29
3
782 Views
Last Modified: 2013-06-03
(code behind is in C#, btw)

drop down list has several values.

In the code behind I want to programmatically select one of them based-upon the "friendly" value, not the ID:

rendered html:
<select name="ctl08$ddlBpoType" id="ctl08_ddlBpoType" style="width:300px;">
	<option selected="selected" value="0">Unassigned</option>
	<option value="223">3rd Opinion</option>
	<option value="219">Appraisal</option>
	<option value="1809">CMA</option>
	<option value="222">Drive By</option>
	<option value="218">ListingAgent</option>
	<option value="913">ListingAgent Updated</option>
	<option value="220">Supplemental</option>
	<option value="1927">VRR</option>

</select>

Open in new window



ddlBpoType.SelectedValue = "Supplemental";

does not work, but

ddlBpoType.SelectedValue = "220";

does.


Except i don't want to use "220" because it might not be "220".

I want to get to the "220" value by using the friendly name of "Supplemental".
0
Comment
Question by:knowlton
3 Comments
 
LVL 19

Accepted Solution

by:
Manoj Patil earned 250 total points
ID: 39206800
Try this
string selectedItem= ddlBpoType.SelectedItem.Text

AND

If you want to set the  item as selected in DropDown then try this

dropdownlist.Items.FindByValue(value).Selected = true;


Get some more ideas about DropDownList
http://asp-net-example.blogspot.in/2009/03/how-to-find-item-by-value-in.html
0
 
LVL 13

Assisted Solution

by:Naman Goel
Naman Goel earned 250 total points
ID: 39208665
You can make use of FindByText method and take index of ListItem in dropdownlist using IndexOf method :

Dim li As ListItem

        li = ddlBpoType.Items.FindByText(CycleNo1.Text)
        ddlBpoType.SelectedIndex = ddlBpoType.Items.IndexOf(li)

Open in new window

0
 
LVL 5

Author Closing Comment

by:knowlton
ID: 39216520
Thanks!
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question