How to find value or text in DropDownList

Posted on 2011-10-21
Last Modified: 2012-05-12
I have a DropDownList with the text  displayed as

Z11-Elements of Chemistry

I d like to search only for 'Z11' or 'A97'-  
or the first 3 strings...
Question by:zachvaldez
    LVL 10

    Expert Comment

    by:Umar Topia
    You will have to use a plugin provided by JQuery (something named as AutoFill)

    or else write your own code to loop through the datasource and filter it based on the typed in characters
    LVL 20

    Expert Comment

    Seems simple enough but can you define the problem a little better. Search where? During what part of the process? When do you want to sort out these items? How is the dropdownlist filled if that's relavent. Show some code if it would be helpful.

    For example:

    After the user selects a choice in the dropdown then they do some other junk and press a submit button. At this point we want to find the 3 alpha-numerics that were selected as described.

    LVL 83

    Expert Comment

    If you want to get the first three letters after user makes a selection in the dropdown, you can use some string functions.

    Dim Value As String = ddl.SelectedValue
    Dim ID as String

    ID = Value.Split("-")(0)

    ID = Value.Substring(0, 3)


    Author Comment

    here's the direction Im trying to accomplish...

     If e.CommandName = "Edit" Then

      Dim lblFChoice As Label = CType(e.Item.FindControl("lblfirstchoiceA"), Label)

    'Here is where Im  having error trying to define and search for the text...
        DropDownList9.SelectedValue = lblFChoice.Text(this line is incorrect) I think I need to find the value of the label in the dropdownlist9. Im only searching for the fors 3 chars of the datatext pf the ddl.

    BTW I load my dropdownlist9 with this code... and this work

       Dim xdoc As New System.Xml.XmlDocument()
            Dim items As New List(Of String)
            If Not IsPostBack Then

                For Each row As System.Xml.XmlNode In xdoc.SelectNodes("//row")
                    Dim item As String = row.SelectSingleNode("Workshop").InnerText

                    item = String.Concat(item, "-", row.SelectSingleNode("Title").InnerText)



                Me.DropDownList9.DataSource = items

    Any idea?
    LVL 83

    Expert Comment

    > I think I need to find the value of the label in the dropdownlist9

    You want the value from the dropdownlist then what does the label has to do with it?

    Assisted Solution

    If the label has text of z11 for instance,it should find that text  in Ddl as z11-Elements.
    And rebinds the ddl. I think I phrse question not that clear.

    Accepted Solution

    here is the answer to my own question
              Dim lblFChoiceA As Label = CType(e.Item.FindControl("lblfirstchoiceA"), Label)
                Dim searchString As String = lblFChoiceA.Text.ToString()

                Dim iA As Integer
                For iA = 0 To Me.DropDownList9.Items.Count - 1
                    If Left(Me.DropDownList9.Items(iA).Text, 3) = searchString Then
                        Me.DropDownList9.SelectedIndex = iA
                        Exit For
                    End If

    Author Closing Comment

    Im posting the solution to this question.

    Featured Post

    Training Course: Java/J2EE and SOA

    This course will cover both core and advanced Java concepts like Database connectivity, Threads, Exception Handling, Collections, JSP, Servlets, XMLHandling, and more. You'll also learn various Java frameworks like Hibernate and Spring.

    Join & Write a Comment

    Article by: jpaulino
    XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String ( Literal, only instead of starting and ending with w…
    Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
    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…
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    730 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

    16 Experts available now in Live!

    Get 1:1 Help Now