How is it possible to get the number/length of html elements with the same class names from ASP.NET code behind.
Asked
Active
Viewed 806 times
0
-
Numer/length of _which_ html file? A different or the one you are going to generate in this lifecycle? If the former, use HtmlAgilityPack, if the latter, i have no idea since it is not predictable what ASP.NET will render on the client's browser. – Tim Schmelter Jul 05 '13 at 08:44
-
aspx file associated or corresponding to the code behind file. – Sam Jul 05 '13 at 08:45
4 Answers
2
IEnumerable<Control> FindRecursive( Control c, Func<Control,bool> predicate )
{
if( predicate( c ) )
yield return c;
foreach( var child in c.Controls )
{
if( predicate( c ) )
yield return c;
}
foreach( var child in c.Controls )
foreach( var match in FindRecursive( c, predicate ) )
yield return match;
}
//Use this forloop get controls
foreach( WebControl c in FindRecursive( Page, c => (c is WebControl) &&
((WebControl)c).CssClass == "test" ) )
{
//Code
}

Community
- 1
- 1

Aditya Singh
- 9,512
- 5
- 32
- 55
-
OP wants to analyze the html elements not the server controls (or at least not only). – Tim Schmelter Jul 05 '13 at 09:28
2
It's difficult to analyze the HTML which is rendered by ASP.NET since that depends on factors like the browser of the client.
However, you can try following approach which uses control.RenderControl
to render the HTML and HtmlAgilityPack
to analyze it:
protected void Page_PreRender(Object sender, EventArgs e)
{
string thisHtml = RenderControl(this.Form);
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(thisHtml);
var nodeColl = doc.DocumentNode.SelectNodes("//*[contains(@class,'fooClass')]");
Console.WriteLine("Count: " + nodeColl.Count);
// here a linq approach with the same result:
var nodes = doc.DocumentNode.Descendants()
.Where(d => d.Attributes.Contains("class") && d.Attributes["class"].Value=="fooClass");
Console.WriteLine("Count: " + nodes.Count());
}
private string RenderControl(Control control)
{
StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);
HtmlTextWriter writer = new HtmlTextWriter(sw);
control.RenderControl(writer);
return sb.ToString();
}
I have used a simple test page with some some controls where some of them have a class/CssClass = fooClass
. The result seemed to be correct.

Tim Schmelter
- 450,073
- 74
- 686
- 939
0
You can only get html elements that marked as runat="server" from server side. Just a suggestion: get the html elements that have same class from client side via jquery selectors and pass them to the server via ajax.

tolga güler
- 308
- 1
- 7
0
Try to override Render method:
protected override void Render(HtmlTextWriter writer)
{
StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);
HtmlTextWriter htw = new HtmlTextWriter(sw);
base.Render(htw);
string s = sb.ToString();
//here you are able to use HTMLAgilityPack to parse HTML
writer.Write(s);
}
You will retrieve HTML as string s and will be able to parse it

Anton
- 9,682
- 11
- 38
- 68