Higlight dropdown list item

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pavanip
    New Member
    • Mar 2008
    • 53

    Higlight dropdown list item

    Hi,

    I am having 2 dropdown lists 1st dropdownlist contains A-Z list.2nd dropdown list contains some names like akila,bindu,chi nni,....zibra etc.
    when i select name in 2nd dropdown list, the first letter of that name should be highlight in 1st dropdown.eg. i select chinni then "C" should be highlight in 1st dropdown. Please help me how to resolve this problem.


    Thanks in advance
    Pavani
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    If you want to solve this using ASP.NET, enable "AutoPostBa ck" for the second DropDownList and then implement a method that will handle that control's SelectedIndexCh anged Event.

    For example, you'd set AutoPostBack="T rue" in your second DropDownList:
    Code:
    <asp:DropDownList ID="mySecondDropDownList" runat="server" AutoPostBack="True"></asp:DropDownList>
    And then in your VB.NET code (or C# code) implement a method that handles the SelectedIndexCh anged Event:
    Code:
    Private Sub mySecondDropDownList_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles mySecondDropDownList.SelectedIndexChanged
        'In this method you want to check the what letter the
        'mySecondDropDownList.SelectedItem.Text  starts with
        'and then select the corresponding item in 
        'the firstDropDownList
    End Sub
    -Frinny

    Comment

    • pavanip
      New Member
      • Mar 2008
      • 53

      #3
      Higlight dropdown list item when i select 2nd dropdown list

      Thanks for your response I got solution for my problem using the following code
      Code:
      foreach (ListItem item in ddlcont.Items)
      {
          if (item.Text.StartsWith(txtcont.Text, StringComparison.CurrentCultureIgnoreCase))
          {
              ddlcont.SelectedIndex = ddlcont.Items.IndexOf(item);
           }
      }
      Last edited by Frinavale; Feb 2 '09, 02:17 PM. Reason: added [code] tags

      Comment

      • Frinavale
        Recognized Expert Expert
        • Oct 2006
        • 9749

        #4
        Does your code actually work for you?
        Why are you looping through all of the items in your DropDownList?

        Comment

        • shweta123
          Recognized Expert Contributor
          • Nov 2006
          • 692

          #5
          Hi,

          You can try the following code in SelectedIndexCh anged event of second dropdownbox.

          Code:
          protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
              {
                 //Here get the first character of the string item selected
          
                  string strchar = DropDownList2.SelectedItem.Text.Substring(0, 1).ToString();
          
                  // Highlight the item in the other dropdown accordingly
          
                  DropDownList1.SelectedItem.Text = strchar;
          
              }
          Note : Please make Autopostback property = true for Dropdownlist2 in order to work the above specified code.

          Comment

          Working...