Codebehind Call Javascript Funtions Contained in Central js file?

Posted on 2009-02-13
Medium Priority
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
  • 4
LVL 11

Accepted Solution

jmwheeler earned 800 total points
ID: 23636357
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

jmwheeler earned 800 total points
ID: 23636397

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

CB_Thirumalai earned 200 total points
ID: 23640787
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.
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

LVL 11

Expert Comment

ID: 23659319
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

ID: 23661973
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

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

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses
Course of the Month13 days, 12 hours left to enroll

755 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