RegisterClientScriptBlock within AJAX method call

后端 未结 5 1401
长情又很酷
长情又很酷 2020-12-09 12:53

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

相关标签:
5条回答
  • 2020-12-09 13:00

    If there's anyone else out there like myself, and the accepted answer STILL won't work for you, then look no further.

    Check out this link - a simple class that pops a Javascript alert no matter if you're on page load, unload, AJAX request, etc:

    WebMsgBox.Show("Your message here");
    
    0 讨论(0)
  • 2020-12-09 13:12

    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

    0 讨论(0)
  • 2020-12-09 13:13

    It works if you specify the UpdatePanel being updated on the AJAX call back. For example:

    ScriptManager.RegisterClientScriptBlock(UpdatePanelMain, typeof(UpdatePanel),
       UpdatePanelMain.ClientID,
       "document.getElementById('imgLoading').style.display = 'none';" +
       "document.getElementById('divMultiView').style.display = 'inline';",
       true);
    

    The control in the first argument must be inside the update panel or the update panel itself that triggers the update.

    0 讨论(0)
  • 2020-12-09 13:18

    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).

    0 讨论(0)
  • 2020-12-09 13:18

    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....

    0 讨论(0)
提交回复
热议问题