How to Get or Selected items in a RadListBox?

4 posts, 0 answers
  1. Dan
    Dan avatar
    1 posts
    Member since:
    Nov 2010

    Posted 02 Aug 2011 Link to this post

    I would think this would be simple.  Trying to test a web page which contains a RadListBox I'm able to get it just fine.   The problem appears nothing indicates what is  selected in the list.  I would think SelectedIndex or SelectedItem would work but both are useless.

    My code:

            public string GetSelectedItem(HtmlDiv parentElement, string controlID)
            {
                RadListBox theListbox = parentElement.Find.ById<RadListBox>(controlID);
                
                //  proof the code is getting the list box and can get a RadListBoxItem...
                for( int idx = 0; idx < theListbox.ChildNodes.Count(); idx++ )
                {
                    RadListBoxItem  li = theListbox.ChildNodes[idx];
                    string xx = li.TextContent;
                }

                // only after this:
                return theListbox.ChildNodes[theListbox.SelectedIndex].TextContent ;
            }

    And going further i would think if I'm able to get a ListBoxItem, while interating through the for loop testing if a given item has the text that i would like to set, when the time come i thought i would need to do something like:

    public Boolean SetSelectedItem(HtmlDiv parentElement, string controlID)
    {
                RadListBox theListbox = parentElement.Find.ById<RadListBox>(controlID);
                
                //  proof the code is getting the list box and can get a RadListBoxItem...
                for( int idx = 0; idx < theListbox.ChildNodes.Count(); idx++ )
                {

                    RadListBoxItem  li = theListbox.ChildNodes[idx];
                    if( li.TextContent.Equals( "???) )
                    {
                            li.Selected = true;
                            // or
                            theListBox.SelectedItem = li;
                            //
                            theListBox.SelectedIndex = idx;
                           return true;
                }
                return false;
    }

    But nothing seems to be working and the SelectedItem and SelectedIndex are both read only.  

    Any suggestions.   This seems simple, but couldn't find any samples on here or documentation for the Art Of Test or Telerik controls which i've seen that is useful.  Any suggestions are welcome.

                // only after this:
                return theListbox.ChildNodes[theListbox.SelectedIndex].TextContent ;
            }
  2. Helen
    Admin
    Helen avatar
    1052 posts

    Posted 04 Aug 2011 Link to this post

    Hello Dan,

    Please try the code below:

    public string GetSelectedItemText(string controlID)
       {
           RadListBox theListbox = Find.ById<RadListBox>(controlID);
     
           //  proof the code is getting the list box and can get a RadListBoxItem...
           for (int idx = 0; idx < theListbox.Items.Count; idx++)
           {
               RadListBoxItem li = theListbox.Items[idx];
               if (theListbox.Items[idx].Selected == true)
               {
                   return theListbox.Items[idx].Text;
               }
     
           }
     
           return null;
       }
     
       public void SetSelectedItem(string controlID, string text)
       {
           RadListBox theListbox = Find.ById<RadListBox>(controlID);
     
           for (int idx1 = 0; idx1 < theListbox.Items.Count; idx1++)
           {
     
               RadListBoxItem li = theListbox.Items[idx1];
               if (li.Text.Equals(text))
               {
                   li.Selected = true;
     
     
               }
                
           }
       }
     
       [TestMethod]
       public void Test()
       {
           Manager.LaunchNewBrowser();
     
           RadListBox listBox1 = Find.ById<RadListBox>("RadListBox1");
     
           RadListBoxItem itemFiction = listBox1.Items[6];
           Assert.AreEqual("Fiction", itemFiction.Text);
     
           itemFiction.Selected = true;
     
           Assert.AreEqual("Fiction", GetSelectedItemText("RadListBox1"));
     
           SetSelectedItem("RadListBox1", "Cooking");
           Assert.AreEqual("Cooking", GetSelectedItemText("RadListBox1"));
     
            
       }

    RadListBoxItem wrapper has a "Selected" property and a 'Select' method.

    Hope it helps.

    Regards,
    Helen
    the Telerik team
    Check out the Test Studio roadmap to find out more about the new performance testing functionality coming in our R2 2011 release this September!

    Have you looked at the new Online User Guide for Telerik Test Studio?
  3. Dan
    Dan avatar
    1 posts
    Member since:
    Nov 2010

    Posted 04 Aug 2011 Link to this post

    Thanks for the reply, however the suggest doesn't appear to work.   What i'm confused about....   Given i search for the control by:

    var lstBox  = parent.Find.ByID( controlID) ;
    I'll get the control just fine,  looking in the debugger i saw it was reporting the type as a RadListBox,  in actuality the control in question is really a combo box.   But trying to search for or cast the object to a ComboBox i'll get a run time error "Unable to cast object". 

    So if i then try using the code provided as the debugger and the find routines for Telerik identify the control as a RadListBox  trying to  use:

                    RadListBox lstBox = _assetFilterContainer.Find.ById<RadListBox>(ConsoleConstants.ApplyFilterPopUpAssetCultureList);
                    // lstBox.Items.Count == 0 ;
                   // lstBox.base.base.ChildNodes.count == 0x5b;
                    string xxx = lstBox.Items[5].TextContent;  // or lstBox.Items[5].Text;

    Both raise an exception.   I found a work around that appears to work fine but seems crazy the regular telerik controls have troubles here...

  4. Helen
    Admin
    Helen avatar
    1052 posts

    Posted 05 Aug 2011 Link to this post

    Hi Dan,

    Did you try the test I send you previously against our live demos:

    http://demos.telerik.com/aspnet-ajax/listbox/examples/functionality/checkboxes/defaultcs.aspx

    Does it work at your side?
    Could you send us a sample .aspx page you test against to examine it locally?


    Regards,
    Helen
    the Telerik team
    Vote for Telerik Test Studio at the Annual Automation Honors Voting!
Back to Top