C# HTML Helper Class(keepitsimpleprojects.com)

submitted by simplicityiskeysimplicityiskey(650) 4 years, 2 months ago

This utility class comes in VERY handy when you have to generate HTML in code. Also a big help when using Ajax with .NET outside of AJAX.NET or Ajax enabled controls.

19 comments |category: |Views: 952

tags: another

new Add a live kick counter to your blog >> liveImage

You can even customize the image by choosing your own colors, and then clicking the button below to update the preview and the html code:

  • "Kick It" text
  • "Kick It" background
  • kick count text
  • kick count background
  • border

Simply copy and paste this HTML into your blog post.


Users who kicked this story:
Comments:

posted by aquinasaquinas(20) 4 years, 2 months ago 0

Why not HtmlTextWriter?

Reply

posted by simplicityiskeysimplicityiskey(650) 4 years, 2 months ago 0

There is more overhead associated with HTMTextWriter

Reply

posted by simplicityiskeysimplicityiskey(650) 4 years, 2 months ago 0

You do, however, need to use HTMLTextWriter when rendering asp.net server controls as html.

Reply

posted by bzbettybzbetty(0) 4 years, 2 months ago 0

More overhead maybe, but there has to be something said about using standard built in framework classes over rolling your own ones.

Reply

posted by simplicityiskeysimplicityiskey(650) 4 years, 2 months ago 0

Good point. I would agree, probably the way to go with limited usage.

Reply

posted by DamienGDamienG(1405) 4 years, 2 months ago 0

Also fails to encode attributes so could be opening up your site to HTML injection vulnerabilities.

[)amien

Reply

posted by simplicityiskeysimplicityiskey(650) 4 years, 2 months ago 0

I'm not sure what you mean by that. Are you saying that the HTML Helper class doesn't encode attributes? When using the HTMLTextWriter to render a control, you get the same output HTML. With or without attributes.

Reply

posted by simplicityiskeysimplicityiskey(650) 4 years, 2 months ago 0

After reading these comments I am realizing that it is the tendency of most developers to over-complicate almost everything. You should realize that this class is just a simple helper class to generate limited html. I am not suggesting that you generate all of your HTML with this class. Ther are always cases where an HTMLTextWriter would be a better solution. In terms of encoding, in my use of this class my Ajax class and my server side aspx page manage the encoding and un-encoding of the information going back and forth. This is not by any means and end all be all. Just a helper that might save a few folks some time. Remember KEEP IT SIMPLE. All of these points brought up should be addressed based on your individual use of HTML generation.

But they are all valid points. Don't get me wrong, I am not discounting anyones opinions here.

Reply

posted by yesthatmcgurkyesthatmcgurk(4063) 4 years, 2 months ago 0

I think you're all wrong. So there.

Reply

posted by aquinasaquinas(20) 4 years, 2 months ago 0

try this:

System.Diagnostics.Stopwatch stopw = new System.Diagnostics.Stopwatch();
stopw.Start();

for (int i = 0; i < 1000; i++) {
System.Text.StringBuilder sb = new StringBuilder();
List<ElementAttribute> attributes = new List<ElementAttribute>();
attributes.Clear();
attributes.Add(new ElementAttribute(HTMLHelper.ID, "gallery"));
//attributes.Add(new ElementAttribute(HTMLHelper.CLASS, "\" <hello>blah</hello>"));
attributes.Add(new ElementAttribute(HTMLHelper.CLASS, "whatever"));
attributes.Add(new ElementAttribute("onclick", "whatever()"));

for (int j = 0; j < 1000; j++) {
sb.Append(HTMLHelper.BeginDiv(attributes));
sb.Append("blah");
sb.Append(HTMLHelper.EndDiv());
}
}

stopw.Stop();
Console.WriteLine(stopw.ElapsedMilliseconds);


