UpdatePanel 更新时未调用 Sys.Application.add_init

发布于 2024-09-17 19:35:58 字数 1605 浏览 3 评论 0原文

我有一个自定义 UI 控件,其中有一个围绕 AJAX.NET 框架编写的 JavaScript 类。它继承自Sys.UI.Control。该控件本身是一个 UserControl。在渲染方法中,我将控件包装在一个跨度中,并为该跨度的 id 提供用户控件的 ClientID。这样我可以稍后执行此操作:

var control = $find('<%= ServerControlID.ClientID %>');

这使我能够获取代表 UI 控件的客户端对象。

我通过执行此操作(也在渲染方法中)在 Javascript 中创建控件,

writer.Write(@"<script type='text/javascript'>
Sys.Application.add_init(function() {
debugger;
    $create(General.Filters.AccountGroupFilter,
        " + GetProperties() + @",
        null,
        null,
        $get('" + this.ClientID + @"'));
});
</script>");

这将创建对象。它是在生命周期的 init 事件内部完成的。

问题是该控件被消费页面/控件包装在 UpdatePanel 内。当另一个控件启动更新时,所有控件的 UI 都会重新绘制,但 init 事件永远不会再次触发。

当我这样做时会发生什么:

var control = $find('<%= ServerControlID.ClientID %>');
if(control != null)
  control.doSomething();

没有任何反应,因为 $find 在 AJAX 调用后找不到控件。我知道我这样做不正确。我哪里错了。 MSDN 上关于这个东西的文章让我陷入了困境!

编辑:我想我会在 UserControl.cs 部分中包含我正在执行的 Render 方法

    protected override void Render(HtmlTextWriter writer)
    {
        writer.Write("<span id=\"" + this.ClientID + "\" >");
        writer.Write(@"<script type='text/javascript'>
        Sys.Application.add_init(function() {

        $create(TradePMR.OMS.Resources.UserControls.General.Filters.AccountGroupFilter,
        " + GetProperties() + @",
        null,
        null,
        $get('" + this.ClientID + @"'));
        });
        </script>");

        base.Render(writer);
        writer.WriteEndTag("span");
    }

I have a custom UI control which has a JavaScript class written around the AJAX.NET framework. It inherits from the Sys.UI.Control. The control itself is a UserControl. In the render method I wrap the control in a span and give the span's id the ClientID of the user control. This way I can do this later on:

var control = $find('<%= ServerControlID.ClientID %>');

This allows me to get the client object that represents the UI control.

I create the control in Javascript by doing this (also in the render method)

writer.Write(@"<script type='text/javascript'>
Sys.Application.add_init(function() {
debugger;
    $create(General.Filters.AccountGroupFilter,
        " + GetProperties() + @",
        null,
        null,
        $get('" + this.ClientID + @"'));
});
</script>");

This creates the object. It's done inside of the init event of the life cycle.

The problem is that this control is wrapped inside of an UpdatePanel by a consuming page/control. When another control initiates an update all the controls' UIs get redrawn, BUT the init event NEVER FIRES again.

What happens then is when I do this:

var control = $find('<%= ServerControlID.ClientID %>');
if(control != null)
  control.doSomething();

Nothing happens because the $find does not find the control after the AJAX call. I know I'm NOT doing this correctly. Where am I going wrong. The MSDN article for this stuff is pointing me in circles!

EDIT: Figured I'd include the Render method that I'm doing in the UserControl.cs portion

    protected override void Render(HtmlTextWriter writer)
    {
        writer.Write("<span id=\"" + this.ClientID + "\" >");
        writer.Write(@"<script type='text/javascript'>
        Sys.Application.add_init(function() {

        $create(TradePMR.OMS.Resources.UserControls.General.Filters.AccountGroupFilter,
        " + GetProperties() + @",
        null,
        null,
        $get('" + this.ClientID + @"'));
        });
        </script>");

        base.Render(writer);
        writer.WriteEndTag("span");
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

隔纱相望 2024-09-24 19:35:58

您不需要自己编写所有 Sys.Application.add_init 内容;按照这篇 MSDN 文章 实现 IScriptControl

关键是关于 RegisterScriptControlRegisterScriptDescriptors 的部分,它们使用 ScriptManager 注册您的控件,以便异步回发将根据需要重新创建您的客户端控件。

作为脚注,如果您是,这里有一篇单独但类似的文章改为实现 Sys.UI.Behavior

You don't need to write all that Sys.Application.add_init stuff yourself; implement IScriptControl as per this MSDN article.

The key is the part about RegisterScriptControl and RegisterScriptDescriptors, which registers your control with the ScriptManager so that an async postback will recreate your client control as necessary.

As a footnote, there is a seperate but similar article here if you are implementing Sys.UI.Behavior instead.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文