none
Input value to combobox through a webbrowser

    Question

  • Hello All,

    I'm migrating myself from VB to C# to help my cause at my company.  I'm not great at C# but am doing decent given my circumstances.  I've came across an issue with an application I'm building that has me "stuck".  The goal of the application is automate problem logs for redundant, yet tracked, problems.  The problem log application is web based and uses mainly dropdown/combo boxes for data input.

    Here's a snippet of one dropdown:
    <select name='RC_SOURCE' id='RC_SOURCE' tabindex='565' size='1'  class='PSDROPDOWNLIST' style="width:210px; " >
    <option value=""></option>
    <option value="AUTOM">Automation</option>
    <option value="BYPAS">Bypass</option>
    <option value="CPF">CPF</option>
    <option value="ECO">ECO</option>
    <option value="EMAIL">Email</option>
    <option value="EXCHA">Exchange</option>
    </select>
    
    
    Here's my code that semi-works:
    HtmlElement el = webDoc.Window.Frames[2].Document.GetElementById("RC_SOURCE");
    HtmlElementCollection elCol = el.GetElementsByTagName("option");
    int X = 0;
    foreach (HtmlElement op in elCol)
    {
         if(op.InnerText == "Automation")
         {
              el.SetAttribute("value", el.Children[X].InnerText);
              break;
         }
         X++;
    }
    Basically what happens is the code does not populate the dropdown when X = 0,1, or 2.  However, anything like 3 or 4 or 15 and it populates it just fine (32 elements in the list).  I don't understand why that isn't working.  I'm assuming it's because what I have is glitchy.  I'm not doing this through an IDE.  Notepad with the XP built-in complier (v3.5) is the way I am doing this.  I want to use the X to reference the index to make the code re-usable and if the website changes, the code still works.

    If more info is needed, I'll add what I can.  This has just became frustrating the past few days and a command-line complier is just the icing for me.

    Thank you in advance for any/all help,
    Jason
    • Edited by JShelb Monday, May 04, 2009 5:11 PM Spacing
    Monday, May 04, 2009 5:07 PM

Answers

  • 1. Download C# Express Edition.  It's free.  No license needed.  No more excuses about not using the IDE.  :)

    2. You're setting the selectedIndex to "el.Children[X].InnerText", just set it to "X.ToString()".  The following code worked for me:

    HtmlElement el = webBrowser1.Document.GetElementById("RC_SOURCE");
    HtmlElementCollection elCol = el.GetElementsByTagName("option");
    int X = 0;
    foreach (HtmlElement op in elCol)
    {
        if (op.InnerText == "Automation")
        {
            el.SetAttribute("selectedIndex", X.ToString());
            break;
        }
        X++;
    }

    David Morton - http://blog.davemorton.net/
    • Marked as answer by JShelb Monday, May 04, 2009 5:56 PM
    Monday, May 04, 2009 5:45 PM
    Moderator

