locked
How do I select in a drop down in C# web browser? RRS feed

  • Question

  • My code > webBrowser1.Document.GetElementById("MonthDropdown").SetAttribute("value", "Jan");

    Doesn't work, selects, but doesn't count it as changed.

    Website code :

    <select class="input-field rbx-select ng-valid ng-dirty ng-valid-parse ng-valid-birthday ng-not-empty ng-touched" id="MonthDropdown" tabindex="1" rbx-valid-birthday="" rbx-form-interaction="" rbx-form-validation="" name="birthdayMonth" ng-model="signup.birthdayMonth" ng-change="onChange()">
                                    <option value="" disabled="" selected="selected">Month</option>
                                            <option value="Jan">January</option>
                                            <option value="Feb">February</option>
                                            <option value="Mar">March</option>
                                            <option value="Apr">April</option>
                                            <option value="May">May</option>
                                            <option value="Jun">June</option>
                                            <option value="Jul">July</option>
                                            <option value="Aug">August</option>
                                            <option value="Sep">September</option>
                                            <option value="Oct">October</option>
                                            <option value="Nov">November</option>
                                            <option value="Dec">December</option>
    
                                </select>

    • Moved by KareninstructorMVP Thursday, July 26, 2018 12:03 AM Moved from vb.net forum
    • Moved by CoolDadTx Thursday, July 26, 2018 2:14 PM Winforms related
    Wednesday, July 25, 2018 7:28 PM

All replies

  • Have you tried to set the value, then invoke the function directly: WebBrowser1.Document.InvokeScript("onChange")?


    Wednesday, July 25, 2018 8:45 PM
  • No, I have not. How would I do that exactly?
    • Edited by Tr0 Wednesday, July 25, 2018 10:14 PM
    Wednesday, July 25, 2018 10:13 PM
  • I don't know what you are trying to do. I don't know if you need to change the value for Jan or if you need to add Jan. I suspect that one problem is that you are setting the "value"  attribute for the select statement but probably you need to add or replace the "option" tag.


    Sam Hobbs
    SimpleSamples.Info

    Thursday, July 26, 2018 2:56 PM
  • Hi,

    If it is in Winform, then try this:

            private void Form1_Load(object sender, EventArgs e)
            {
                webBrowser1.DocumentText = "<select  class='input-field rbx-select ng-valid ng-dirty ng-valid-parse ng-valid-birthday ng-not-empty ng-touched' id='MonthDropdown' value = '123' tabindex='1' rbx-valid-birthday='' rbx-form-interaction='' rbx-form-validation='' name='birthdayMonth' ng-model='signup.birthdayMonth' ng-change='onChange()'  style='width: 100px; '>"
                      + "<option value = '' disabled = '' selected = 'selected' > Month </option>"
                      + "<option value = 'Jan' > January </option> "
                      + "<option value = 'Feb' > February </option>"
                      + "<option value = 'Mar' > March </option>"
                      + "<option value = 'Apr' > April </option>"
                      + "<option value = 'May' > May </option>"
                      + "<option value = 'Jun' > June </option>"
                      + "<option value = 'Jul' > July </option>"
                      + "<option value = 'Aug' > August </option>"
                      + "<option value = 'Sep' > September </option>"
                      + "<option value = 'Oct' > October </option>"
                      + "<option value = 'Nov' > November </option>"
                      + "<option value = 'Dec' > December </option "
                      + "</select> ";
            }
    
            //get value
            private void button1_Click(object sender, EventArgs e)
            {
                textBox1.Text = webBrowser1.Document.GetElementsByTagName("option")[0].GetAttribute("value");
            }
    
            //set value
            private void button2_Click(object sender, EventArgs e)
            {
                webBrowser1.Document.GetElementsByTagName("option")[0].SetAttribute("value","Jan");            
            }

    Regards,

    Stanly


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Saturday, July 28, 2018 3:10 AM
  • Hi,

    Have you solved this problem now?

    I think the above reply can provide you with a solution, have you tried it?

    If so, hope you can close this thread by marking the reply as answer as this will help others looking for the same or similar issues down the road.

    Best Regards,

    Stanly


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, August 2, 2018 2:41 AM