473,439 Members | 1,733 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,439 software developers and data experts.

Using js file in asp.net 2.0

Hi,

I simply wish to code a JS function ONCE, in a .js file, include the .js
file in the Master page, and then in several but not all pages, I wish to
execute the function on pageload.

I don't want to use the
Page.ClientScript.RegisterStartupScript(this.GetTy pe(),"displaymessage",
strScript, false);

method because this seemingly requires me to code the JScript on all the
pages (code behind) where I want to run the function, which is stupid.

I think what I want to use is something like :

Designate the body tag as :

<body runat="server" id="body">

and in the code behind for a particular page:

protected void Page_Load(object sender, EventArgs e)

{

try

{

string temp = "displaymessage();";
HtmlGenericControl body =
(HtmlGenericControl)Master.FindControl("Body");

body.Attributes.Add("onload", temp);

}

catch (NullReferenceException x)

{

Response.Write(x);

}

}

but this give a JS error where it says 'Object Required' where the body tag
is in the rendered html

It does work if I do:

protected void Page_Load(object sender, EventArgs e)

{

try

{

string temp = "alert('Hello World!');";

HtmlGenericControl body =
(HtmlGenericControl)Master.FindControl("Body");

body.Attributes.Add("onload", temp);

}

catch (NullReferenceException x)

{

Response.Write(x);

}

}

So what is wrong?
Dec 7 '06 #1
3 20879
Hello Frank,

Have u tried to use RegisterClientScriptInclude ?

