Do you use resource file for storing your static script?

Last updated by Brook Jeynes [SSW] 8 months ago.See history

Resource files provide a structured and centralized approach to storing and retrieving static scripts, eliminating the need for scattered code snippets and enhancing your development workflow.

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

CreateResource small
Figure: Add a recourse file into your project in VS2005

ReadResource small
Figure: Read value from the new added resource file

We open source. Powered by GitHub