Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / December 2007

Tip: Looking for answers? Try searching our database.

Object Expected, Code Behind assignment of javascript

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
cfps.Christian - 11 Dec 2007 15:48 GMT
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?
Mark Rae [MVP] - 11 Dec 2007 16:33 GMT
> I've written a .js file and put the script in my usercontrol:
> <script src="myscript.js" />
[quoted text clipped - 8 lines]
>
> 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...?

Signature

Mark Rae
ASP.NET MVP
http://www.markrae.net

cfps.Christian - 11 Dec 2007 16:49 GMT
> > I've written a .js file and put the script in my usercontrol:
> > <script src="myscript.js" />
[quoted text clipped - 26 lines]
> 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).
bruce barker - 11 Dec 2007 16:50 GMT
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)

> I've written a .js file and put the script in my usercontrol:
> <script src="myscript.js" />
[quoted text clipped - 8 lines]
>
> Where am I going wrong with this?
cfps.Christian - 11 Dec 2007 17:21 GMT
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
bruce barker - 11 Dec 2007 17:31 GMT
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)

> 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
[quoted text clipped - 6 lines]
> I tried changing the path to the file to: "~/Controls/myscript.js"
> with the same issue
cfps.Christian - 11 Dec 2007 17:36 GMT
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?
bruce barker - 11 Dec 2007 18:28 GMT
make the js file a web resource instead of a file.

-- bruce (sqlwork.com)

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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.