473,396 Members | 2,036 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,396 software developers and data experts.

Object Expected, Code Behind assignment of javascript

I've written a .js file and put the script in my usercontrol:
<script src="myscript.js" />

Added the attributes to my controls:
lbl.Attributes.Add("onclick", "DoWork(this);")
- Also tried "javascript:DoWork(this);"
- and "javascript:DoWork();" - to see if it was the "this"

I've also tried the RegisterClientScriptInclue() method to register
this script as well to no avail.

Where am I going wrong with this?

Dec 11 '07 #1
7 2024
"cfps.Christian" <ge*******@otc.eduwrote in message
news:95**********************************@s19g2000 prg.googlegroups.com...
I've written a .js file and put the script in my usercontrol:
<script src="myscript.js" />

Added the attributes to my controls:
lbl.Attributes.Add("onclick", "DoWork(this);")
- Also tried "javascript:DoWork(this);"
- and "javascript:DoWork();" - to see if it was the "this"

I've also tried the RegisterClientScriptInclue() method to register
this script as well to no avail.

Where am I going wrong with this?
Firstly, I'm assuming lbl is an <asp:Labelwebcontrol... If so, it will
render as an HTML <span>. Does it actually contain any text...?

Secondly, do this as a test:

<asp:Label ID="MyTestLabel" runat="server" Text="Click me" />

MyTestLabel.Attributes.Add("onclick", "alert('Hello');");

When you click the word "Hello", do you see the JavaScript alert...?

If you do, then change the onclick attribute to "DoWork(this);" and put the
alert at the top of the DoWork method. Do you see the alert now...?
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Dec 11 '07 #2
On Dec 11, 10:33 am, "Mark Rae [MVP]" <m...@markNOSPAMrae.netwrote:
"cfps.Christian" <ge0193...@otc.eduwrote in message

news:95**********************************@s19g2000 prg.googlegroups.com...
I've written a .js file and put the script in my usercontrol:
<script src="myscript.js" />
Added the attributes to my controls:
lbl.Attributes.Add("onclick", "DoWork(this);")
- Also tried "javascript:DoWork(this);"
- and "javascript:DoWork();" - to see if it was the "this"
I've also tried the RegisterClientScriptInclue() method to register
this script as well to no avail.
Where am I going wrong with this?

Firstly, I'm assuming lbl is an <asp:Labelwebcontrol... If so, it will
render as an HTML <span>. Does it actually contain any text...?

Secondly, do this as a test:

<asp:Label ID="MyTestLabel" runat="server" Text="Click me" />

MyTestLabel.Attributes.Add("onclick", "alert('Hello');");

When you click the word "Hello", do you see the JavaScript alert...?

If you do, then change the onclick attribute to "DoWork(this);" and put the
alert at the top of the DoWork method. Do you see the alert now...?

--
Mark Rae
ASP.NET MVPhttp://www.markrae.net
I've narrowed it down to it not finding the method. The alert in the
click event works, at the top of the method does not.

Also this is dynamic because I need to create a possibly endless list
and add all the properties to it as I add them. I click each line and
it give me a new line for the javascript error (same error though).
Dec 11 '07 #3
you have one of three errors:

1) DoWork spelled wrong, javascript is case senstive
2) myscript.js has a script tag inside
3) myscript.js is not in the same folder as the page loading the control
turn on javascript debugging in the browser and vs.

-- bruce (sqlwork.com)
"cfps.Christian" wrote:
I've written a .js file and put the script in my usercontrol:
<script src="myscript.js" />

Added the attributes to my controls:
lbl.Attributes.Add("onclick", "DoWork(this);")
- Also tried "javascript:DoWork(this);"
- and "javascript:DoWork();" - to see if it was the "this"

I've also tried the RegisterClientScriptInclue() method to register
this script as well to no avail.

Where am I going wrong with this?

Dec 11 '07 #4
Syntax wise everything is correct, I copy the method names to ensure
that. Also I can get other files to work fine but this is the first
time with the UserControl/Dynamic script thing which is where I'm
getting problems.

The js file is in a different location since this is a user control in
a separate folder.

I tried changing the path to the file to: "~/Controls/myscript.js"
with the same issue

Dec 11 '07 #5
the js file path must be relative to the page folder, not the control.

"~" is not valid in a script tag, only in the url of a server side control,
because thats the only way it gets translated (its not supported by iis or
the browser).

-- bruce (sqlwork.com)
"cfps.Christian" wrote:
Syntax wise everything is correct, I copy the method names to ensure
that. Also I can get other files to work fine but this is the first
time with the UserControl/Dynamic script thing which is where I'm
getting problems.

The js file is in a different location since this is a user control in
a separate folder.

I tried changing the path to the file to: "~/Controls/myscript.js"
with the same issue

Dec 11 '07 #6
Well you were correct about the having to be in the directory of the
page. I ended up putting the script tag in the calling page rather
than the user control.

Is there no way to reference a js file in a user control and just put
the user control in a page?

Dec 11 '07 #7
make the js file a web resource instead of a file.

-- bruce (sqlwork.com)
"cfps.Christian" wrote:
Well you were correct about the having to be in the directory of the
page. I ended up putting the script tag in the calling page rather
than the user control.

Is there no way to reference a js file in a user control and just put
the user control in a page?

Dec 11 '07 #8

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

Similar topics

28
by: Daniel | last post by:
Hello =) I have an object which contains a method that should execute every x ms. I can use setInterval inside the object construct like this - self.setInterval('ObjectName.methodName()',...
54
by: tshad | last post by:
I have a function: function SalaryDisplay(me) { var salaryMinLabel = document.getElementById("SalaryMin"); salaryMinLabel.value = 200; alert("after setting salaryMinLabel = " +...
16
by: sneill | last post by:
How is it possible to take the value of a variable (in this case, MODE_CREATE, MODE_UPDATE, etc) and use that as an object property name? In the following example I want 'oIcon' object to have...
30
by: jimjim | last post by:
Hello, This is a simple question for you all, I guess . int main(){ double *g= new double; *g = 9; delete g; cout<< sizeof(g)<<" "<<sizeof(double)<<" "<<sizeof(*g)<<" "<<*g<<" "<<endl; *g =...
4
by: Kiyomi | last post by:
Hello, I am trying to replace my alert message box with a popup page. In my page behind,
15
by: Sam Kong | last post by:
Hello! I got recently intrigued with JavaScript's prototype-based object-orientation. However, I still don't understand the mechanism clearly. What's the difference between the following...
4
by: Greg | last post by:
I'm guessing the problem I'm having has something to do with Master Pages or DetailsView because the exact same code works fine on a page without a Master Page and DetailsView controls. The...
5
by: JLuv | last post by:
I have a javascript function defined in my <headtag on my ASP.NET page. In the code-behind i have the lines... void pGrid_DeleteCommand(object source, DataGridCommandEventArgs e) { string...
2
by: Ralph | last post by:
Hi I don't understand why it's not working: function schedule(imTop){ this.tdImagesTop = imTop; } schedule.prototype.selectEl = function() { alert(this.tdImagesTop);
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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
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...
0
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
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,...

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.