[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

DropDownList.SelectedItem

Posted on 2008-11-13
7
Medium Priority
?
542 Views
Last Modified: 2013-12-17
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
0
Comment
Question by:an_and
7 Comments
 
LVL 1

Accepted Solution

by:
Aleksey_Shturman earned 450 total points
ID: 22957789
For selecting of default value - I use the following code:
...
DropDownList.Items.FindbyValue("1008").Selected = true;
...
0
 
LVL 26

Assisted Solution

by:Anurag Thakur
Anurag Thakur earned 450 total points
ID: 22957891
itemCodeToBeSelected = "1008";
ddlList.SelectedValue = itemCodeToBeSelected;
the only thing is that the itemCodeToBeSelected has to exist in the list of values
0
 
LVL 1

Author Comment

by:an_and
ID: 22958131
@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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 26

Expert Comment

by:Anurag Thakur
ID: 22958160
did you try what i have done casuse i have been using this in my entire application
ID:22957891
0
 
LVL 1

Author Comment

by:an_and
ID: 22958256
@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
 
LVL 3

Expert Comment

by:ahll
ID: 22960235
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
 
LVL 26

Expert Comment

by:Anurag Thakur
ID: 22960586
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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
Screencast - Getting to Know the Pipeline
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses
Course of the Month18 days, 1 hour left to enroll

830 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