DotNetKick.com is an open-source project. Please report any bugs and let us know your great suggestions. Currently running svn revision 620 (rss)

Kick Spy!, Kick Zeitgeist and Kick Widgets

15
kicks
published 3 months, 25 days ago, submitted by simplicityiskey 3 months, 29 days ago

keepitsimpleprojects.com — 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.

Add a comment 19 comments | category: | Views: 34 | Get KickIt image code
tags: | tag it

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:
Why not HtmlTextWriter?
posted by aquinas 3 months, 25 days ago
There is more overhead associated with HTMTextWriter
posted by simplicityiskey 3 months, 25 days ago
You do, however, need to use HTMLTextWriter when rendering asp.net server controls as html.
posted by simplicityiskey 3 months, 25 days ago
More overhead maybe, but there has to be something said about using standard built in framework classes over rolling your own ones.
posted by bzbetty 3 months, 25 days ago
Good point. I would agree, probably the way to go with limited usage.
posted by simplicityiskey 3 months, 25 days ago
Also fails to encode attributes so could be opening up your site to HTML injection vulnerabilities.

[)amien
posted by DamienG DamienG 3 months, 24 days ago
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.
posted by simplicityiskey 3 months, 24 days ago
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.
posted by simplicityiskey 3 months, 24 days ago
I think you're all wrong. So there.
posted by yesthatmcgurk yesthatmcgurk 3 months, 24 days ago
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.
posted by aquinas 3 months, 24 days ago
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.
posted by simplicityiskey 3 months, 24 days ago
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.
posted by simplicityiskey 3 months, 24 days ago
Meh. It got published. That's more than I can say for many of my blog posts.
posted by powerrush powerrush 3 months, 24 days ago
I've only had a few published but seemed to have taken a sever beating on this one.
posted by simplicityiskey 3 months, 24 days ago
severe
posted by simplicityiskey 3 months, 24 days ago
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.
posted by aquinas 3 months, 24 days ago
[comment removed]
posted by jbizkit 3 months, 23 days ago
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.
posted by simplicityiskey 3 months, 23 days ago
I am a big fan of helper classes. I don't know about this one, really, but the idea is good.
posted by samdnp 3 months, 20 days ago



information Login or create an account to comment on this story
 

Sponsored Link: www.carlist.ie

Search:

Ads via The Lounge