tags:

views:

70

answers:

5

I have some javascrtipt eg:

<script type="text/javascript"> 
flashvars.myval  = "blah"; //get this from c# ..etc 
</script>

i need to assign the variable from c#

how can i do that? can i call a c# method?

im using regular asp.net.

the javascript is on the aspx page.

A: 

You might want to have a look at something like Script#.

RichardOD
+2  A: 

You need to provide more info. Assuming that's on a page, some options are:

  1. trigger a postback and have the server send a script with the response to execute after everything is loaded (using RegisterStartupScript)
  2. do an ajax request to get the data from the server, and set the variable or do anything on the callback
  3. if its always sent from the server on web forms do something like (or call a property on the page):

    var somevar = '<%=SomePageMethodThatReturnsTheValue()%>';

  4. if its always sent from the server on asp.net mvc do something like (or ViewData["SomeValue"] if its an untyped view):

    var somevar = '<%=Model.MyProperty%>';


After the edit: what u want its option 3. Something like:

<script type="text/javascript"> 
flashvars.myval  = '<%= SomeMethodInThePageThatReturnsTheValue() %>'; //get this from c# ..etc 
</script>

the code can also be a property that you set on load: <%= SomeProperty %>.

eglasius
Freddy is correct; these are the two only sensible options.@raklos: Remember the javascript is executed on the client, while the c# code is executed on the server - hence there is no way to directly interact between the two, although AJAX makes interaction between client and server code easier for the developer.
Digitalex
A: 

Is this script a separate file or is it on the aspx page?

If so something like this could work:

.get({ 'userID': '<%=UserID%>', rest of JSON});

UserID is a C# property.

AKofC
A: 

It depends what technology you are using.

If you are using ASP MVC, pass the data into the view (whatever way suits you) and then use the <%= syntax

i.e

flashvars.myval = <%=ViewData["MyVal"]%>
qui
A: 

The answer I'm going to give is going to make a lot of assumptions, but given the lack of detail in your post, they seem fair. First of all, I assume you are running this code in a web page, and secondly I'm going to assume that you have access to code running in a web server. If both assumptions are correct, you could use AJAX to call code on the server, and populate this value with the output.

If you are using jQuery, you might want to look here for more information on the javascript code, and here for details on the C# side.

Pete OHanlon