2

From my previous question(Create json using JavaScriptSerializer), In .ashx file I am printing the json object using:

context.Response.ContentType = "application/json";
context.Response.Write(json);

I am calling this .ashx file from default.aspx which has some javascript function inside its <head> tag. My question is :
How will I be able to call the javascript function from .ashx file after context.Response.Write(json);?

UPDATE:
My ultimate goal is to achieve Server Side Processing for DataTable.In that i want to bind the rows with context menu using javascript function. For that I am using following code to call .ashx file:

 $('#example').dataTable({
            'bProcessing': true,
            'bServerSide': true,
            'sAjaxSource': '/data.ashx'
        });
Community
  • 1
  • 1
Prasad Jadhav
  • 5,090
  • 16
  • 62
  • 80

2 Answers2

1

Are you using ajax requests? In that case, you can use the success method that is available in javascript, as in the following example from w3schools:

function showHint(str)
{
var xmlhttp;
if (str.length==0)
  { 
  document.getElementById("txtHint").innerHTML="";
  return;
  }
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    // You can call your custom method here...  
    document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","gethint.asp?q="+str,true);
xmlhttp.send();

}

Or if you are using jquery:

$.ajax({
  url: "test.html",
  context: document.body
}).done(function() { 
  // You can call your custom method here... 
  $(this).addClass("done");
});

UPDATE

Check out: http://datatables.net/usage/callbacks The method you can use is: fnInitComplete

e.g.

$('#example').dataTable({
            'bProcessing': true,
            'bServerSide': true,
            'sAjaxSource': '/data.ashx',
            'fnInitComplete' : function() {
                alert('Your menu population code here!');
             }
        });
wasimbhalli
  • 5,122
  • 8
  • 45
  • 63
  • I am sorry, i should have mentioned it earlier that how am i calling `.ashx` file. Please have a look at updated part of question. – Prasad Jadhav Oct 17 '12 at 11:22
0

You could use

eval

to evaluate the response as javascript on client-side. But I doubt you really need or want this, it might not be a very elegant solution. So what you want to archieve?

mbue
  • 1,591
  • 2
  • 14
  • 34
  • i dont know how `eval` really works...will you please clarify for me? My ultimate goal is to achieve [Server Side Processing for DataTable](http://stackoverflow.com/questions/3531438/jquery-datatables-server-side-processing-using-asp-net-webforms).In that i want to bind the rows with context menu using javascript function. – Prasad Jadhav Oct 17 '12 at 11:08
  • eval takes a string argument containing the javascript code to execute. Why don't you just assign HTML-class attributes to the rows (e.g. main-row, sub-row, row-with-special-context-menu) on server-side and run a client-side javascript to assign the respective context-menu for each row-class after getting response? – mbue Oct 17 '12 at 11:37