ScriptManager is unavailable

I am trying to call a js function implemented on an aspx page from a .cs class. But ScriptManager does not seem to exist in the .cs class. Basically, the .cs file is part of the dll that I use in my project. I need to call the js function, which is implemented on an aspx page, from my .cs file present in the dll.

The js function runs successfully from the aspx page, but when I try to use the same code in the .cs file, it says

ScriptManager is unavailable due to its level of protection.

here is the code i am trying

protected void MyMethod() { ScriptManager.RegisterStartupScript(this, this.GetType(), "key", "jsfunction();", true); } 

any ideas why the same code works successfully on an aspx page but not from the .cs class?

+4
source share
2 answers

Well, to overcome the above problem, I just tried using this piece of code

 System.Web.UI.ScriptManager.RegisterStartupScript(this, this.GetType(), "key", "jsfunction();", true); 

Note the use of the full namespace using the ScriptManager.

+1
source

ScriptManager.RegisterStartupScript takes a page or control as the first argument. Make sure you pass the current page to the cs method

 protected void MyMethod(Page page) { ScriptManager.RegisterStartupScript(page, typeof(UpdatePanel), new Guid().ToString() , "jsfunction();", true); } 

And call up the aspx.cs page using:

 MyMethod(this.Page); 
+1
source

All Articles