Calling JavaScript function from codebehind C# in a user control

aleafonso picture aleafonso · Sep 8, 2011 · Viewed 12.4k times · Source

Currently, I am calling my JavaScript functions using:

ScriptManager.RegisterClientScriptBlock(this, typeof(Page), Guid.NewGuid().ToString(), "$(function(){$.jGrowl('Hello World');});", true);

It works perfectly! Even using master page and update panel it works as expected.

However, when I try to do the same in a user control that is embedded in a page that is being called with a jQuery thickbox, it does not work!

Does anyone know how to solve this issue?

Answer

aleafonso picture aleafonso · Sep 9, 2011

This solved the problem:

ScriptManager.RegisterClientScriptBlock(this.Page, typeof(UpdatePanel), Guid.NewGuid().ToString(), "$(function(){$.jGrowl('Hello World');});", true);

As it was mentioned by @Joel, there was a problem with the type I was using as a parameter for the function.

Note: If you're using a thickbox, probably you are not using the master page in the page that contains the user control. Therefore, jQuery needs also to be referenced in that page since the master page is not partaking in the thickbox.