views:

1998

answers:

3

I am trying to RegisterClientScriptBlock in a method that is only called via an AJAX call. It doesn't appear to actually register the script on the page and I'm guessing this is because it's not actually reloading the entire page. Is there any way to register javascript on a page from within an ajax method call?

 protected void MyMethod(object sender, EventArgs e)
 {
  // This method only called via AJAX call

  Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "resize", "alert('here');", true);
 }
A: 

As far as I know for this you would be forced to be calling this method via a PostBack and not an ajax call. There may be OTHER ways of doing this, but it is not possible with Page.ClientScript....

Mitchel Sellers
+5  A: 

With AJAX enabled pages, you should use the ScriptManager to register scripts:

ScriptManager.RegisterClientScriptBlock(Page, typeof(MyPage), 
    "MyScript", "GoStuff()", true)

You can use this to register all your scripts (Original load, postback, AJAX postback).

Robert Wagner
A: 

In general, when loading external javascript after appending an element innerHTML with a block containing such script, one needs to evaluate (eval) the script in order for it to work properly and render itself into the current loaded document.

I'd suggest doing on of the following:

Use an external tool such as YUI get utility which is supposed to enable such behavior or do some evaluation for scripts yourself like this

joeysim