2

I am building my ASP server control which has some resources. One of them is .js file where i must pass a variable from my ServerControl.cs file.

I know how to for example get resource images in my resource style files and javascript files like this:

var resourceOpen  = "<%=WebResource("PatientList.Images.DirOpen.png")%>" ;
var resourceClose = "<%=WebResource("PatientList.Images.DirClose.png")%>";

background: url('<%=WebResource("PatientList.Images.letter-bg.png")%>');

I want to do the same with my public variables in my server control .cs file Let's say i've got

public string TestVariable = "It works"

How to pass it to javascript file in my resources?

var jsvariable = "<%=TestVariable%>"

Doesn't work. It seems that only WebResource(...) works. When i try to use variables, when I make something like this:

alert("<%=TestVariable%>");

The window with string "<%=TestVariable%>" will appear instead of "It works"

4
  • It should work... except for the obvious 'you're missing semicolons'. If you're using VS (as I am) intellisense shows variables. Commented Jun 22, 2012 at 11:36
  • It works but not in my embeded resources javascript files. Only when i have separate javascript and c# variable in my codebehind to my aspx file. But i want to make server control .dll file containing everything. There the problem occurs. It's strange because it works with WebResource(...) inside <%= %> but seems to fail with everything else. Commented Jun 22, 2012 at 11:46
  • Ahh I see. Maybe you should pass the values to your javascript functions from the main page. Commented Jun 22, 2012 at 12:11
  • if find it odd that WebResource works because DateTime.Now.ToString() does not. Commented Jun 22, 2012 at 12:15

1 Answer 1

1

Maybe I'm not understanding your question correctly, but you have a server control that references an external JavaScript file, but in the external file you want to use a property on the control.

If that is what you are asking, then I'm afraid that it's not possible without writing custom handlers or something of the sort. You see, the external JavaScript file is requested by the client seperately from your control execution. At the time the JavaScript file is served by the server, the control has long been destroyed.

Here's what happens:

  1. A request comes to your server for the page
  2. The page loads and creates your usercontrol and a LINK to an external javascript file
  3. The client gets the result from the server
  4. The client sees a link to the external javascript file and requests it from the server
  5. The request for the javascript file comes in on the server
  6. The server gives the client the javascript file

As you can see, there are two requests coming in (step 1 and step 5). In the second request, the one for the javascript file, you no longer have the control with the property available (which was in step 1).

The only way I can think of to get this done is to do something with a custom handler to provide the javascript. This way you can pass any variables as querystring values and have the custom handler insert them into your java script.

I hope this answers your question.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for your answer. But i have javascript file as an embeded resource in my server control and i CAN parse this file when i want to get other resources from my control like images (as presented in my question) I only had to add PerformSubstitution = true to my AssemblyInfo.cs file. So when I can PARSE .js file before it loads i don't believe there is no option to get single variables from c# code. But i know only how to get resources... variables don't work :(
You can add external images because they are constants. From your original post you are able to reference "PatientList.Images.letter-bg.png" because this file is hard compiled into your assembly and as such is always there. The value of your local variable "TestVariable" is only available in the specific request that fills it. The follow up request that actually gets the JavaScript file content no longer has that variable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.