473,394 Members | 1,709 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Getting the selected text of WebBrowser control dropdown box.

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.
May 21 '07 #1
11 20252

"Frank Rizzo" <no**@none.comwrote in message
news:uX**************@TK2MSFTNGP06.phx.gbl...
>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").V alue;

May 21 '07 #2
Mr. Arnold wrote:
>
"Frank Rizzo" <no**@none.comwrote in message
news:uX**************@TK2MSFTNGP06.phx.gbl...
>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").V alue;
There is no .Value on the return of the .GetElementById method.
May 22 '07 #3

"Frank Rizzo" <no**@none.comwrote in message
news:O1**************@TK2MSFTNGP04.phx.gbl...
Mr. Arnold wrote:
>>
"Frank Rizzo" <no**@none.comwrote in message
news:uX**************@TK2MSFTNGP06.phx.gbl...
>>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;

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.


May 22 '07 #4
Mr. Arnold wrote:
>
"Frank Rizzo" <no**@none.comwrote in message
news:O1**************@TK2MSFTNGP04.phx.gbl...
>Mr. Arnold wrote:
>>>
"Frank Rizzo" <no**@none.comwrote in message
news:uX**************@TK2MSFTNGP06.phx.gbl...
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;

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.
It's a winforms application and a WinForms WebBrowser control.
May 22 '07 #5

"Frank Rizzo" <no**@none.comwrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
Mr. Arnold wrote:
>>
"Frank Rizzo" <no**@none.comwrote in message
news:O1**************@TK2MSFTNGP04.phx.gbl...
>>Mr. Arnold wrote:

"Frank Rizzo" <no**@none.comwrote in message
news:uX**************@TK2MSFTNGP06.phx.gbl...
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;

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.

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.


May 22 '07 #6
>
It's a winforms application and a WinForms WebBrowser control.
Don't hold me to it, but the link may hold your solution by using the
InnerText to get the Value.

http://www.codeproject.com/aspnet/Prevent_attacks.asp

May 22 '07 #7
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 (wa****@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.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/subscripti...t/default.aspx.
==================================================

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

May 22 '07 #8
Walter Wang [MSFT] wrote:
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
>

Hope this helps.
Sincerely,
Walter Wang (wa****@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.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/subscripti...t/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
May 22 '07 #9
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 (wa****@online.microsoft.com, 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.

May 23 '07 #10
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 (wa****@online.microsoft.com, 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.

May 28 '07 #11
Walter Wang [MSFT] wrote:
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
May 30 '07 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Kenneth | last post by:
Hi, I am implementing a VB program using webbrowser control. Whenever a user makes a selection on the webbrowser control, I will have to identify the location where this selected text resides in...
5
by: Hassan Bassam | last post by:
I am having a problem. How can I get the selected text from a text editor. I am using ASP.NET and VB as language. Thanx
0
by: tshad | last post by:
How do you get the selected text of a dropdown box? Selectedvalue will get you the value and selectedindex will get you the position, but how do you get the text (it isn't selectedText). ...
1
by: Mike Evans | last post by:
In a windows application we have a webbrowser control and we want to change the selected item of a select or combobox inside the webbrowser control. We know how to change the value of a textbox...
4
by: afrinspray | last post by:
Is there a way to get the selected text inside a div, or other element? Mike
4
by: Steve Richter | last post by:
I would like to build an HTML stream as a string and have the WebBrowser control render that HTML. Then on PostBack, or whatever it is called, I would like my code to be the one that receives what...
6
by: yasodhai | last post by:
Hi, I used a dropdown control which is binded to a datagrid control. I passed the values to the dropdownlist from the database using a function as follows in the aspx itself. <asp:DropDownList...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.