FI simply wish to code a JS function ONCE, in a .js file, include the
F.js file in the Master page, and then in several but not all pages, I
Fwish to execute the function on pageload.
F>
FI don't want to use the
FPage.ClientScript.RegisterStartupScript(this.GetT ype(),"displaymessag
Fe", strScript, false);
F>
Fmethod because this seemingly requires me to code the JScript on all
Fthe pages (code behind) where I want to run the function, which is
Fstupid.

---
WBR,
Michael Nemtsev [C# MVP] :: blog: http://spaces.live.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
Dec 7 '06 #2
Thanks for your repsonse. I have tried this:

Created a file name global.js which contains:
// JScript File

function displaymessage()

{

alert("Hello World!")

}

Then in the Master page I did like so:

<head runat="server">

<script language="JavaScript" src="common/js/global.js"
type="text/JavaScript"></script>

</head>

<body runat="server" id="body"

Then in one of the pages in the site I did like so:

protected void Page_Load(object sender, EventArgs e)

{

// Define the name, type and url of the client script on the page.

String csname = "PageLoadScript";

String csurl = "~/common/gjs/global.js";

Type cstype = this.GetType();

// Get a ClientScriptManager reference from the Page class.

ClientScriptManager cs = Page.ClientScript;

// Check to see if the include script exists already.

if (!cs.IsClientScriptIncludeRegistered(cstype, csname))

{

cs.RegisterClientScriptInclude(cstype, csname, ResolveClientUrl(csurl));

}

string temp = "displaymessage()";

HtmlGenericControl body = (HtmlGenericControl)Master.FindControl("Body");

body.Attributes.Add("onload", temp);

}

The page starts up but a JS error occurs (Object Expected) and point to the
body tag line... The rendered HTML starts like so:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head><title>
Untitled Page
</title><link href="../common/css/admin_style.css" rel="stylesheet"
type="text/css" />
<script language="JavaScript" src="common/js/global.js"
type="text/JavaScript"></script>
</head>
<body id="ctl00_body" onload="displaymessage()">
<form name="aspnetForm" method="post" action="test.aspx"
id="aspnetForm">
<div>.

..

..

"Michael Nemtsev" <ne*****@msn.comwrote in message
news:17***************************@msnews.microsof t.com...
Hello Frank,

Have u tried to use RegisterClientScriptInclude ?

FI simply wish to code a JS function ONCE, in a .js file, include the
F.js file in the Master page, and then in several but not all pages, I
Fwish to execute the function on pageload.
FFI don't want to use the
FPage.ClientScript.RegisterStartupScript(this.GetT ype(),"displaymessag
Fe", strScript, false);
FFmethod because this seemingly requires me to code the JScript on all
Fthe pages (code behind) where I want to run the function, which is
Fstupid.

---
WBR,
Michael Nemtsev [C# MVP] :: blog: http://spaces.live.com/laflour

"At times one remains faithful to a cause only because its opponents do
not cease to be insipid." (c) Friedrich Nietzsche


Dec 7 '06 #3
OK, I got it:

<%@ Page Language="C#"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
public void Page_Load(Object sender, EventArgs e)
{
// Define the name, type and url of the client script on the page.
String csname = "ButtonClickScript";
String csurl = "~/script_include.js";
Type cstype = this.GetType();

// Get a ClientScriptManager reference from the Page class.
ClientScriptManager cs = Page.ClientScript;

// Check to see if the include script exists already.
if (!cs.IsClientScriptIncludeRegistered(cstype, csname))
{
cs.RegisterClientScriptInclude(cstype, csname,
ResolveClientUrl(csurl));
}

//for the body onload functionality
string temp = "displaymessage()";
HtmlGenericControl body =
(HtmlGenericControl)Master.FindControl("Body");
body.Attributes.Add("onload", temp);

}
<html >
<head>
<title>ClientScriptManager Example</title>
</head>
<body>
<form id="Form1" runat="server">
<div>
<input type="text"
id="Message"/>
<input type="button"
value="ClickMe"
onclick="DoClick()"/>
</div>
</form>
</body>
</html>

This example requires a JavaScript file named Script_include.js with the
following contents:
function DoClick() {Form1.Message.value='Text from include script.'}
function displaymessage()
{
alert('Hello World!');
}
*** add <body runat="server" id="body"to masterpage for the pageload
functionality
Dec 7 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Brandon McCombs | last post by:
This may be the wrong group but I didn't see anything for VC++ so I'm trying here. I have a C++ book by Deitel and Deitel that says I can use fstream File("data.dat", ios::in | ios::out |...
3
by: Random Person | last post by:
Does anyone know how to use VBA to relink tables between two MS Access databases? We have two databases, one with VBA code and the other with data tables. The tables are referenced by linked...
11
by: Grasshopper | last post by:
Hi, I am automating Access reports to PDF using PDF Writer 6.0. I've created a DTS package to run the reports and schedule a job to run this DTS package. If I PC Anywhere into the server on...
15
by: Nathan | last post by:
I have an aspx page with a data grid, some textboxes, and an update button. This page also has one html input element with type=file (not inside the data grid and runat=server). The update...
3
by: JDeats | last post by:
I have some .NET 1.1 code that utilizes this technique for encrypting and decrypting a file. http://support.microsoft.com/kb/307010 In .NET 2.0 this approach is not fully supported (a .NET 2.0...
21
KevinADC
by: KevinADC | last post by:
Note: You may skip to the end of the article if all you want is the perl code. Introduction Uploading files from a local computer to a remote web server has many useful purposes, the most...
1
by: WeCi2i | last post by:
Okay, I have a problem that has been stumping me for weeks. I have tried many different solutions and this is pretty much my last resort. I have seen a lot of good answers give here so I figured I...
8
by: =?Utf-8?B?Q2hyaXMgRmluaw==?= | last post by:
I am trying to make a minor modification to the code below and need some assistance. Currently this code is using the java.util, java.util.zip, and java.io assemblies from the vjslib.dll assembly....
221
Atli
by: Atli | last post by:
You may be wondering why you would want to put your files “into” the database, rather than just onto the file-system. Well, most of the time, you wouldn’t. In situations where your PHP application...
7
by: =?Utf-8?B?QU9UWCBTYW4gQW50b25pbw==?= | last post by:
Hi, I have been using the code (some of it has been removed for simplicity) below to allow authenticated (using ASP.NET membership database) users to get a file from their archive area. It...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.