stopw.Reset();
stopw.Start();
for (int i = 0; i < 1000; i++) {
System.IO.StringWriter sw = new System.IO.StringWriter();
HtmlTextWriter writer = new HtmlTextWriter(sw);

for (int j = 0; j < 1000; j++) {
writer.AddAttribute(HtmlTextWriterAttribute.Id, "gallery");
//writer.AddAttribute(HtmlTextWriterAttribute.Class, "\" <hello>blah</hello>");
writer.AddAttribute(HtmlTextWriterAttribute.Class, "whatever");
writer.AddAttribute(HtmlTextWriterAttribute.Onclick, "whatever()");
writer.RenderBeginTag(HtmlTextWriterTag.Div);
writer.Write("blah");
writer.RenderEndTag();
//string tstring = sw.GetStringBuilder().ToString();
}
}

stopw.Stop();
Console.WriteLine(stopw.ElapsedMilliseconds);

Console.ReadLine();

On my machine the version that uses HtmlTextWriter is about 300 milliseconds FASTER. If you uncomment out the lines that are commented out, it's about 100 ms slower. The reason why it's slower is because it properly esacapes HTML entities, which the HTMLHelperClass does not. That's what [)amien was referring to when he said your version is open to HTML injection.

I'm not trying to rail against you. I just take exception with your comment of "After reading these comments I am realizing that it is the tendency of most developers to over-complicate almost everything." It just seems a bit odd that you're saying people over complicate things, when there is an existing class that does what your class does, but you've invented your own. AND you have to tell people that use it to be careful because you need to do your own html escaping.

Reply

posted by simplicityiskeysimplicityiskey(650) 4 years, 2 months ago 0

Man! You guys are ruthless. I have a simple class here that I use to generate some simple HTML. As I stated before - it is NOT the end all be all. What is the result of the HTMLTextWriter.Innerwriter.ToString() (which is what has to be called to get the resulting HTML) OMG it's a string! The same as the Helper output. You guys are wack. I am not trying to build the fastest most secure HTML generator on the face of the earth, this is just a class that I use occassionaly and thought most people here would be wise enough to take it with a grain of salt.

Reply

posted by simplicityiskeysimplicityiskey(650) 4 years, 2 months ago 0

I guess what everyone is not understanding is that there are few cases in some of my applications where I just need a quick peice of HTML generated and I would rather not have the overhead of generating an instance of an HTMLTextWriter and the StringWriter required for it's constructor. If I were building an application that generated large amounts of HTML on a consistent basis I would agree - ( As I stated above) that the HTMLTextWriter is the appropriate way to do this.

This is typical developer ego in action - try to trump the other developers. This attitude is what complicates software development in almost every shop. There is always more than one way to do something, and there is always the right and wrong way but all should be evaluated within the context of the application.

Reply

posted by powerrushpowerrush(3873) 4 years, 2 months ago 0

Meh. It got published. That's more than I can say for many of my blog posts.

Reply

posted by simplicityiskeysimplicityiskey(650) 4 years, 2 months ago 0

I've only had a few published but seemed to have taken a sever beating on this one.

Reply

posted by simplicityiskeysimplicityiskey(650) 4 years, 2 months ago 0

severe

Reply

posted by aquinasaquinas(20) 4 years, 2 months ago 0

Well, you stated that "There is more overhead associated with HTMTextWriter." That's why I included the timings, so that those who might wish to use your code were aware that their did not appear to be any significant overhead as far as time goes. If you meant some other overhead besides time, then sorry, my bad.

Reply

posted by simplicityiskeysimplicityiskey(650) 4 years, 2 months ago 0

I meant more overhead in terms of creating instances of objects that ultimately take up system resources. If I want to create a simple anchor tag, with the HTMLTextWriter, I am creating instances of two objects the HTMLTextWriter and the StringWriter which is required by the HTMLTextWriter constructor. With the helper class I simply call a static method that returns a string.

Don't get me wrong. I know there is a time in which the HTMLTextWriter should be used. This helper class is not designed to be used in those circumstances. Just a simple helper to quickly generate some HTML quickly.

Reply

posted by samdnpsamdnp(980) 4 years, 2 months ago 0

I am a big fan of helper classes. I don't know about this one, really, but the idea is good.

Reply

information Login or create an account to comment on this story