Posted on 2008-11-13
Last Modified: 2013-12-17
Hi There,
I have one Dropdownlist control in my 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
I dont want to use SelectedIndex because some time the index locaition of the item will be changed.

Best Regards,
Question by:an_and
    LVL 1

    Accepted Solution

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

    Assisted Solution

    by:Anurag Thakur
    itemCodeToBeSelected = "1008";
    ddlList.SelectedValue = itemCodeToBeSelected;
    the only thing is that the itemCodeToBeSelected has to exist in the list of values
    LVL 1

    Author Comment

    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 ?...
    LVL 26

    Expert Comment

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

    Author Comment

    @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"
    LVL 3

    Expert Comment

    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" ragi0017 solution would work
    LVL 26

    Expert Comment

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

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my 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 describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    746 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

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now