views:

120

answers:

4

Looking to pass variables from c# to javascript to use some jquery code. Passing doubles, ints, strings, arrays. Does anyone know how to do this?

for example if I have this code snip in c#:

string blah = "this is a blah string";

I would like to pass this into javascript so that I could use a mouseover event in jquery:

$('#myDiv').mouseover(function(){ //do something with my 'blah' string });
A: 

You need this(How Do I: Add JavaScript to An ASP.NET Page), I guess?

ArsenMkrt
anything in c#?
cfarm54
A: 

I recently used this ToJson() extension method along with Page.RegisterClientScriptBlock Method to pass down a multi-level mapping object that populates three levels of cascaded dropdowns.

Floyd Pink
@FloydPink not sure what you mean here, but could you see above for the edited portion?
cfarm54
+1  A: 

Nevermind I think I figured it out. Assuming the code above, you can write in javascript:

<script type="text/javascript"> var JavascriptBlah = '<%=blah%>'</script>

This will pass the blah in c# to the JavascriptBlah on the client side. Then you can manipulate on client side.

cfarm54
A: 

you can use asp:HiddenField variables to pass values between codebehind and js.. this accomplished with viewstate of page during postbacks...

dankyy1
@dankyy1 can you show me some code?
cfarm54
http://stackoverflow.com/questions/252222/access-an-asphiddenfield-control-in-javascript the link has a sample code by this way you may access value of hiddenfield either code behind or javascript
dankyy1