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);
}
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).
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With