473,503 Members | 1,818 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

onclick Client-side Code problem

Hello All--

I'm having a problem getting an onclick event to work. Here is my setup: I
have an external JavaScript file, a master page that registers the javascript
as an external file, and a content page that has controls that I am trying to
get onclick to work for.

// =====================
// MyScriptFile.js
// =====================
function ReportOnClick(msg)
{
alert('ReportOnClick: ' + msg);
}

// =====================
// MasterPage.master.cs
// =====================
protected void Page_Load(object sender, EventArgs e)
{
ClientScript.RegisterClientScriptInclude("MyJavaSc riptFile",
"MyScriptFile.js");
}

// =====================
// ContentPage.aspx.cs
// =====================
protected void Page_Load(object sender, EventArgs e)
{
if (! IsPostback)
{
// Does not work
Label1.Attributes.Add("onclick", "ReportOnClick('Label1 was
clicked.');");

// Does not work
// Label1.Attributes.Add("onclick",
"javascript:ReportOnClick('Label1 was clicked.');");

// Does work
// Label1.Attributes.Add("onclick", "alert('Label1 was clicked.');");
}
}

So, the adding of the attribute works fine, as I get an alert if I hardcode
one in. However, whenever I try to access a function in my external script,
I get a "Microsoft JScript runtime error: Object expected".

Any ideas?

Thanks,
PAGates

Oct 4 '06 #1
13 2363
"pagates" <pa*****@discussions.microsoft.comwrote in message
news:9A**********************************@microsof t.com...
Any ideas?
There was a similar problem discussed in another thread recently...

How are you including the JavaScript file in the MasterPage...?
Oct 4 '06 #2
"Mark Rae" wrote:
How are you including the JavaScript file in the MasterPage...?
// =====================
// MasterPage.master.cs
// =====================
protected void Page_Load(object sender, EventArgs e)
{
ClientScript.RegisterClientScriptInclude("MyJavaSc riptFile",
"MyScriptFile.js");
}

Oct 4 '06 #3
"pagates" <pa*****@discussions.microsoft.comwrote in message
news:D2**********************************@microsof t.com...
"Mark Rae" wrote:
>How are you including the JavaScript file in the MasterPage...?

// =====================
// MasterPage.master.cs
// =====================
protected void Page_Load(object sender, EventArgs e)
{
ClientScript.RegisterClientScriptInclude("MyJavaSc riptFile",
"MyScriptFile.js");
}
Hmm - yes, I think I see the problem...

Can you please do a View Source on the page and show the markup which the
above code has generated i.e. the "<include..." line in the header...
Oct 4 '06 #4
You're using the wrong format for the include. This format doesn't include a
file, but includes the code instead. The version you want takes three
parameters, not two. Check out the sample at:
http://msdn2.microsoft.com/en-us/library/kx145dw2.aspx
--
Hope this helps,
Mark Fitzpatrick
Former Microsoft FrontPage MVP 199?-2006

"pagates" <pa*****@discussions.microsoft.comwrote in message
news:9A**********************************@microsof t.com...
Hello All--

I'm having a problem getting an onclick event to work. Here is my setup:
I
have an external JavaScript file, a master page that registers the
javascript
as an external file, and a content page that has controls that I am trying
to
get onclick to work for.

// =====================
// MyScriptFile.js
// =====================
function ReportOnClick(msg)
{
alert('ReportOnClick: ' + msg);
}

// =====================
// MasterPage.master.cs
// =====================
protected void Page_Load(object sender, EventArgs e)
{
ClientScript.RegisterClientScriptInclude("MyJavaSc riptFile",
"MyScriptFile.js");
}

// =====================
// ContentPage.aspx.cs
// =====================
protected void Page_Load(object sender, EventArgs e)
{
if (! IsPostback)
{
// Does not work
Label1.Attributes.Add("onclick", "ReportOnClick('Label1 was
clicked.');");

// Does not work
// Label1.Attributes.Add("onclick",
"javascript:ReportOnClick('Label1 was clicked.');");

// Does work
// Label1.Attributes.Add("onclick", "alert('Label1 was
clicked.');");
}
}

So, the adding of the attribute works fine, as I get an alert if I
hardcode
one in. However, whenever I try to access a function in my external
script,
I get a "Microsoft JScript runtime error: Object expected".

Any ideas?

Thanks,
PAGates

Oct 4 '06 #5
"Mark Rae" wrote:
"pagates" <pa*****@discussions.microsoft.comwrote in message
news:D2**********************************@microsof t.com...
"Mark Rae" wrote:
How are you including the JavaScript file in the MasterPage...?
// =====================
// MasterPage.master.cs
// =====================
protected void Page_Load(object sender, EventArgs e)
{
ClientScript.RegisterClientScriptInclude("MyJavaSc riptFile",
"MyScriptFile.js");
}

