0
votes

This isn't a duplicate, 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/

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?

1
You have not shown your use of RegisterStartupScript in your question. Your question needs to demonstrate your issue.mason
mason, I added that.HerrimanCoder

1 Answers

0
votes

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