onclick Client-side Code problem

G

Guest

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("MyJavaScriptFile",
"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
 
G

Guest

Mark Rae said:
How are you including the JavaScript file in the MasterPage...?

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

Mark Rae

// =====================
// MasterPage.master.cs
// =====================
protected void Page_Load(object sender, EventArgs e)
{
ClientScript.RegisterClientScriptInclude("MyJavaScriptFile",
"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...
 
G

Guest

Mark Rae said:
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.
 
G

Guest

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
 
M

Mark Rae

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...?
 
G

Guest

Mark Rae said:
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
 
M

Mark Rae

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!
 
G

Guest

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
 
G

Guest

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
 
M

Mark Rae

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

Guest

Well, at least it's "nice" to have confirmation from somebody else that it's
not just me...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,740
Latest member
AdolphBig6

Latest Threads

Top