Click here to Skip to main content
15,885,365 members
Articles / Web Development / HTML
Tip/Trick

A Few Points to Consider While Working with Literal String in ASP.NET

Rate me:
Please Sign up or sign in to vote.
4.67/5 (5 votes)
28 Feb 2015CPOL3 min read 10.6K   4  
This tip is to provide an overview of writing strings in ASP.NET web applications to avoid concatenations.

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 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:

C#
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:

C#
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 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):

C#
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:

C#
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:

C#
// Notice the single quote in the strings; which would 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.

History

  • 28th February, 2015: Initial version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Pakistan Pakistan
Afzaal Ahmad Zeeshan is a computer programmer from Rabwah, Pakistan, currently living in The Netherlands, likes .NET Core and Node.js for regular everyday development. Afzaal Ahmad works at Adyen as a Developer Advocate.

He is an expert with Cloud, Mobile, and API development. Afzaal has experience with the Azure platform and likes to build cross-platform libraries/software with .NET Core. Afzaal is an Alibaba Cloud MVP, twice he has been awarded Microsoft MVP status for his community leadership in software development, four times CodeProject MVP status for technical writing and mentoring, and 4 times C# Corner MVP status in the same field.

Comments and Discussions

 
-- There are no messages in this forum --