Introduction
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 an efficient way of creating a personalized string
to represent the data on the websites.
Here, it would be possible to share a few tips that I had in my mind 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 string
s and modification and personalization of the string
s.
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);
The 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:
- Write the message to print in a very literal (constant) way, with just embedding the parameters in the form a {index}.
- 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 string
s 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 you 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 cause 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:
String.Format("<img src='{0}' alt='{1}' />", image_source, alternate_text);
"<img src="~/value/of_image_source.png" alt="value of alternate_text" />"
These methods allow us to efficiently create string
s. This code lets .NET Framework handle the rest of the problems.
History
- 28th February, 2015: Initial version