All replies

  • First, why are you not using the IDE?

    Second, have you tried setting the attribute "selectedIndex" instead of "value"? And have you tried setting it to the index represented in "X"?

    David Morton - http://blog.davemorton.net/
    Monday, May 04, 2009 5:18 PM
    Moderator
  • First, why are you not using the IDE?

    Second, have you tried setting the attribute "selectedIndex" instead of "value"? And have you tried setting it to the index represented in "X"?

    David Morton - http://blog.davemorton.net/

    Thanks David for the prompt reply.

    I'd love to be using the IDE.  However my current job position is not a programming position.  I'm teaching/learning scripting/coding during my spare time of my current job duties.  My department will not purchase the license for me.  I'm working on them though, haha.  Also, placing buttons/textboxes on a form through X,Y manipulation is killer!

    This:
         el.SetAttribute("selectedIndex", el.Children[X].InnerText);

    Returned:
         System.NotSupportedException: The attribute you specified is not supported on this element. at System.Windows.Forms.HtmlElement.SetAttribute(String
         attributeName, String value)


    "Setting it to the index represented in "X"?"

    Forgive me, but I'm not exactly following what you're asking.  If you mean removing "X" and directly inserting "1", yes I have and I get the same response.  If you mean using something to the affect of el.SelectedIndex = 1, I usually get a HtmlElement doesn't have a reference for SelectedIndex at compile time.

    Thank you again.
    Jason
    Monday, May 04, 2009 5:34 PM

  • I'd love to be using the IDE.  However my current job position is not a programming position.  I'm teaching/learning scripting/coding during my spare time of my current job duties.  My department will not purchase the license for me.  I'm working on them though, haha.  Also, placing buttons/textboxes on a form through X,Y manipulation is killer!

    Jason,

    You might want to consider using the free Visual Studio Express editions: http://www.microsoft.com/downloads/details.aspx?familyid=f3fbb04e-92c2-4701-b4ba-92e26e408569&displaylang=en
    Reed Copsey, Jr. - http://reedcopsey.com
    Monday, May 04, 2009 5:37 PM
    Moderator
  • 1. Download C# Express Edition.  It's free.  No license needed.  No more excuses about not using the IDE.  :)

    2. You're setting the selectedIndex to "el.Children[X].InnerText", just set it to "X.ToString()".  The following code worked for me:

    HtmlElement el = webBrowser1.Document.GetElementById("RC_SOURCE");
    HtmlElementCollection elCol = el.GetElementsByTagName("option");
    int X = 0;
    foreach (HtmlElement op in elCol)
    {
        if (op.InnerText == "Automation")
        {
            el.SetAttribute("selectedIndex", X.ToString());
            break;
        }
        X++;
    }

    David Morton - http://blog.davemorton.net/
    • Marked as answer by JShelb Monday, May 04, 2009 5:56 PM
    Monday, May 04, 2009 5:45 PM
    Moderator
  • Sounds like Express Web Developer might be more appropriate.
    Mark the best replies as answers. "Fooling computers since 1971."
    Monday, May 04, 2009 5:52 PM
    Moderator
  • 1. Download C# Express Edition.  It's free.  No license needed.  No more excuses about not using the IDE.  :)

    2. You're setting the selectedIndex to "el.Children[X].InnerText", just set it to "X.ToString()".  The following code worked for me:

    HtmlElement el = webBrowser1.Document.GetElementById("RC_SOURCE");
    HtmlElementCollection elCol = el.GetElementsByTagName("option");
    int X = 0;
    foreach (HtmlElement op in elCol)
    {
        if (op.InnerText == "Automation")
        {
            el.SetAttribute("selectedIndex", X.ToString());
            break;
        }
        X++;
    }

    David Morton - http://blog.davemorton.net/
    Reed/David
    I did not try the free version angle with them.  I'll run that past them and see what kind of response I receive.  I used the VB.Net version, I can't believe I didn't think of the C# version.

    David-
    This worked great:
         el.SetAttribute("selectedIndex", X.ToString());

    Thank you very much for the help and the prompt responses.

    Jason
    • Marked as answer by JShelb Monday, May 04, 2009 5:56 PM
    • Unmarked as answer by JShelb Monday, May 04, 2009 5:56 PM
    Monday, May 04, 2009 5:55 PM
  • The real problem (IMHO) is that you are not databinding using ASP.Net controls. For example take the drop down control

    <asp:DropDownList ID="ddlMain" runat="server"/>
    
    Then bind it in the codebehind and specify the value to be used when the user selects it

    int index = 0;
    List<string> display = new List<string>() { "Abc", "Def", "GHI" };
    
    var toTheScreen = from itm in display
                      select new
                      {
                          Value = index++,
                          Text = itm
                      };
    
    
    ddlMain.DataTextField = "Text";
    ddlMain.DataValueField = "Value";
    ddlMain.DataSource = toTheScreen;
    ddlMain.DataBind();

    I realize you may be working with some other person's code. But if it is 3.5..why deal with old ASP code? HTH GL


    William Wegerson (www.OmegaCoder.Com)
    Monday, May 04, 2009 6:05 PM
    Moderator