473,569 Members | 2,688 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.ClientScri pt.RegisterStar tupScript(this. GetType(),"disp laymessage",
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(objec t sender, EventArgs e)

{

try

{

string temp = "displaymessage ();";
HtmlGenericCont rol body =
(HtmlGenericCon trol)Master.Fin dControl("Body" );

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

}

catch (NullReferenceE xception 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(objec t sender, EventArgs e)

{

try

{

string temp = "alert('Hel lo World!');";

HtmlGenericCont rol body =
(HtmlGenericCon trol)Master.Fin dControl("Body" );

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

}

catch (NullReferenceE xception x)

{

Response.Write( x);

}

}

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

Have u tried to use RegisterClientS criptInclude ?

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.ClientScr ipt.RegisterSta rtupScript(this .GetType(),"dis playmessag
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="JavaS cript" 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(objec t 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 ClientScriptMan ager reference from the Page class.

ClientScriptMan ager cs = Page.ClientScri pt;

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

if (!cs.IsClientSc riptIncludeRegi stered(cstype, csname))

{

cs.RegisterClie ntScriptInclude (cstype, csname, ResolveClientUr l(csurl));

}

string temp = "displaymessage ()";

HtmlGenericCont rol body = (HtmlGenericCon trol)Master.Fin dControl("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.dt d">

<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="JavaS cript" src="common/js/global.js"
type="text/JavaScript"></script>
</head>
<body id="ctl00_body " onload="display message()">
<form name="aspnetFor m" method="post" action="test.as px"
id="aspnetForm" >
<div>.

..

..

"Michael Nemtsev" <ne*****@msn.co mwrote in message
news:17******** *************** ****@msnews.mic rosoft.com...
Hello Frank,

Have u tried to use RegisterClientS criptInclude ?

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.ClientScr ipt.RegisterSta rtupScript(this .GetType(),"dis playmessag
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.dt d">
public void Page_Load(Objec t sender, EventArgs e)
{
// Define the name, type and url of the client script on the page.
String csname = "ButtonClickScr ipt";
String csurl = "~/script_include. js";
Type cstype = this.GetType();

// Get a ClientScriptMan ager reference from the Page class.
ClientScriptMan ager cs = Page.ClientScri pt;

// Check to see if the include script exists already.
if (!cs.IsClientSc riptIncludeRegi stered(cstype, csname))
{
cs.RegisterClie ntScriptInclude (cstype, csname,
ResolveClientUr l(csurl));
}

//for the body onload functionality
string temp = "displaymessage ()";
HtmlGenericCont rol body =
(HtmlGenericCon trol)Master.Fin dControl("Body" );
body.Attributes .Add("onload", temp);

}
<html >
<head>
<title>ClientSc riptManager Example</title>
</head>
<body>
<form id="Form1" runat="server">
<div>
<input type="text"
id="Message"/>
<input type="button"
value="ClickMe"
onclick="DoClic k()"/>
</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
9834
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 | ios::binary) to declare a file object with read/write modes turned on for working with binary data. I've tried this and my file is not created. The only...
3
24010
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 tables in the database where the code resides. If we move the database with the data tables to a new directory, the links are no longer valid. I...
11
6566
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 where the job is running, the job runs sucessfully, PDF files got generated, everything is good. If I scheduled the job to run at the time that I am...
15
4734
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 button will verify the information that has been entered and updates the data base if the data is correct. Update will throw an exception if the data is...
3
8236
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 build with these methods, will appear to encrypt and decrypt, but the resulting decrypted file will be corrupted. I tried encrypting a .bmp file and...
21
34360
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 obvious of which is the sharing of files. For example, you upload images to a server to share them with other people over the Internet. Perl comes ready...
1
4405
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 would give it a try. First of all, I am using Visual Studio 2005 to write my program. I am using C# .NET as the language. I am running Windows XP...
8
18237
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. This code works fine by taking file(s) from disk and creating a zip file to disk. I need to modify this code to read a file from a byte array...
221
367160
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 needs to store entire files, the preferred method is to save the file onto the server’s file-system, and store the physical location of the file in...
7
2810
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 seems to work fine, however I noticed that no web log entry is added when a successful download occurs (normally a 200 HTTP status code, however, if...
0
7926
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7679
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7983
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5514
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5223
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3657
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3647
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2117
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
946
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.