Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RegisterClientScriptBlock within AJAX method call

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);
    }
like image 494
Aaron Palmer Avatar asked Nov 17 '08 21:11

Aaron Palmer


2 Answers

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

like image 130
Robert Wagner Avatar answered Nov 12 '22 08:11

Robert Wagner


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.

like image 32
Mark Brito Avatar answered Nov 12 '22 07:11

Mark Brito



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!