嵌套在UpdatePanel中无法获得ScriptManager.RegisterStartupScript在WebControl的工作 [英] Can't get ScriptManager.RegisterStartupScript in WebControl nested in UpdatePanel to work

查看:221
本文介绍了嵌套在UpdatePanel中无法获得ScriptManager.RegisterStartupScript在WebControl的工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有什么,我相信应该是一个相当简单的问题,但对我的生活中,我看不到我的问题。这个问题涉及到ScriptManager.RegisterStartupScript,这是我以前使用过很多次了。

I am having what I believe should be a fairly simple problem, but for the life of me I cannot see my problem. The problem is related to ScriptManager.RegisterStartupScript, something I have used many times before.

这个场景我是,我已经被插入到页面中的自定义Web控件。控制(和一个或两个其他)被嵌套一个UpdatePanel内部。他们被插入到页面上的占位符:

The scenario I have is that I have a custom web control that has been inserted into a page. The control (and one or two others) are nested inside an UpdatePanel. They are inserted onto the page onto a PlaceHolder:

<asp:UpdatePanel ID="pnlAjax" runat="server">
  <ContentTemplate>
    <asp:PlaceHolder ID="placeholder" runat="server">
    </asp:PlaceHolder>
    ...

protected override void OnInit(EventArgs e){
  placeholder.Controls.Add(Factory.CreateControl());
  base.OnInit(e);
}

这是唯一的更新面板在页面上。

This is the only update panel on the page.

控制需要一些初始的javascript运行它才能正常工作。控制要求:

The control requires some initial javascript be run for it to work correctly. The control calls:

ScriptManager.RegisterStartupScript(this, GetType(), 
                                    Guid.NewGuid().ToString(), script, true);

和我也尝试过:

ScriptManager.RegisterStartupScript(Page, Page.GetType(), 
                                    Guid.NewGuid().ToString(), script, true);

问题是,首先显示的页面时,正确的脚本运行,但没有一个部分回发后重新运行。我已经试过如下:

The problem is that the script runs correctly when the page is first displayed, but does not re-run after a partial postback. I have tried the following:

  1. 从CreateChildControls中调用的RegisterStartupScript
  2. 在调用的RegisterStartupScript从的OnLoad /在preRender
  3. 使用不同的参数组合的前两个参数(在上面的控制的例子是页面和类型为的GetType(),但我已经尝试使用控件本身,等试过)。
  4. 我用执着和新的IDS(不,我相信这应该有大的影响无论哪种方式)试过了。
  5. 在我使用了一些断点,因此已验证注册行的调用是否正确。

我没有尝试过的唯一的事情就是用在UpdatePanel本身作为控制和类型,因为我不相信该控件应注意更新面板(在任何情况下,似乎没有成为一个好办法获取更新面板?)。

The only thing I have not tried is using the UpdatePanel itself as the Control and Type, as I do not believe the control should be aware of the update panel (and in any case there does not seem to be a good way of getting the update panel?).

有人能看到什么,我会在上面是做错了什么?

Can anyone see what I might be doing wrong in the above?

感谢:)

嗯,回答上面的查询 - 它看起来好像占位符莫名其妙地打乱了ScriptManager.RegisterStartupScript

Well, to answer the query above - it does appear as if the placeholder somehow messes up the ScriptManager.RegisterStartupScript.

当我拉控制输出C是占位符和$ C $直接到注册脚本正常工作的页面(我也使用控制本身作为一个参数AM)。

When I pull the control out of the placeholder and code it directly onto the page the Register script works correctly (I am also using the control itself as a parameter).



ScriptManager.RegisterStartupScript(this, GetType(), Guid.NewGuid().ToString(), script, true);

任何人都可以扔在了为什么注射的对照到占位将prevent使用ScriptManager无法正常注册脚本任何光线?我猜这可能是与动态控件的生命周期,但将AP preciate(我自己的知识),如果有一个正确的流程上面。

Can anyone throw any light on why an injected control onto a PlaceHolder would prevent the ScriptManager from correctly registering the script? I am guessing this might have something to do with the lifecycle of dynamic controls, but would appreciate (for my own knowledge) if there is a correct process for the above.

推荐答案

我觉得你确实应该使用的控制超负荷的RegisterStartupScript的

I think you should indeed be using the Control overload of the RegisterStartupScript.

我已经试过以下code服务器控件:

I've tried the following code in a server control:

[ToolboxData("<{0}:AlertControl runat=server></{0}:AlertControl>")]
public class AlertControl : Control{
    protected override void OnInit(EventArgs e){
        base.OnInit(e);
        string script = "alert(\"Hello!\");";
        ScriptManager.RegisterStartupScript(this, GetType(), 
                      "ServerControlScript", script, true);
    }
}

然后在我的网页我有:

Then in my page I have:

protected override void OnInit(EventArgs e){
    base.OnInit(e);
    Placeholder1.Controls.Add(new AlertControl());
}

在哪里PLACEHOLDER1是在更新面板中的占位符。占位符有几个其他控件就在里面,包括按钮。

Where Placeholder1 is a placeholder in an update panel. The placeholder has a couple of other controls on in it, including buttons.

这表现完全按照您所期望的,我得到了一个警告说你好,每次我加载页面或导致更新面板更新。

This behaved exactly as you would expect, I got an alert saying "Hello" every time I loaded the page or caused the update panel to update.

其他的事情你可以看看是挂接到一些正在更新面板请求期间解雇页面生命周期事件:

The other thing you could look at is to hook into some of the page lifecycle events that are fired during an update panel request:

Sys.WebForms.PageRequestManager.getInstance()
   .add_endRequest(EndRequestHandler);

在每一个更新面板完成其更新时间 PageRequestManager可以endRequestHandler 事件激发 - 这将允许你调用一个方法来设置你的控制。

The PageRequestManager endRequestHandler event fires every time an update panel completes its update - this would allow you to call a method to set up your control.

我唯一的问题是:

  • 什么是你的脚本实际上是在做什么?
  • presumably你可以看到HTML脚本在页面的底部(正好结束&lt前; /表&gt;标签)?
  • 你有没有尝试把几个警报(在这里);在你的启动脚本调用,以查看它是否被正确叫什么名字?
  • 您是否尝试过Firefox和萤火虫 - ?是,报告任何脚本错误
  • What is your script actually doing?
  • Presumably you can see the script in the HTML at the bottom of the page (just before the closing </form> tag)?
  • Have you tried putting a few "alert("Here");" calls in your startup script to see if it's being called correctly?
  • Have you tried Firefox and Firebug - is that reporting any script errors?

这篇关于嵌套在UpdatePanel中无法获得ScriptManager.RegisterStartupScript在WebControl的工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