ASP.NET 在更新面板中嵌套的用户控件中注入 javascript

发布于 2024-07-09 06:46:08 字数 282 浏览 10 评论 0原文

我正在尝试在更新面板的异步回发期间通过 Page.LoadControl 方法将带有用户 Web 控件的 JavaScript 代码加载到页面中。

我已经尝试了专门针对该场景设计的脚本管理器方法,但是 javascript 没有返回给用户。

为了更好地解释我的场景:

母版页具有脚本管理器,并且一个页面在异步回发期间通过 Page.LoadControl 方法加载用户控件。 自定义控件将 JavaScript 注入到预渲染事件处理程序中。 这是注入js的时间问题还是根本不可能这样做?

I'm trying to load javascript code with a user web control into a page via a the Page.LoadControl method during an asyncron post back of an update panel.

I've tried the specially for that scenario designed methods of the scriptmanager, but the javascript just doens't get returned to the user.

To explain my scenario a bit better:

Master Page has the script manager and one page loads the user control via Page.LoadControl-method during an async post back. The custom control injects in the pre-render event handler the javascript. Is that a matter of timing to inject the js or is it just not possible to do so?

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

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

发布评论

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

评论(5

飘逸的'云 2024-07-16 06:46:08

为此你可以做

string scr;
scr = "<script src='/scripts/myscript.js'></script>"
Page.ClientScript.RegisterStartupScript(GetType(Page), "key", scr, false)

HTH

For that you can do

string scr;
scr = "<script src='/scripts/myscript.js'></script>"
Page.ClientScript.RegisterStartupScript(GetType(Page), "key", scr, false)

HTH

冷弦 2024-07-16 06:46:08

如果您不想对 JavaScript 进行硬编码,而是从文件中包含它,请调用 ScriptManager.RegisterClientScriptInclude ,然后在 ScriptManager.RegisterStartupScript

protected void Page_Load(object sender, EventArgs e)
{
   ScriptManager.RegisterClientScriptInclude(
      this, GetType(), "formatterScript", ResolveUrl("~/js/formatter.js"));
   ScriptManager.RegisterStartupScript(
      this, GetType(), "formatTableFunction", "formatTable()", true);
}

If you don't want to hard-code your JavaScript, but instead include it from a file, call ScriptManager.RegisterClientScriptInclude and then call your initialization function in ScriptManager.RegisterStartupScript.

protected void Page_Load(object sender, EventArgs e)
{
   ScriptManager.RegisterClientScriptInclude(
      this, GetType(), "formatterScript", ResolveUrl("~/js/formatter.js"));
   ScriptManager.RegisterStartupScript(
      this, GetType(), "formatTableFunction", "formatTable()", true);
}
两个我 2024-07-16 06:46:08

您尝试过吗?

Page.ClientScript.RegisterStartUpScript(GetType(Page), "key", <your script here>, addSctiptTags:=true)

我们在用户控件中执行此操作,它对我们有用

HTH

Have your tried

Page.ClientScript.RegisterStartUpScript(GetType(Page), "key", <your script here>, addSctiptTags:=true)

We do this in our User Controls and it works for us

HTH

赏烟花じ飞满天 2024-07-16 06:46:08

您可以使用 ScriptManager 类的 RegisterStartupScript 方法来注入可执行脚本:

public partial class WebUserControl : System.Web.UI.UserControl
{          
    protected void Page_PreRender(object sender, EventArgs e)
    {
        ScriptManager.RegisterStartupScript(this, GetType(), ClientID, "alert(1)", true);
    }
}

You can use the RegisterStartupScript method of the ScriptManager class to inject executable script:

public partial class WebUserControl : System.Web.UI.UserControl
{          
    protected void Page_PreRender(object sender, EventArgs e)
    {
        ScriptManager.RegisterStartupScript(this, GetType(), ClientID, "alert(1)", true);
    }
}
陌若浮生 2024-07-16 06:46:08

如果您的控件嵌套在 asp.net 模式弹出窗口中,则将 RegisterStartupScript 放入 Page_PreRender 事件中可能不起作用。 相反,当 IsPostBack = False 时将其放入 Page_Load 中。

Putting the RegisterStartupScript in the Page_PreRender event may not work if your control is nested in an asp.net modal popup. Instead, put it in the Page_Load when IsPostBack = False.

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