Do you use resource file for storing your static script?
Rules to Better .NET Projects|b453a4db-2021-4c21-82cc-db69f0e180bb
v3.0
Posted at
20/10/2010 7:56 PM by
System Account
Rule Intro
Write a Intro pragraph here
Page Content
-
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
- Figure: Add a recourse file into your project in VS2005
- Figure: Read value from the new added resource file
{826A38D4-86E2-4E68-9809-643454ED2F48}
Do you feel this rule needs an update?