Hmm - yes, I think I see the problem...

Can you please do a View Source on the page and show the markup which the
above code has generated i.e. the "<include..." line in the header...
The following is in the body (not the header):
<script src="MyScriptFile.js" type="text/javascript"></script>

Note: there are other functions in that file that are called correctly, but
they are during the window.onload and window.onresize events, which are coded
into the javascript file. It seems to be a problem specifically with onclick.

Oct 4 '06 #6
Hi Mark,

Using either call for the function results in the same code in the body:

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

Thanks,
PAGates

"Mark Fitzpatrick" wrote:
You're using the wrong format for the include. This format doesn't include a
file, but includes the code instead. The version you want takes three
parameters, not two. Check out the sample at:
http://msdn2.microsoft.com/en-us/library/kx145dw2.aspx
--
Hope this helps,
Mark Fitzpatrick
Former Microsoft FrontPage MVP 199?-2006

"pagates" <pa*****@discussions.microsoft.comwrote in message
news:9A**********************************@microsof t.com...
Hello All--

I'm having a problem getting an onclick event to work. Here is my setup:
I
have an external JavaScript file, a master page that registers the
javascript
as an external file, and a content page that has controls that I am trying
to
get onclick to work for.

// =====================
// MyScriptFile.js
// =====================
function ReportOnClick(msg)
{
alert('ReportOnClick: ' + msg);
}

// =====================
// MasterPage.master.cs
// =====================
protected void Page_Load(object sender, EventArgs e)
{
ClientScript.RegisterClientScriptInclude("MyJavaSc riptFile",
"MyScriptFile.js");
}

// =====================
// ContentPage.aspx.cs
// =====================
protected void Page_Load(object sender, EventArgs e)
{
if (! IsPostback)
{
// Does not work
Label1.Attributes.Add("onclick", "ReportOnClick('Label1 was
clicked.');");

// Does not work
// Label1.Attributes.Add("onclick",
"javascript:ReportOnClick('Label1 was clicked.');");

// Does work
// Label1.Attributes.Add("onclick", "alert('Label1 was
clicked.');");
}
}

So, the adding of the attribute works fine, as I get an alert if I
hardcode
one in. However, whenever I try to access a function in my external
script,
I get a "Microsoft JScript runtime error: Object expected".

Any ideas?

Thanks,
PAGates


Oct 4 '06 #7
"pagates" <pa*****@discussions.microsoft.comwrote in message
news:50**********************************@microsof t.com...
>Can you please do a View Source on the page and show the markup which the
above code has generated i.e. the "<include..." line in the header...

The following is in the body (not the header):
<script src="MyScriptFile.js" type="text/javascript"></script>
Indulge me...

1) Temporarily comment out the server-side code which adds the include file

2) Hard-code the above markup into the MasterPage, but with one tiny
modification:

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

Note the extra space at the end of "text/javascript "

Does it work now...?
Oct 4 '06 #8


"Mark Rae" wrote:
"pagates" <pa*****@discussions.microsoft.comwrote in message
news:50**********************************@microsof t.com...
Can you please do a View Source on the page and show the markup which the
above code has generated i.e. the "<include..." line in the header...
The following is in the body (not the header):
<script src="MyScriptFile.js" type="text/javascript"></script>

Indulge me...

1) Temporarily comment out the server-side code which adds the include file

2) Hard-code the above markup into the MasterPage, but with one tiny
modification:

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

Note the extra space at the end of "text/javascript "

Does it work now...?
No, but....

Hard-coding the markup (without the space) seems to allow it to work
correctly.

Or should that be spelled "correctly?!" So, it looks like there is a
problem somewhere in the mechanism with the RegisterClientSideInclude
function when specifying a javascript event with the same name as a
server-side event.

I'm not sure why I didn't think of trying that myself earlier, but I
didn't... Thank you!

Thanks again,
PAGates
Oct 4 '06 #9
"pagates" <pa*****@discussions.microsoft.comwrote in message
news:49**********************************@microsof t.com...
>Does it work now...?

No, but....
Damn! Scroll down to the thread entitled "menu controls on 2.0",
specifically the last few posts - I was hoping this might be the same
thing...
Hard-coding the markup (without the space) seems to allow it to work
correctly.
Hmm - there's "definitely" something not quite right with include files in
MasterPages...
I'm not sure why I didn't think of trying that myself earlier, but I
didn't... Thank you!
Welcome!
Oct 4 '06 #10
Wow. Damn is right! : )

I've done the Submit a Bug thing through the VS feedback. We'll see what
happens.

Thanks again for the help.

