DropDownList.SelectedItem

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,
Anand
LVL 1
an_andAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Aleksey_ShturmanCommented:
For selecting of default value - I use the following code:
...
DropDownList.Items.FindbyValue("1008").Selected = true;
...
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Anurag ThakurTechnical ManagerCommented:
itemCodeToBeSelected = "1008";
ddlList.SelectedValue = itemCodeToBeSelected;
the only thing is that the itemCodeToBeSelected has to exist in the list of values
0
an_andAuthor Commented:
@Aleksey_Shturman
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 ?...
0
Amazon Web Services

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.

Anurag ThakurTechnical ManagerCommented:
did you try what i have done casuse i have been using this in my entire application
ID:22957891
0
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"
dropdownlist.JPG
0
ahllCommented:
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
0
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
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.