ASP.NET中的RegisterStartupScript()不起作用

时间:2017-05-23 17:51:27

标签: javascript c# asp.net

这不是重复,我已经阅读并尝试了以下所有建议:

  1. RegisterStartupScript doesn't work with ScriptManager,Updatepanel. Why is that?
  2. https://msdn.microsoft.com/en-us/library/asz8zsxy(v=vs.110).aspx
  3. ScriptManager.RegisterStartupScript code not working - why?
  4. Page.ClientScript.RegisterStartupScript doesn't work - why?
  5. RegisterStartupScript doesn't appear to be working on page postback within update panel
  6. https://codewala.net/2011/11/24/page-clientscript-registerstartupscript-is-not-working/
  7. 我已经尝试过上述论坛主题和文章中的大量排列和实验,并且在任何情况下我的javascript都无法注册和执行。我试过查看源代码 - 我的脚本没有出现。作为一个完整性检查,我将我的javascript注入ASP.NET Literal控件,并且工作正常,我的js执行。

    这是我的ASP.NET页面,非常简单:

    {
      "rows": [
        {
          "key": "201705",
          "value": 8
        },
        {
          "key": "201706",
          "value": 1
        }
      ]
    }
    

    我的代码隐藏:

    <%@ Page language="c#" Trace="false" EnableSessionState="true" EnableViewState="false" AutoEventWireup="false" Codebehind="PopOut.aspx.cs" Inherits="MySystem.MPC.WebComponents.Configurator.UI.PopOut" %>
    <!DOCTYPE html>
    <html>
    <head>
       <base target="_self" />
       <title>MySystem Test</title>
       <link type="text/css" rel="stylesheet" href="site/styles/Common/Core.css" />
       <script type="text/javascript" src="scripts/JSUI.js"></script>
    </head>
    <body onload="loadWindow();" onunload="unloadWindow();">
        <div class="xCfg" popout>
          <div class="xTb" id="POPOUP_TOOLBAR">
             <div class="xTbBtns">
                <a id="b12" onclick="return expTbClick(event, 12);" href="">
                   <img src=""><span></span>
                </a>    
                <a id="b14" onclick="return expTbClick(event, 14);" href="">
                   <img src="">
                </a>
             </div>
          </div>
    
    
          <div class="xBody" id="POPOUT_BODY" scrollable>
             <div class="xPage" id="bodyContent" notitle>
                <div class="xPopOutLargeText" id="loading"></div>
                <div class="xPopOutLargeText" id="allHidden" style="display: none;"></div>
             </div>
          </div>
          <div class="xSb" id="POPOUP_STATUSBAR"></div>
       </div>
    </body>
    </html>
    

    另外,我尝试添加using System; using System.Linq; using System.Text; using System.Web.UI; using System.Web.UI.WebControls; namespace MySystem.UI { public class PopOut : ConfigPageBase { protected Literal uiStyleLink; protected Literal litScript; protected override void OnLoad(EventArgs e) { base.OnLoad(e); uiStyleLink.Text = string.Format("<link href=\"/experlogix/site/styles/{0}/{0}.css?{1}\" rel=\"stylesheet\" type=\"text/css\">", ExpSettings.Site.UserInterface.Theme, GlobalState.StaticResourceVarianceParameter); } protected override void OnPreRender( EventArgs e ) { //base.OnPreRender(e); this.ClientScript.RegisterClientScriptBlock(this.GetType(), "key1", "<script>alert('testing');</script>"); [and various other approaches as per posts and articles above] } } } (加上结束时),但没有任何区别。

    有什么想法吗?

1 个答案:

答案 0 :(得分:0)

您需要form runat="server"来实现这一目标:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="44141788.aspx.cs" Inherits="AspNetDemoProject.ExternalCode._44141788" %>

<!DOCTYPE html>
<html>
<head>
    <base target="_self" />
    <title>MySystem Test</title>
    <link type="text/css" rel="stylesheet" href="site/styles/Common/Core.css" />
    <script type="text/javascript" src="scripts/JSUI.js"></script>
</head>
<body onload="loadWindow();" onunload="unloadWindow();">
    <form runat="server">
        <div class="xCfg" popout>
            <div class="xTb" id="POPOUP_TOOLBAR">
                <div class="xTbBtns">
                    <a id="b12" onclick="return expTbClick(event, 12);" href="">
                        <img src=""><span></span>
                    </a>
                    <a id="b14" onclick="return expTbClick(event, 14);" href="">
                        <img src="">
                    </a>
                </div>
            </div>


            <div class="xBody" id="POPOUT_BODY" scrollable>
                <div class="xPage" id="bodyContent" notitle>
                    <div class="xPopOutLargeText" id="loading"></div>
                    <div class="xPopOutLargeText" id="allHidden" style="display: none;"></div>
                </div>
            </div>
            <div class="xSb" id="POPOUP_STATUSBAR"></div>
        </div>
    </form>
</body>
</html>

代码旁边:

public partial class _44141788 : System.Web.UI.Page
{
    protected Literal uiStyleLink;
    protected Literal litScript;

    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);

    }


    protected void Page_PreRender(object sender, EventArgs e)
    {
        ClientScript.RegisterStartupScript(GetType(),  "alert", "alert('hey');", true);

    }
}

源代码: https://github.com/kblok/StackOverflowExamples/blob/master/AspNetDemoProject/AspNetDemoProject/ExternalCode/44141788.aspx

https://github.com/kblok/StackOverflowExamples/blob/master/AspNetDemoProject/AspNetDemoProject/ExternalCode/44141788.aspx.cs