Rule #35
Do you use resource file for storing your static script?
  v3.0 Posted at 20/10/2010 7:56 PM by system
Write a Intro pragraph here

 

     StringBuilder sb = new StringBuilder();
sb.AppendLine(@"<script type=""text/javascript"">");
sb.AppendLine(@"function deleteOwnerRow(rowId)");
sb.AppendLine(@"{");
sb.AppendLine(string.Format(@"{0}.Delete({0}.
GetRowFromClientId(rowId));", OwnersGrid.ClientID));
sb.AppendLine(@"}");
sb.AppendLine(@"</script>");
Bad example - Hard to read ?the string is surrounded by rubbish + inefficient because you have an object and 6 strings

 

     string.Format(@"<script type=""text/javascript"">                  
function deleteOwnerRow(rowId)
{ {0}.Delete({0}.GetRowFromClientId(rowId)); } </script> ",
OwnersGrid.ClientID);
Good example Slightly easier to read ?but it is 1 code statement across 10 lines
     string scriptTemplate = Resources.Scripts.DeleteJavascript;
string script = string.Format(scriptTemplate, OwnersGrid.ClientID);
     <script type=""text/javascript"">
function deleteOwnerRow(rowId)
{
{0}.Delete({0}.GetRowFromClientId(rowId));
}
</script>

Figure: The code in the first box, the string in the resource file in the 2nd box. This is the easiest to read + you can localize it eg. If you need to localize an Alert in the javascript

Create a Resource file
Figure: Add a recourse file into your project in VS2005
Create a Resource file
Figure: Read value from the new added resource file