1 Replies - 1380 Views - Last Post: 04 January 2012 - 08:45 AM

#1 erin  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 0
  • Joined: 09-February 09

Select by text in a Dropdown List

Posted 09 February 2009 - 11:58 AM

Description: There are two ways included. To add this property to your DropDownLists, create a new control that inherits from DropDownList and add the property. If you don't want to create a control, put the method into your page, and call it on the dropdown. Sometimes I wish that Dropdown lists in ASP.NET 2.0 had a SelectedText Property that I could use like the SelectedValue property. I found that adding a property to my a control extending from Dropdown list solves my problem. Since it might seem like a bit of a hassle to create a control just for this, I have included a page method version. You should try to work with the selectedValue and SelectedIndex properties if you can, and resort to this method in cases like when some other form that you can't change passes you the text that you want to select in a dropdown, instead of the value.
Property:

        /// 
        ///Gets the text of the selected listitem in the control, 
        /// or selects the first control that has the specified text
        /// 
        public string SelectedText
        {
            get
            {
                return this.SelectedItem.Text;
            }
            set
            {
                ListItem listItemToFind = this.Items.FindByValue(value);
                if (this.Items.Contains(listItemToFind))
                {
                    this.SelectedValue = listItemToFind.Value;
                }
            }
        }

Method
        public void SelectByText(DropDownList list, string text)
        {
            ListItem listItemToFind = list.Items.FindByValue(text);
            if (listItemToFind != null)
            {
                if (list.Items.Contains(listItemToFind))
                {
                    list.SelectedValue = listItemToFind.Value;
                }
            }
        }


Is This A Good Question/Topic? 0
  • +

Replies To: Select by text in a Dropdown List

#2 assiance  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 7
  • Joined: 23-October 08

Re: Select by text in a Dropdown List

Posted 04 January 2012 - 08:45 AM

Here's some selenium webdriver helper classes that allow you to do the functions above plus more. It also supports Tables, Checkboxes, etc... https://github.com/A...-C-Sharp-Helper
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1