c# - Assign an outside JavaScript variable inside a ASP.NET user control that is loaded using Ajax -


i have user control assigns javascript variable defined inside script file. user control compiled asp.net generic handler html , markup pasted jquery ajax inside specified container. problem assignment script not being pasted compiled user control html. javascript code resides inside javascript file:

var xyz; function initenvironment(){ $.ajax({         url: handlerurl,         type: "post",         data: json.stringify(             {                 xsrftoken: xsrf_token,                 data: ajaxdata             })         ,         datatype: "json",         cache: true,         async: isajaxasync,         beforesend: null,         success: function (data) {                 $('#containerdiv').html(data); //this must contain assignment statement xyz                 alert(xyz); //xyz null here         }     }); } 

and user control c# code should assign variable:

literal1.text = lithtml + @"<script type='text/javascript'>             xyz = " + xyz + ";</script>"; 

lithtml contains html code pasted inside container concatenated script code not pasting. can reason?

update

xyz @ javascript side json object.

instead of setting xyz using literal script tag, try using registerstartup script execute funciton set it. might have decode , encode json object.

server side

clientscriptmanager cs = page.clientscript; cs.registerstartupscript(this.gettype(), "setxyz", "setxyz(" + xyz + ")"); 

client side

function setxyz(xyzval) {   xyz = xyzval; } 

you might able well

cs.registerstartupscript(this.gettype(), "setxyz", @"<script type='text/javascript'>         xyz = " + xyz + ";</script>"); 

Comments