用户控件中的 AJAX 脚本管理器
我有一个 UserControl,其中包含一个包含其他一些控件的 UpdatePanel。 UserControl 将用于某些已经具有 ScriptManager 的页面和其他没有 ScriptManager 的页面。 我希望 UserControl 自动携带自己的 ScriptManager(如果不存在)。
我尝试过 ScriptManager.GetCurrent,如果它返回 null,我将创建自己的 ScriptManager 并将其插入到表单中,但我无法在 UserControl 的生命周期中尽早找到运行此代码的位置。 我不断收到错误“ID 为‘uPnlContentList’的控件需要页面上的 ScriptManager。ScriptManager 必须出现在任何需要它的控件之前。” 每次我尝试加载页面时。 我尝试运行代码的地方是 OnInit、CreateChildControls 和 PageLoad,它们从未被调用,因为它在到达它们之前就死掉了。
我应该把这张支票放在哪里?
I have a UserControl that contains an UpdatePanel which wraps some other controls. The UserControl will be used on some pages that already have a ScriptManager and other pages that do not have a ScriptManager. I'd like the UserControl to automatically bring its own ScriptManager if one does not exist.
I have tried ScriptManager.GetCurrent and if it returns null i create my own ScriptManager and insert it into the Form but I can't find a place early enough in the UserControl's lifecycle to run this code. I keep getting the error "The control with ID 'uPnlContentList' requires a ScriptManager on the page. The ScriptManager must appear before any controls that need it." every time i try loading the page. The places i've tried running my code are OnInit, CreateChildControls and PageLoad and they never get called because it dies before reaching them.
Where should I put this check?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
将 ScriptManager 放入所有 ASPX 文件引用的 MasterPage 中。 然后,站点中需要使用 ScriptManager 的任何其他地方都可以使用 ScriptManagerProxy,它将从母版页获取 ScriptManager,并让您在用户控件和后面的代码中使用它。
现在调用 ScriptManager.GetCurrent(Page) 将为您提供对脚本管理器的引用。
这是一个链接:
MSDN 信息
put a ScriptManager in your MasterPage that all your ASPX files reference. Then anywhere else in the site that you need to work with the ScriptManager use a ScriptManagerProxy that will get the ScriptManager from the master page and let you work with it in the user control and in the code behind.
Now calling ScriptManager.GetCurrent(Page) will give you a reference to a script manager.
Here's a link:
MSDN Info
我不想从另一个方向来讨论这个问题,但是您使用母版页吗? 如果是这样,您是否考虑过在其上放置一个 ScriptManager 并完成它?
I hate to come at this in another direction, but are you using a master page? And if so, have you considered placing a single ScriptManager on it and being done with it?
我认为 Init 事件应该有效。 使用您描述的技术(即在添加之前检查 ScriptManager.GetCurrent 返回 null),但是当您添加 ScriptManager 时,请确保将其添加为表单内的第一个控件。
我还没有测试过这个,但我认为它会起作用。
I think the Init event should work. Use the technique you described (i.e. checking that ScriptManager.GetCurrent returns null before adding) but when you add the ScriptManager, make sure you add it as the first control inside the form.
I haven't tested this but I think it will work.
对我有用的是在 < 之后包含这一行。 表格>
这让我遇到了“...需要页面上的 ScriptManager。ScriptManager 必须出现在任何需要它的控件之前。”
What worked for me was including this line after < form >
That got me around the "...requires a ScriptManager on the page. The ScriptManager must appear before any controls that need it."
我做了两件有效的事情。
在 UserControl 上创建此子:
在调用站点上,在 ScriptManagerCall 之前调用此子
如果您的 UserControl 上有 AutoPostback,则还必须在 TextChanged 事件中设置焦点。
希望这有效。
I have made 2 things that works.
On the UserControl make this Sub:
On the caling site, call this before your ScriptManagerCall
If you have AutoPostback on you UserControl you also have to set the focus in TextChanged event.
Hope this works.