1

I'm having some issues accessing a static variable in a class when getting it from a code behind function called from javascript.

My aspx page:

<script type="text/javascript">
function AlertMsg(msg) {
   var msg213 = "<%= GetValue(" msg ") %>";
   alert(msg + '::' + msg213);
}
</script>

Code behind:

public string GetValue(string sString)
{
   return MyNamespace.MyClass.MyStaticVariable;
}

I set this variable in a page_load in another page. I'm accessing the javascript function by invoking it from a C# WebBrowser application. It's always empty.

Any ideas?

5
  • What is actually happening is that GetValue is being invoked on the server, and the parameter you are passing in is the string literal " msg ". JavaScript runs on the client and server tags (such as <% %>) execute as the page output is being generated by the server. Commented Aug 7, 2014 at 15:41
  • You probably don't want to do this but you could load the value into a hiddenfield so it is available client side. Commented Aug 7, 2014 at 15:41
  • What does the generated HTML/JS look like? Commented Aug 7, 2014 at 15:43
  • But is the other page loaded before you try to get the value of the static value? Better make the class static and use the static constructor to initialize the value. Otherwise use global.asax file. Commented Aug 7, 2014 at 15:44
  • I'm setting the value at runtime. After that I try to invoke the javascript function to get the variable (through WebBrowser.InvokeScript), but then it comes up as NULL. Commented Aug 7, 2014 at 19:20

4 Answers 4

1

I think you just need to add '+' around your reference to 'msg'

var msg213 = "<%= GetValue(" + msg + ") %>";
Sign up to request clarification or add additional context in comments.

Comments

1

ASP.NET isn't like a desktop application, any variables written on another page will be lost when moving to another page. You need to save the value to somewhere persistent.

  1. Session
  2. Cache
  3. Database
  4. App or Web Config files.
  5. Variable needs to be a const or static

5 Comments

I agree that he isn't understanding the difference between code executing on the client vs code executing on the server, but since he is storing the value in a static variable, then when he sets it in one page, it can be available when another page request comes in (depending on how he declared the static field).
Each page request wipes all variables clean, unless its a const any value written to it will change.
Any field declared as static will retain the last value that it was assigned, regardless of how many page requests there have been, as long as the app pool hasn't recycled.
Yep, apologies you are right on that. It can be declared static as well.
I'm using a static variable in a class, and this works great within other pages. But when I'm trying to fetche it from Default.aspx, it is always NULL.
1

Try this

'<%= GetValue("Some Value") %>';

This means when page rendering, GetValue method calls and return string will be write in the document body, like Respose.Write

This will only happend when when page rendering and no further call will happend.

Comments

0

I think part of the confusion is coming from the formatting in the code. If you look at just the server tag: <%= GetValue(" msg ") %>, you will see that the GetValue method is being invoked, and the literal string msg is being passed in. There are quotes around the server tag itself, but those do not affect what is inside the server tag. You are not passing in the value of the msg parameter of the JavaScript function.

Server methods cannot be invoked from JavaScript in such a simple manner, it requires using something like AJAX to accomplish.

Comments

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.