Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# VB HTML
Hi,
How to get "class" attribute of an HTML element.
For example, for this HTML code, this C# code does well:
hElement.GetAttribute("id")  // Results "target_validate"

But it does not work for "class" attribute:
hElement.GetAttribute("class")  // Results ""
 
I use C# WebBrowser control.
 
<table id="target_validate" class="res_target" cellpadding="1" cellspacing="1">
	<tr>
		<td class="vil" colspan="2">AVATAR (18|60)</td>
	</tr>
	<tr>
		<th>User:</th>
		<td><a href="spieler.php?uid=4018">Iman</a></td>
	<tr>
		<th>Time:</th>
		<td>0:31:49</td>
	</tr>
</table>
Posted 13-Apr-10 11:26am
Edited 13-Apr-10 11:27am
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

user "className' instead of "class"
 
like GetAttribute("className")
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Hey,
 
I have no idea why, and doesnt make sense with what is actually in the html, but instead of htmlelement.GetAttribute("class"), use htmlelement.GetAttribute("classname"). I have it implemented as such in my app and works perfect.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

the following code will return "res_target"
 

alert(document.getElementById('target_validate').className);
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Guess you are using some C# methods to access the HTML DOM. Would be useful to know which library you are using (is that built into .Net?). Anyway, I imagine this explains why it isn't working:

http://www.webmasterworld.com/javascript/3090396.htm

Basically, you should use "obj.className" rather than "obj.getattribute('className')". Not sure if there is a corresponding function on whatever class "hElement" is. If not, then perhaps you can call a JavaScript function that just returns the result of "obj.className". You'd embed that JavaScript function in the HTML page you are calling, then call that JavaScript function from C# (again, I'd have to know what library you are using).
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

hello
how to access to "display" style type (hide, block, ...) in webbrowser?
tnx
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 410
1 Maciej Los 280
2 OriginalGriff 248
3 CHill60 186
4 Sinisa Hajnal 145


Advertise | Privacy | Mobile
Web03 | 2.8.141022.2 | Last Updated 16 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100