56566

Calling JavaScript function from codebehind C# in a user control

Question:

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?

Answer1:

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.

Answer2:

It looks to me like what you have is incompatible with respect to types. When you include this in an actual page this portion of the code: (this, typeof(Page),... works because you're dealing with a Page. Once you put it in a UserControl you're no longer dealing with a Page.

Something you can try is adding a public property to your user control:

public System.Web.Page ParentForm { get; set; }

In the page that includes the control include this code either in the Page_InitComplete or Page_Load event:

myUserControl.ParentForm = this;

Then you can modify your scriptmanager statement to be:

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

Answer3:

Have you ensured that your user control is calling databind()? I've experienced a similar issue before.

Recommend

  • Page EventValidation is True error?
  • Asp.net updatepanel Listbox not refreshing layout
  • Cannot Access the Controls inside an UpdatePanel
  • firing ASP.NET Events from javascript
  • How to trigger UpdatePanel from a GridView TemplateColumn click?
  • Matching IDs in Update Panel within a Repeater - “already contains a definition for…”
  • How do I preserve a value entered in a TextBox inside a gridview's templatefield when the gridv
  • NSOpenPanel's setDirectoryURL doesn't work on Lion
  • Windows Forms switch between Panels
  • Delphi Chromium Embedded - Clear browser cache
  • addressing in assembler
  • Extjs, handling success or failure when doing a standard submit in a form
  • How to access culture data in globalize.js V1.0.0
  • How to disable all widgets inside Panel or inside Composite?
  • PHP buffered output depending on server setting?
  • Functions in global context
  • Email format validation in mvc3 view
  • Nant, Vault & Windows Integrated Authentication
  • Bug in WPF DataGrid
  • Is possible to count alias result on mysql
  • Check if a string to interpolate provides expected placeholders
  • Font Awesome Showing Box instead of Icons
  • jQuery tmpl and DataLink beta
  • How can I estimate amount of memory left with calling System.gc()?
  • Why winpcap requires both .lib and .dll to run?
  • retrieve vertices with no linked edge in arangodb
  • using conditional logic : check if record exists; if it does, update it, if not, create it
  • How to disable jQuery.jplayer autoplay?
  • CSS Applying specific rule for a specific monitor resolution with only CSS is posible?
  • How to set the response of a form post action to a iframe source?
  • Understanding cpu registers
  • Setting background image for body element in xhtml (for different monitors and resolutions)
  • Authorize attributes not working in MVC 4
  • Are Kotlin's Float, Int etc optimised to built-in types in the JVM? [duplicate]
  • Add sale price programmatically to product variations
  • unknown Exception android
  • Busy indicator not showing up in wpf window [duplicate]
  • Python/Django TangoWithDjango Models and Databases
  • Net Present Value in Excel for Grouped Recurring CF