【问题标题】:problem adding javascript to control添加javascript来控制的问题
【发布时间】:2011-05-04 13:39:36
【问题描述】:

我在 .cs 页面中使用这个脚本..

public void messagebox(string msg)
{
  Label lbl = new Label();
  lbl.Text = "<script language='javascript'>" + Environment.NewLine + "window.alert('" + msg + "')</script>";
  Page.Controls.Add(lbl);
}

错误为

无法修改 Controls 集合,因为该控件包含代码块(即 )。

【问题讨论】:

  • 在继续提问之前,您应该接受对之前问题的更多回答。
  • 最初我查看并看到许多未回答的问题,但随后在 24 个问题中,很多问题已得到回答,至少有一些值得接受
  • Javascript Alert in ASP.NET 的可能重复项

标签: c# javascript


【解决方案1】:

要注册脚本,请使用 ScriptManager

ScriptManager.RegisterStartupScript(this, this.GetType(), "registeredAlert", lbl, false);

【讨论】:

    【解决方案2】:

    要在页面上注册脚本,请使用RegisterStartupScript

    ScriptManager.RegisterStartupScript Method (Control, Type, String, String, Boolean)
    

    如果您使用的是更新面板,则必须使用更新面板控件调用前两个参数

    ScriptManager.RegisterStartupScript(updatePanel,
        updatePanel.GetType(),
        "key",            // unique key means it will never insert the same script twice
        "alert('hi');",   // javascript
        true);            // include <script> tags
    

    【讨论】:

      【解决方案3】:

      这与 Javascript 无关。错误信息很清楚;不允许在代码中此时向 Page.Controls 添加更多控件。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-07-26
        • 1970-01-01
        • 2021-07-19
        • 2013-10-11
        • 1970-01-01
        • 2017-11-27
        • 2012-05-15
        相关资源
        最近更新 更多