ScriptManager无法访问

时间:2012-12-26 10:07:34

标签: javascript asp.net class scriptmanager

我正在尝试从.cs类调用aspx页面上实现的js函数。但是,ScriptManager似乎不存在于.cs类中。基本上,.cs文件是我在项目中使用的dll的一部分。我需要从dll中的.cs文件调用aspx页面上实现的js函数。

从aspx页面成功调用了js函数,但是当我在.cs文件中尝试相同的代码时,它说

  

由于其保护级别,ScriptManager无法访问。

这是我正在尝试的代码

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

为什么相同的代码在aspx页面上成功运行而不是从.cs类运行的任何想法?

2 个答案:

答案 0 :(得分:1)

ScriptManager.RegisterStartupScript接受Page或Control作为第一个参数。确保将当前页面传递给cs方法

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

使用以下命令从aspx.cs页面调用:

MyMethod(this.Page);

答案 1 :(得分:1)

好吧,为了克服上述问题,我只是尝试使用这段代码

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

请注意在ScriptManager中使用完整命名空间。

相关问题