4

我正在尝试从 .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 类?

4

2 回答 2

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);
于 2012-12-26T10:17:16.247 回答
1

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

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

注意 ScriptManager 使用完整的命名空间。

于 2012-12-28T16:51:47.423 回答