[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 345
  • Last Modified:

How do I execute javascript from code behind in aspx

I have a javascript function in my aspx page
    <script type="text/javascript" language="javascript">

        function showDialogue() {alert ("It worked"); }

And I have this in my code behind at the form load
Page.ClientScript.RegisterStartupScript(Page.GetType(), "Key", "showDialogue", True)

A response.write proves to me that the form load is being run, but the alert doesn't show

What am I doing wrong?
  • 2
2 Solutions
Not fully conversant with .net but calling functions in js requires () when used i.e.

Page.ClientScript.RegisterStartupScript(Page.GetType(), "Key", "showDialogue()", True)
käµfm³d 👽Commented:
Gary's got your answer. I'll expand on why  = )

The RegisterStartupScript method's third argument is the actual script code you want to run. You could just as easily put your alert code in that parameter. Since you instead put the script code in your .aspx page, you simply need to pass a call to that function--what Gary mentioned--as your script. Adding the parens (and probably a semi-colon for good measure) should cause it to execute.
g_johnsonAuthor Commented:
kaufmed --

that's interesting.  This was just a small test for what will be a much more complex function:

        function CS15922_SysPrintSelect(division, page, onchangeScript, ctl_no, item_no, qty)

            icsysDivision = division;

            var args;
            var u = "CS15922_PrintSelect.aspx?Division=" + icsysDivision + "&Id=" + id + "&@endnum=" + qty + "&bpEM=" + byrPlnrEmail + "&updateNumber=1";

            SysDialog.ctl = null;
            SysDialog.bKeyInRef = null;
            SysDialog.refurl = page;
            SysDialog.onchangeScript = onchangeScript;
            SysDialog.extraResults = "";
            SysShowModal(u, args, "850px", "600px", "CS15922_SysPrintSelectHandler();", true);


Am I understanding you to say that I could put each of the commands in the function into the third parameter of the RegisterStartupScript separated by semicolons and it would run just fine?

Due to the complexity I believe I'll leave it as a function, but I just wanted to be sure I understand.

Thanks to both of you!
käµfm³d 👽Commented:
In theory, yes that should work. I'm only going by what the docs say. I haven't really used RegisterStartupScript. I usually stuff everything into the .aspx (well, .cshtml file now, since almost all of our work is in MVC). For things to run at startup, we usually use JQuery.

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now