سؤال

I am trying to invoke the js function implemented on the aspx page from a .cs class. But, ScriptManager doesn't seem to exist in .cs class. Basically, the .cs file is a part of dll that I am using in my project. I need to invoke the js function that is implemented on the aspx page, from my .cs file present in the dll.

The js function is successfuly invoked from the aspx page, but when I try the same code in .cs file it says

ScriptManager is inaccessible due to its protection level.

here is code I am trying

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

any ideas why the same code runs successfuly on the aspx page but not from a .cs class?

هل كانت مفيدة؟

المحلول 2

Well, to overcome the above issue I simply tried with this piece of code

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

Notice the use of full namespace with ScriptManager.

نصائح أخرى

ScriptManager.RegisterStartupScript accept Page or Control as first argument. Make sure you pass your current Page to the cs method

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

And call from aspx.cs page using:

MyMethod(this.Page);
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top