Codebehind Call Javascript Funtions Contained in Central js file?

Posted on 2009-02-13
Last Modified: 2012-05-06
I would like to be able to call several different javascripts from a central .js file in a central script folder. Looks like a nice tidy way to do.......

protected void Button1_Click(object sender, EventArgs e)
      call ~/script/JScript.js function MessageBoxAlert(textstring)

Question by:pointeman
    LVL 11

    Accepted Solution

    Create a javascript file in your project

    Reference the file in the head tag of your page/master page

       <script type="text/javascript" src="myjavascript.js"></script>

    Call the javascript from anywhere in your code.
    LVL 11

    Assisted Solution


    //javascript file
    function myAlert() {

    //HTML file
    <asp:Button OnClientClick="return myAlert();" runat="server" />

    //Code Behind if you have a ScriptManager control on your page
    ScriptManager.RegisterStartup(this, this.GetType(), "myKey", "myAlert();", true);

    //Code Behind if you don't have a ScriptManager
    ClientScript.RegisterClientScriptBlock(this.GetType(), "myKey", "myAlert();", true);
    LVL 12

    Assisted Solution

    You may also do it to add attribute to the Button control during the page load as,
         Button1.Attributes.Add("onclick", "MyFunction();");
    To just call a javascript function, you never need to go server-side to do that.
    LVL 11

    Expert Comment

    You never NEED to go server-side to call javascript but you may WANT to if you have some C#/VB code that you want to run first.

    Author Comment

    Okay, placed your example function in my master.js file, then performed drag-n-drop file onto the actual aspx page to obtain correct script file reference link like so. The <script src> located on the master.master page did not work probably because my page is located in another sub directory. Anyway this works...
    Q. What does "mykey" refer to?
    <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
    <script src="../scripts/master.js" type="text/javascript"></script>

     --- code ---

    protected void Button1_Click1(object sender, EventArgs e)
         ScriptManager.RegisterStartupScript(this, this.GetType(), "myKey", "myAlert();", true);
    LVL 11

    Expert Comment

    It is just a reference name given to the script block that you are registering.  Just provide some descriptive name.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    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…
    I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…
    This video discusses moving either the default database or any database to a new volume.

    728 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

    18 Experts available now in Live!

    Get 1:1 Help Now