ScriptManager.GetCurrent 方法背后的原因
Asp.net 团队设计的脚本管理器使得每页仅存在一个实例(HttpHandler),我找不到他们扩展诸如 ScriptManager.GetCurrent
获取页面内的实例。为什么开发商不能这样做
if(ScriptManager == null)
{
throw new Exception("The Below ajax control requires ScriptManager in the page");
}
Asp.net team had designed script manager such that only one instance existed per page(HttpHandler), i can't find a valid reason why they had extended a method like ScriptManager.GetCurrent
to get the instance inside a page. Why couldn't developers do
if(ScriptManager == null)
{
throw new Exception("The Below ajax control requires ScriptManager in the page");
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我相信是因为除了页面的代码隐藏文件之外可能还有其他地方可以访问实际的 ScriptManager。使用静态方法 GetCurrent(),您可以从代码中的任何位置(例如从类库)访问当前上下文的 ScriptManager。 GetCurrent 的实现如下所示:
因此,它只是访问 ScriptManager 实例的快捷方式。
当 ScriptManager 在母版页上定义时,您的代码将无法在内容页或用户控件中运行。
I believe because there might be other places than the code behind file of the page to access the actual ScriptManager. With the static method GetCurrent() you can access the ScriptManager of the current context from anywhere in the code (e.g. from a class library). The implementation of GetCurrent looks like this:
Therefore it's just a shortcut to get access to the ScriptManager instance.
Your code would not work from a content page or a user control, when the ScriptManager is defined on the master page.