• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 555
  • Last Modified:


Hi There,
I have one Dropdownlist control in my asp.net project and the item where add while runtime.

The DropdownList has items like given below.

("itemcode" - "itemname")
1003 - foo
1008 - bar
1005 - some value5
1010 - some value10

So now what is need is i want to set the SelectedItem is item which STARTSWITH the code "1008" using c# or vb.net.
I dont want to use SelectedIndex because some time the index locaition of the item will be changed.

Best Regards,
2 Solutions
For selecting of default value - I use the following code:
DropDownList.Items.FindbyValue("1008").Selected = true;
Anurag ThakurTechnical ManagerCommented:
itemCodeToBeSelected = "1008";
ddlList.SelectedValue = itemCodeToBeSelected;
the only thing is that the itemCodeToBeSelected has to exist in the list of values
an_andAuthor Commented:
i used your code but the problem is that it was getting some exception {"Object reference not set to an instance of an object."}

DDLcontrol1l.Items.FindByValue("1002").Selected = true;

could you help on this ?...
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

Anurag ThakurTechnical ManagerCommented:
did you try what i have done casuse i have been using this in my entire application
an_andAuthor Commented:
@ragi0017 sir could you please tell me what is itemCodeToBeSelected ? is it string or ?
if its a string we cant select the listitem using only the first 4 digits "itemcode (1002)" , we have to use full item name like "1002 - an_and2"
Hi..can you post the code for your DropdownList?

You have to have a value for each record in the list. The text can be whaterver you want  but the value would be only "1002"...so ragi0017 solution would work
Anurag ThakurTechnical ManagerCommented:
i was giving my solution based on some assumption that you are binding
ddl.DataValueField = "value";
ddl.DataTextField = "Text";
if you are talking that approach then my suggested approach will work as you can supply the value in the .SelectedValue and it will get selected in the dropdown list

if not then let us know i will provide you a different solution based on the text of the ddl
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now