I know this blog discusses a very simple problem but this simple problem is being confused by many and there are lots of suggestion floating in the net for the solution, which needless to say is an exaggerated solution to a simple problem. This blog is purely meant to clear those myths, else I wouldn’t have blogged on such a simple issue in so length.
Many of us would have come across a scenario where we needed to call a server side asp.net function and retrieve a value, from within javascript/HTML. I have come across this scenario many a times. But, recently, while going through some forums I found many people giving suggestions to make use of ScriptManager, ASP.NET Ajax controls and other stuffs. ScripManager, ASP.NET Ajax controls etc are purely meant for enabling AJAX/Partial page loading concept. But the real problem here is to just call a server side function or access a server variable and use it in javascript or html.
I would say, without using all these high funda stuffs we can solve this problem pretty easily using inline server codes. Though I am not a very big fan of this type of coding but there is no escaping from it, I had to use inline server code in many places to embed some server side variable’ value or the return value of some function in javascript or html tags. The syntax used for doing so is <% Server side code goes here %>. One can write any server side code/logic either in C# or VB in between “<% %>”, but in this blog I will concentrate on retrieving output of a server side function as there are lots of myths associated with such a simple thing.
I am a big fan of localization and so I use resource files (wherever possible) or XML file to implement localization. In many situations while writing javascript I had to show the localized strings retrieved from the server. I have used the simple code shown below to retrieve data from the server.
<%@ Page Language="C#" MasterPageFile="~/MasterPages.Master" AutoEventWireup="true" CodeBehind="MyProjectHome.aspx.cs" Inherits="Home_TDI" Title="Home Page" %> <script language="javascript" type="text/javascript"> //Function which handles the AJAX response.!!! Sandeep function showText(textToBeDisplayed) |
In the above code one can see that I am making use of the below line.
“<% =ResourceManagerUtility.GetString(Constants.[CONSTANT_NAME]) %>” |
What the above code does is it invokes the static “GetString” method defined in “ResourceManagerUtility” class and retrieves the corresponding string attached to the constant without making use of the so called ScriptManager or ASP.NET AJAX or any other stuffs. The code for ResourceManagerUtility and Constants classes is shown below.
namespace TDI_Tool_Prototype.Utilities |
“GetString” method creates an instance of “ResourceManager” class and returns the value based on the key passed in the “theKey” argument of the function.
The “Constants” class structure is shown below.
public class Constants |
The above class holds only the constants. These constants are passed to the “GetString” method and based on these constants the localized strings are retrieved.
In many blogs I have seen people saying one can access only methods in a page’ code behind file, I would like to say it’s not true. One can very well access any method inside any class, namespace or dll by adding a reference to the namespace. In the above code I am accessing “ResourceManagerUtility” class from another namespace called “Utilities” by importing it in the aspx page using the below code.
<%@ Import Namespace="Utilities"%> |
The above code imports the Utilities namespace. The code is similar to the below C# code.
using Utilities; |
Once you have imported the namespace using the @Import attribute you can use a syntax something like this.
<% =ClassName.StaticMethod([Arguments]) %> |
So in the above examples I have used the above syntax in javascript where I am retrieving a text from the resource file and passing it to a javascript function called “showText”. Also I have made use of the above syntax in javascript’ alert message as well. The code snippets are pasted below.
// Passing the localised string retrieved from the server to a javascript function. // Passing the localised string retrieved from the server to a javascript alert box. |
Also the same syntax is used in between html tags and for assigning the text value of a label control. The code is pasted below.
<!--Writing the localised text in a table' column--> |
One could see from the above code snippet that how easy is to access server functions in javascript, html tags and also in server control tags.
Possible mistake while using the above syntax can be as follows.
<% ResourceManagerUtility.GetString(Constants.OPEN_CONNECTION) %> |
The above statement can throw the below pasted error if used in javascript or in html or with server controls.
Compilation ErrorDescription: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately. |
<% ResourceManagerUtility.GetString(Constants.OPEN_CONNECTION); %> |
The above code is perfectly correct if it is placed in between html tags or used along with server control tags. The modified code without the “=” sign is shown below.
<!--Writing the localised text in a table' column--> |
But the same code without the “=” will not work if used in javascript. The code using the “alert” statement is modified (without “=”) and pasted below.
alert('<% ResourceManagerUtility.GetString(Constants. |
The above code will invoke the method and get the value but the alert message will be a blank one. The reason is the absence of the “=” sign. The “=” sign means an assignment, as the “=” sign is missing; the value is not getting assigned. So if you are trying to get some value from the server side in javascript then one has make use of the code with ‘=” sign and without the semicolon.
alert('<% =ResourceManagerUtility.GetString(Constants. SCREWED_UP_PENDING_REVIEW) %>') |
Compilation ErrorDescription: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately. |
The error prone code is shown below.
// Error prone javascript code having both = and ;. <!--Error prone HTML code with = and ; used together--> |
To avoid the error, either use “=” without “;” with inline code in javascript and “;” without “=” with inline code in html. To be on the safer side one can safely use only “=” sign along with inline code in both javascript and html tags as shown below.
// Perfect and safest way to use the same technique <!--The same way as it is used in the javascript--> |
I would recommend the above approach that works with javascript, html tags as well as with server control tags. Just for the sake of knowledge you can keep yourself aware about the other ways as well, but while implementing please follow the common methodology which will help you to avoid hell lot of problems.
Also in some blog I have read someone saying that only static methods can be accessed, this is not at all true. One can very well access normal methods of a class by creating an instance of the class and then accessing them in inline code as shown below.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Dummy.aspx.cs" Inherits="MyProject.Dummy" %> <html xmlns="http://www.w3.org/1999/xhtml" > |
One can notice a line something like this.
<% =dummyString%> |
What I am doing in the above line of code is accessing a server side variable and displaying the value. Below is the code behind file where the variable is declared.
public partial class DisplayFile : System.Web.UI.Page |
One point to note here is that one can only access protected or public variables. If one tries to access private variables in inline code the below pasted error will crop up.
Compilation ErrorDescription: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately. |
The same applies to methods also. One cannot access private code behind methods in inline code. The only restriction which applies in the case of inline code is one cannot access private members. Static, protected and public members can very well be accessed.
The only reason which I find why many are saying that only static methods can be used in inline codes is that one need not create a member variable to access the return value of a function. Other than that I don’t see any logic in saying this. One can very well access any protected, public and static members (variables, methods) of a code behind file or a class.
Hope the above e.g. clears some of the myths associated with accessing server side methods, variables and classes.
Very good post. Thanks.
ReplyDeleteThanks for the positive comments.
ReplyDeleteVery helpful post.
ReplyDeleteNice one...
ReplyDeleteThanks!
Thats great. How can we use a constant from constants.vb in the user control? I am trying it just like this and as soon as I put ResourceManagerUtility.GetString alert to this (inside server side symbols)(Constants.DESCRIPTION_LENGTH_LONG),the user control itself becomes unrecognizable and the application shows many build errors.
ReplyDeleteHi,
ReplyDeleteIt will be great if you could paste some code so that I can better understand your problem. Or you can send me the code to sndppr@gmail.com. From what you have explained I am not sure what is the problem.
Hi Sandeep,
ReplyDeleteThis is a great post... But simplicity always wins... You have used too many code to explain the concept... when I read this post its bit hard to follow the code as it is too verbose...
Thanks for the suggestion. Will incorporate the same in future posts.
ReplyDeleteSuch a great done by you in this article. Your article information is very helpful and useful for us. Keep it up. Now it's time to avail Baby Liquid Soap for more information.
ReplyDelete