PAGates

"Mark Rae" wrote:
"pagates" <pa*****@discussions.microsoft.comwrote in message
news:49**********************************@microsof t.com...
Does it work now...?
No, but....

Damn! Scroll down to the thread entitled "menu controls on 2.0",
specifically the last few posts - I was hoping this might be the same
thing...
Hard-coding the markup (without the space) seems to allow it to work
correctly.

Hmm - there's "definitely" something not quite right with include files in
MasterPages...
I'm not sure why I didn't think of trying that myself earlier, but I
didn't... Thank you!

Welcome!
Oct 4 '06 #11
Wait a minute. I stand (un)corrected. I am still having the same problem
with the hard-coded page (although, by God, it did work once, for some
"random" reason).

PAGates

"pagates" wrote:
Wow. Damn is right! : )

I've done the Submit a Bug thing through the VS feedback. We'll see what
happens.

Thanks again for the help.

PAGates

"Mark Rae" wrote:
"pagates" <pa*****@discussions.microsoft.comwrote in message
news:49**********************************@microsof t.com...
>Does it work now...?
>
No, but....
Damn! Scroll down to the thread entitled "menu controls on 2.0",
specifically the last few posts - I was hoping this might be the same
thing...
Hard-coding the markup (without the space) seems to allow it to work
correctly.
Hmm - there's "definitely" something not quite right with include files in
MasterPages...
I'm not sure why I didn't think of trying that myself earlier, but I
didn't... Thank you!
Welcome!

Oct 4 '06 #12
"pagates" <pa*****@discussions.microsoft.comwrote in message
news:11**********************************@microsof t.com...
Wait a minute. I stand (un)corrected. I am still having the same problem
with the hard-coded page (although, by God, it did work once, for some
"random" reason).
There's clearly something wrong here - e,g, MasterPages won't even process a
self-closing <script /tag - has to be <script></script>
Oct 4 '06 #13
Well, at least it's "nice" to have confirmation from somebody else that it's
not just me...
"Mark Rae" wrote:
"pagates" <pa*****@discussions.microsoft.comwrote in message
news:11**********************************@microsof t.com...
Wait a minute. I stand (un)corrected. I am still having the same problem
with the hard-coded page (although, by God, it did work once, for some
"random" reason).

There's clearly something wrong here - e,g, MasterPages won't even process a
self-closing <script /tag - has to be <script></script>
Oct 4 '06 #14

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

Similar topics

5
1542
by: Spank | last post by:
Hello, I have a thumbnail image with the onclick event. When clicked, a larger pic is displayed via a javascript funtion. When browsing the page, the client does not realize the image is...
3
12586
by: Curtis | last post by:
Sorry if this is the wrong newsgroup in which to post. We have an asp page on a webserver supporting asp pages. How can we run/call a function on this page using an onclick or other event from...
2
14788
by: Galsaba | last post by:
How can I send back an argumet with "onClick"? <a href=test.php onClick="checkLink ('Gallon')" > 1 Gallon</a> function checkLink(a) { return (a+3) } When the user click on the word "Gallon"...
5
7701
by: José Carlos | last post by:
Hi. I am trying send a text value when click in an image. If i write: onClick="javascript:document.client.submit();" send all form, but i want to send only a input type text (name = cod),...
17
4844
by: abs | last post by:
My element: <span onclick="alert('test')" id="mySpan">test</span> Let's say that I don't know what is in this span's onclick event. Is it possible to add another action to this element's onclick...
53
81657
by: usenet | last post by:
See <ul> <li><a name="link1" onClick="alert(this.name);return false;" href="#">Link1</a></li> <li><a name="link2" href="javascript:alert(this);">Link2</a></li> <li>Item 3</li> </ul> ...
4
2948
by: Fabrizio | last post by:
Hi I'm creating a web page where I use a rollover function on a html image. Can i associate the onclick event of this client control with a C# function resident on the aspx.cs file Thank you...
13
5625
by: Chris | last post by:
I can create Javascript confirm message boxes during page creation, etc adding them to the button attributes (many good posts on this!). But how can I add this event after the button is pressed? I...
6
1869
by: Dave Mennenoh | last post by:
I have a Flash based video player that uses a javaScript function on the page to modify variables within the Flash. This is so, external, jpg, thumbnails can change the video without reloading the...
4
3527
by: Jl_G_0 | last post by:
Hey all. hope someone can give me a little advice here. I have a GridView, and on each line of it there's an <asp:imagebuttonlike this: <asp:ImageButton ID="hlCancel" runat="server"...
0
7202
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7330
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
6991
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
7460
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
5578
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,...
1
5014
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...
0
4672
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...
0
3167
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...
0
1512
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 ...

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.