Wednesday, February 20, 2013

Replaces invalid XML characters in a string with their valid XML equivalent


Sometimes when creating XML documents with user specific data, string values of the XML nodes have to be encoded to maintain the structure of the XML nodes. Following table shows invalid XML characters and their escaped equivalent.

Invalid XML Character
Escaped equivalent
“<”
"<"
“>”
">"
“\””
"""
“\’”
"'"
“&”
“&"


There is a build-in method in .NET to achieve this called SecurityElement.Escape under the system. Security namespace.  This method accept string parameter and returns a string with invalid characters replaced.

e.g :-
      string xml = "\"node\"&"; 
      string encodedXml = System.Security.SecurityElement.Escape(xml);
      //RESULT: <node>"node"&<node>


Tuesday, February 19, 2013

How to encode JavaScript string to escape characters such as single quotes in .NET


When registering JavaScript functions or calling JavaScript functions from code behind there might me some specials characters such as single quotes, new line characters. These kinds of characters will causes for errors such as ” Unexpected identifier” , “Unterminated String constant” or “Expected ')'” type errors. To avoid these errors string encoding has to be done.

In .Net framework 4 a new method has been introduced to cater these types of errors under the System.Web.HttpUtility namespace called HttpUtility.JavaScriptStringEncode. This method does the encoding and injects necessary escape characters.

As an example, to show an alert which includes single quotes we have to use the encoding.


  string message = "The value 'x' is not allowed";

  ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "Error", string.Format("alert('{0}');", message), true); // This will give an error

  ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "Error", string.Format("alert('{0}');", HttpUtility.JavaScriptStringEncode(message)), true); // JavaScript encoding has been done.


Monday, February 18, 2013

ASP.Net client side validation using Page_ClientValidate


ASP.Net client side validation using Page_ClientValidate
In typical web forms validators such as required field validators, regular expression validators, etc. are using with regular asp.net controls. With validation groups part of the controls in the web form can be validate. But with some of client events such as ‘OnClientClick’ those validations may not work as intended.  In such scenarios some mechanism has to use to check the validity of the validators to avoid unnecessary post backs. For achieve this java script Page_ClientValidate() function can be used.\\
 
Page_ClientValidate() function returns true or false based on the validity of the validators in the web form.


e.g :-
   if(retValue != null && Page_ClientValidate()){  //retValue is some variable
      return true;
   }
   else{
     return false;
   }
</ code>

Page_ClientValidate function can be used to validate controls which are belong to a validation group only.


e.g :-
   if(retValue != null && Page_ClientValidate('theGroup')){  //thGroup is a validation group
      return true;
   }
   else{
     return false;
   }