0

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. 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.

Here is my ASP.NET page, pretty simple:

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

Any ideas?

Prior work

I have already read and tried all suggestions from the following:

  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/
halfer
  • 19,824
  • 17
  • 99
  • 186
HerrimanCoder
  • 6,835
  • 24
  • 78
  • 158

1 Answers1

0

You need a form with runat="server" to get that working:

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

hardkoded
  • 18,915
  • 3
  • 52
  • 64
  • kblok, I should have mentioned, I had already tried that and it made no difference. So I removed it again. – HerrimanCoder May 23 '17 at 19:42
  • @HerrimanCoder I've added the code beside plus the source code, is working locally – hardkoded May 23 '17 at 19:47
  • Thank you kblok but still no joy. I added the Page_PreRender() and set a breakpoint on the `ClientScript.RegisterStartupScript()` -- but the breakpoint is never hit. Execution *does* enter my `protected override void OnPreRender()` though. Something very strange in my environment apparently. – HerrimanCoder May 23 '17 at 20:23
  • @HerrimanCoder have you uncommented //base.OnPreRender(e);? Page needs that to work – hardkoded May 23 '17 at 20:28
  • kblock, I originally had it uncommented, it never worked. I just uncommented and tried again. Still no magic. Side note: *WHY* when I put at-sign followed by username does username disappear when I hit save? I see other people doing it and theirs doesn't disappear. – HerrimanCoder May 23 '17 at 21:19
  • With AutoEventWireup in true Page_PreRender will be called – hardkoded May 23 '17 at 21:20
  • AutoEventWireup was indeed set to false, so I changed it to true and put Page_PreRender back in the codebehind. It still doesn't get called. – HerrimanCoder May 23 '17 at 21:25