Object Expected, Code Behind assignment of javascript

C

cfps.Christian

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?
 
M

Mark Rae [MVP]

cfps.Christian said:
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:Label> webcontrol... 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...?
 
C

cfps.Christian

Firstly, I'm assuming lbl is an <asp:Label> webcontrol... 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...?

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).
 
B

bruce barker

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)
 
C

cfps.Christian

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
 
B

bruce barker

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)
 
C

cfps.Christian

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?
 

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

Forum statistics

Threads
473,754
Messages
2,569,522
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top