Wrox Programmer Forums
Go Back   Wrox Programmer Forums > .NET > Other .NET > General .NET
|
General .NET For general discussion of MICROSOFT .NET topics that don't fall within any of the other .NET forum subcategories or .NET language forums.  If your question is specific to a language (C# or Visual Basic) or type of application (Windows Forms or ASP.Net) try an applicable forum category. ** PLEASE BE SPECIFIC WITH YOUR QUESTION ** When posting here, provide details regarding the Microsoft .NET language you are using and/or what type of application (Windows/Web Forms, etc) you are working in, if applicable to the question. This will help others answer the question without having to ask.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the General .NET section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 27th, 2007, 01:13 AM
Registered User
 
Join Date: Oct 2007
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default How do I get the value attribute of a radio button

I am using WebBrowser component in .NET to load a web page.
How do I get the value attribute of a radio button?

For example:
The HTML source code which let the user choose a color:
<input type="text" name="firstname" value="the name entered by user">
<input type="radio" name="myradio" value="red">Red
<input type="radio" name="myradio" value="blue" checked>Blue
<input type="radio" name="myradio" value="white">White

In C# code (C# Application), I wanted to get the value selected by the user.
I have the below code for getting their values:

========================
HtmlDocument document = this.webBrowser1.Document;
firstName = document.All["firstname"].GetAttribute("value");
color = document.All["myradio"].GetAttribute("value");
========================

I can get the correct value from the firstname textbox, however, the "color" radio button always returns the value "red" event when Red is not selected.

Please help!

nada
 
Old October 27th, 2007, 10:05 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

I believe the problem is the way that radio buttons behave in the document object model. You need to look at the selected index of the radio button object. Then you look for the value of the selected item. In JavaScript you'd do this, perhaps it will apply to your situation as well with some alteration:

color = myradio[myradio.selectedIndex].value;

-Peter
 
Old October 28th, 2007, 04:41 AM
Registered User
 
Join Date: Oct 2007
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks planoie. Yeah, it would have been easier with Javascript, however, I have no control over the other website.

I have come up with a working solution below:
If there's a simpler one, i'd be happy to use it :)


HtmlDocument document = this.webBrowser1.Document;
HtmlElementCollection input = document.All.GetElementsByTagName("input");
foreach (HtmlElement element in input)
{
   if (element.GetAttribute("type").ToLower() == "radio")
   {
      if (element.Name == "myradio" && element.GetAttribute("checked").ToLower() == "true")
         color = element.Value;
   }
}
 
Old October 29th, 2007, 03:09 AM
Registered User
 
Join Date: Oct 2007
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to balakrishnan
Default

Hello sir,
       This is balakrishna, i am very much thankful to u for mailing my problem, i hope so it will be continued.

one more may i get get the free downloadable tutorial on webservices & complete C#.Net& ASP.Net






Similar Threads
Thread Thread Starter Forum Replies Last Post
Radio button gaurisharya C# 2005 2 March 16th, 2008 06:43 AM
radio-button grstad HTML Code Clinic 3 February 28th, 2006 08:32 AM
Radio Button hoailing22 ASP.NET 1.0 and 1.1 Basics 1 June 2nd, 2005 12:08 AM
Button acts depending on radio button values janise Access 4 March 10th, 2004 12:53 AM
radio button damnnono_86 Access 5 October 28th, 2003 02:17 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.