How do I execute javascript from code behind in aspx

Posted on 2014-08-07
Last Modified: 2014-08-07
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?
Question by:g_johnson
    LVL 58

    Accepted Solution

    Not fully conversant with .net but calling functions in js requires () when used i.e.

    Page.ClientScript.RegisterStartupScript(Page.GetType(), "Key", "showDialogue()", True)
    LVL 74

    Assisted Solution

    by:käµfm³d 👽
    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.
    LVL 4

    Author Comment

    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!
    LVL 74

    Expert Comment

    by:käµfm³d 👽
    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

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
    One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on could not pull conte…
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now