javascript - ScriptManager 不可访问

标签 javascript asp.net class scriptmanager

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

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

ScriptManager is inaccessible due to its protection level.

这是我正在尝试的代码

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

知道为什么相同的代码在 aspx 页面上运行成功但在 .cs 类中运行失败吗?

最佳答案

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);

关于javascript - ScriptManager 不可访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14039009/

相关文章:

javascript - jQuery:如何检查内容是否有任何 URL

javascript - 如何在 Mongoose findOne() 调用中使用 Date 对象

javascript - 自定义函数中的回调函数

c# - 将处理程序的结果转换为数组格式

c++ - 使用 guest 对象更改类中构​​造函数的调用顺序

javascript - 为什么 Javascript 异步检索数据(ajax,...)而 PHP 同步检索数据(mysql_query)?

asp.net - EnableHeaderChecking=true 是否足以防止 Http header 注入(inject)攻击?

c# - 如何在一个模型中选择不同表格的内容

ios - Objective-C : Why can't I update a label from another class (AppDelegate > MainViewController)?

c++ - const 和引用类型是如何在初始化列表之前初始化的?