Getting the selected text of WebBrowser control dropdown box.

F

Frank Rizzo

I have a WebBrowser control on a .net 2.0 winforms app. I load up a
page and want to get the selected entry of a dropdown.

I can get the element:

HtmlElement stationList = browser.Document.GetElementById("dropdownFacID");

How do I know get the selected entry (the text, not the id).

Thanks.
 
M

Mr. Arnold

Frank Rizzo said:
I have a WebBrowser control on a .net 2.0 winforms app. I load up a page
and want to get the selected entry of a dropdown.

I can get the element:

HtmlElement stationList =
browser.Document.GetElementById("dropdownFacID");

How do I know get the selected entry (the text, not the id).
browser.Document.GetElementById("dropdownFacID").Value;
 
M

Mr. Arnold

Frank Rizzo said:
There is no .Value on the return of the .GetElementById method.


I don't know about this Net method, but it's making a call to JavaScript.

Below is JavaScript

var myTextField = document.getElementById('myText');
if(myTextField.value != "")
alert("You entered: " + myTextField.value)
else
alert("Would you please enter some text?")}You can do this kind of JavaScript statement.

if (document.getElementById('myText').value != "")

Maybe, you can post to a .NET Web solution specific NG to get more help.
 
F

Frank Rizzo

Mr. Arnold said:
I don't know about this Net method, but it's making a call to JavaScript.

Below is JavaScript

var myTextField = document.getElementById('myText');
if(myTextField.value != "")
alert("You entered: " + myTextField.value)
else
alert("Would you please enter some text?")}You can do this kind
of JavaScript statement.

if (document.getElementById('myText').value != "")

Maybe, you can post to a .NET Web solution specific NG to get more help.

It's a winforms application and a WinForms WebBrowser control.
 
M

Mr. Arnold

Frank Rizzo said:
It's a winforms application and a WinForms WebBrowser control.

It's still making a call to JavaScript I believe. That getElementById
command is a JavaScript command and not a .NET command.

Even though it's a Winform using a WebBrowser control, who .NET is going to
make the call to do it is ASPscript or JavaScript, which are scripting
languages that work with a Web browser.
 
W

Walter Wang [MSFT]

Hi Frank,

Please try this code:

HtmlElement element = webBrowser1.Document.GetElementById("Select1");
object objElement = element.DomElement;
object objSelectedIndex =
objElement.GetType().InvokeMember("selectedIndex",
BindingFlags.GetProperty, null, objElement, null);
int selectedIndex = (int) objSelectedIndex;
if (selectedIndex != -1)
{
MessageBox.Show(element.Children[selectedIndex].InnerText);
}


Hope this helps.


Sincerely,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications. If you are using Outlook Express, please make sure you clear the
check box "Tools/Options/Read: Get 300 headers at a time" to see your reply
promptly.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
F

Frank Rizzo

Walter said:
Hi Frank,

Please try this code:

HtmlElement element = webBrowser1.Document.GetElementById("Select1");
object objElement = element.DomElement;
object objSelectedIndex =
objElement.GetType().InvokeMember("selectedIndex",
BindingFlags.GetProperty, null, objElement, null);
int selectedIndex = (int) objSelectedIndex;
if (selectedIndex != -1)
{
MessageBox.Show(element.Children[selectedIndex].InnerText);
}

Walter,

Thanks for the code, but it didn't work out, because the dropdown has
OPTGROUPs ( http://www.w3schools.com/tags/tag_optgroup.asp ). This is
something I haven't seen up to now, but the code above just does not
work when an optgroup is present. The messagebox shows the contents of
the entire optgroup.

I actually resolved the problem as follows:

HtmlElement element = webBrowser1.Document.GetElementById("Select1")
string selectedValue = element.GetAttribute("value");
string elementSource = element.OuterHtml;

And then running regex
(?<=<OPTION value={0} selected>).*?(?=</OPTION>)
against the elementSource.

It's hacky but it works. If you have the code that works for an
optgroup, that would be great.

Regards
 
W

Walter Wang [MSFT]

Hi Frank,

Thanks for your update and sorry for my ignorance of the OPTGROUP element.

Please try following code, hopefully it should work for situations with or
without OPTGROUP:

HtmlElement element = webBrowser1.Document.GetElementById("Select1");
foreach(HtmlElement child in element.All)
{
if (child.GetAttribute("tagName") == "OPTION")
{
if (child.GetAttribute("selected") == "True")
{
MessageBox.Show(child.GetAttribute("text"));
break;
}
}
}


Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
W

Walter Wang [MSFT]

Hi Frank,

I'm wondering if you have tried the above suggestion. Anyway, please feel
free to let me know if there's anything unclear. Thanks.

Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
F

Frank Rizzo

Walter said:
Hi Frank,

I'm wondering if you have tried the above suggestion. Anyway, please feel
free to let me know if there's anything unclear. Thanks.

Walter, the code has shipped to customers using my Regex solution, but
I'll give your solution a try.

Regards
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top