简体   繁体   English

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

[英]RegisterStartupScript() in ASP.NET doesn't work

This isn't a duplicate, I have already read and tried all suggestions from the following: 这不是重复的内容,我已经阅读并尝试了以下所有建议:

  1. RegisterStartupScript doesn't work with ScriptManager,Updatepanel. RegisterStartupScript不适用于ScriptManager,Updatepanel。 Why is that? 这是为什么?
  2. https://msdn.microsoft.com/en-us/library/asz8zsxy(v=vs.110).aspx https://msdn.microsoft.com/en-us/library/asz8zsxy(v=vs.110).aspx
  3. ScriptManager.RegisterStartupScript code not working - why? ScriptManager.RegisterStartupScript代码不起作用-为什么?
  4. Page.ClientScript.RegisterStartupScript doesn't work - why? Page.ClientScript.RegisterStartupScript不起作用-为什么?
  5. RegisterStartupScript doesn't appear to be working on page postback within update panel RegisterStartupScript似乎在更新面板中的页面回发上不起作用
  6. https://codewala.net/2011/11/24/page-clientscript-registerstartupscript-is-not-working/ https://codewala.net/2011/11/24/page-clientscript-registerstartupscript-is-not-working/

I have tried numerous permutations and experiments from the above mentioned forum topics and articles, and in no case does my javascript get registered and executed. 我已经尝试了上述论坛主题和文章的许多排列和实验,并且在任何情况下都不会注册并执行我的JavaScript。 I have tried viewing source - my script doesn't appear. 我尝试查看源代码-我的脚本没有出现。 As a sanity check I injected my javascript into an ASP.NET Literal control, and that works fine, and my js executes. 作为一个健全性检查,我将JavaScript注入了ASP.NET Literal控件中,并且工作正常,并且我的js得以执行。

Here is my ASP.NET page, pretty simple: 这是我的ASP.NET页面,非常简单:

<%@ 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>

And my codebehind: 而我的代码背后:

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]

      }

   }
}

Also, I tried adding a <form runat="server"> (plus closing at end) but it made no difference. 另外,我尝试添加<form runat="server"> (最后加上结束),但这没什么区别。

Any ideas? 有任何想法吗?

You need a form with runat="server" to get that working: 您需要一个带有runat="server"form才能正常工作:

<%@ 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>

Code Beside: 旁边的代码:

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);

    }
}

Source Code: https://github.com/kblok/StackOverflowExamples/blob/master/AspNetDemoProject/AspNetDemoProject/ExternalCode/44141788.aspx 源代码: 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 https://github.com/kblok/StackOverflowExamples/blob/master/AspNetDemoProject/AspNetDemoProject/ExternalCode/44141788.aspx.cs

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM