If you donโt need those in the form, just make them variables in your JavaScript. To output them, encode them using the JavaScriptSerializer class :
<% // Presumably somewhere in your C# code... JavaScriptSerializer serializer = new JavaScriptSerializer(); %> <script> var hid1 = <%= serializer.Serialize(valueForHid1) %>; var hid2 = <%= serializer.Serialize(valueForHid2) %>; </script>
(See note below on global values.)
Using them later:
$.ajax({ data:{ var1 : hid1, var2 : hid2 } );
Globals: as shown in the figure, hid1 and hid2 end as global (in most browsers they are used when you also use hidden fields). I recommend not using global variables, but instead wrapping everything in scope:
(function() { var hid1 = <%= serializer.Serialize(valueForHid1) %>; var hid2 = <%= serializer.Serialize(valueForHid2) %>; // .... $.ajax({ data:{ var1 : hid1, var2 : hid2 } ); })();
If for some reason you need to use global, use only one:
var myOneGlobal = { hid1: <%= serializer.Serialize(valueForHid1) %>, hid2: <%= serializer.Serialize(valueForHid2) %> };
Using this later:
$.ajax({ data:{ var1 : myOneGlobal.hid1, var2 : myOneGlobal.hid2 } );
You can output the entire graph of an object into a single variable (possibly myOneGlobal ) using the serializer:
<script> var myOneGlobal = <%= serializer.Serialize(objectWithData) %>; </script>
source share