A few points to consider while working with literal string in ASP.NET

This post is for ASP.NET developers, working around with string literals in their applications for displaying run-time or dynamic data to their users. In the past few days, there have been quite a lot of new discussions about efficient way of creating a personalized string to represent the data on the websites.

A few tips that I had in my minds would be possible to share here with other developers of ASP.NET web applications. Note that, I have always been saying ASP.NET is built right on top of .NET framework, so anything that can be developed and run over .NET framework, can be used with ASP.NET too in the back-end. .NET exposes a few functions of the class System.String that are good for use with strings and modification and personalization of the strings.

Response messages

Usually, developers build a few messages to be shown to the clients as a result to their request. You can somehow, write a message (or return a message; possible way of usage in the ajax applications) in the Reponse.Write() function. You can write the string to return the data in the format of,

Response.Write("Thank you for your message " + name + " we will get back to you relating to your " + problem + " and we will have our " + client_name + " working on it right away to give you response in " + max_days);

Above code would work well, show the correct result, and at the very same time would be a good way to show your laziness too. No one uses string concatenation now-a-days. There is a very useful extension in .NET for String class. That is the .Format function. It is overloaded. Now let us consider writing the above code in a String.Format way,

Response.Write(
     String.Format("Thank you for your message {0} we will get back to you relating to your {1} and we will have our {2} working on it right away to give you response in {3}"), name, problem, client_name, max_days);

The second way of doing the same is more efficient. You,

  1. Write the message to print in a very literal (constant) way, with just embedding the parameters in the form a {index}.
  2. You pass the parameters to add to the string as a second, third, fourth and so on number of parameter to the function.

Now once you’re about to write the message in a new way; such as updating the message. You won’t have to worry about the concatenation problems, parser errors; that get raised a lot in ASP.NET applications. You can simply write the message in a string form. Embed the parameters. Pass the values via variables and .NET would take care of the rest.

HTML attributes

Another common usage of building strings is to create the HTML markup of the web applications, on the run-time and then returning the response to the client to be rendered.

Such scenarios, require your to build the HTML markup, in a well formatted way and then sent down to client. For example, this question on CodeProject provides a similar scenario for us to investigate a possible solution to such problems. That is a problem, which gets raised due to a similar problem of string concatenation in HTML elements and attributes.

You should always write the HTML attributes in a very similar way, to avoid concatenation, and making a use of variables and the String.Format() function. An example code can be the following one (the code from the question).

Response.Write("<script language=javascript> 
                    alert('"gk_szEnter_Value_Error_Message"'); 
              </script>");

The above code has a problem of attribute, well can be handled, but another major problem is the concatenation of the string by a variable. Which required him to add this value using a + operator but didn’t so caused a problem.

This could be possibly minimized by using the format function, in the following way,

Response.Write(String.Format("<script>alert('{0}');</script>"),    
                              gk_szEnter_Value_Error_Message);

Now the above string would be created as it is, but the parameter would be filled by the value passed. This now avoids any error of concatenation-type and so. Secondly, for HTML elements like image element, you can create that element in a way,

// Notice the single quote in the strings; whichw ould be converted to double.
String.Format("<img src='{0}' alt='{1}' />", image_source, alternate_text);
// which would yield
"<img src="~/value/of_image_source.png" alt="value of alternate_text" />"

These methods allow us to efficiently create strings. This code lets .NET framework handle the rest of the problems.

Advertisements

4 thoughts on “A few points to consider while working with literal string in ASP.NET

  1. mattps

    You shouldn’t write HTML in code behind. It creates technical debt and aggravates maintainability. There are certain cases (e.g. helper extension methods in MVC) when it is vital to write HTML directly by a library method, however, in such cases it would be better to use TagBuilder class.

    Like

    Reply
    1. Afzaal Ahmad Zeeshan Post author

      That is a valid point that you’ve made and probably this can be published on my post. But I am not going to update the post, because this was not about efficient way of writing strings in HTML, instead it was about concatenation… How to ignore it and so. Hopefully you will understand my point. 🙂

      Like

      Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s