C# UserControl 中的 AJAX ScriptManager

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/650927/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 11:50:56  来源:igfitidea点击:

AJAX ScriptManager in UserControl

c#ajaxuser-controlsscriptmanager

提问by cjserio

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.

我有一个包含 UpdatePanel 的 UserControl,它包装了一些其他控件。UserControl 将在一些已经有 ScriptManager 的页面和其他没有 ScriptManager 的页面上使用。如果不存在,我希望 UserControl 自动带来自己的 ScriptManager。

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.

我已经尝试过 ScriptManager.GetCurrent,如果它返回 null,我会创建自己的 ScriptManager 并将其插入到表单中,但是我无法在 UserControl 的生命周期中尽早找到运行此代码的位置。我不断收到错误消息“ID 为‘uPnlContentList’的控件需要页面上的 ScriptManager。ScriptManager 必须出现在任何需要它的控件之前。” 每次我尝试加载页面时。我尝试运行我的代码的地方是 OnInit、CreateChildControls 和 PageLoad,它们永远不会被调用,因为它在到达它们之前就死了。

Where should I put this check?

我应该把这张支票放在哪里?

采纳答案by Daniel Henry

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?

我讨厌从另一个方向来解决这个问题,但是您使用的是母版页吗?如果是这样,您是否考虑过在其上放置一个 ScriptManager 并完成它?

回答by Dean Poulin

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.

将 ScriptManager 放在所有 ASPX 文件引用的 MasterPage 中。然后,站点中需要使用 ScriptManager 的任何其他地方都使用 ScriptManagerProxy,它将从母版页获取 ScriptManager,并让您在用户控件和后面的代码中使用它。

Now calling ScriptManager.GetCurrent(Page) will give you a reference to a script manager.

现在调用 ScriptManager.GetCurrent(Page) 将为您提供对脚本管理器的引用。

<asp:ScriptManagerProxy>
  <Scripts>
    <asp:ScriptReference Path="~/Scripts/myscript.js" />
  <Scripts>
</asp:ScriptManagerProxy>

Here's a link:

这是一个链接:

MSDN Info

MSDN 信息

回答by Rob Windsor

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.

我认为 Init 事件应该有效。使用您描述的技术(即在添加之前检查 ScriptManager.GetCurrent 是否返回 null),但在添加 ScriptManager 时,请确保将其添加为表单内的第一个控件。

I haven't tested this but I think it will work.

我还没有测试过这个,但我认为它会起作用。

回答by Ga???

What worked for me was including this line after < form >

对我有用的是在 <form> 之后包含这一行

 <asp:ScriptManager ID="scriptManager" runat="server"/>

That got me around the "...requires a ScriptManager on the page. The ScriptManager must appear before any controls that need it."

这让我明白了“...需要页面上的 ScriptManager。ScriptManager 必须出现在任何需要它的控件之前。”

回答by JanBorup

I have made 2 things that works.

我做了两件有效的事情。

On the UserControl make this Sub:

在 UserControl 上做这个 Sub:

Public Sub SetFocus()
    txtBox.Focus()
End Sub

On the caling site, call this before your ScriptManagerCall

在校准站点上,在 ScriptManagerCall 之前调用它

userControl.SetFocus()

If you have AutoPostback on you UserControl you also have to set the focus in TextChanged event.

如果您在 UserControl 上有 AutoPostback,您还必须在 TextChanged 事件中设置焦点。

Protected Sub txtBox_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtBox.TextChanged
    txtBox.Focus()
    ......
End Sub

Hope this works.

希望这有效。