469,579 Members | 1,082 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,579 developers. It's quick & easy.

Troubles using javascript file as a webresource

4
I have a web user control with a button on it and i want to hook a javascript function to this button.

This is my web user control code:

Expand|Select|Wrap|Line Numbers
  1. <Assembly: WebResource("jstest.js", "text/javascript")> 
  2. Partial Class WebUserControl
  3.     Inherits System.Web.UI.UserControl
  4.  
  5.     Protected Sub Page_Load(ByVal sender As Object, ByVal e As    System.EventArgs) Handles Me.Load
  6.  
  7.         ' Define the resource name and type.
  8.         Dim rsname As String = "jstest.js"
  9.         Dim rstype As Type = Me.GetType()
  10.         ' Get a ClientScriptManager reference from the Page class.
  11.         Dim cs As ClientScriptManager = Page.ClientScript
  12.         '  Get the web resource url.
  13.         Dim ResourceURL As String = cs.GetWebResourceUrl(rstype, rsname)
  14.         cs.RegisterClientScriptInclude(rstype, "myscript", ResourceURL)
  15.         ' Register the client resource with the page.
  16.         cs.RegisterClientScriptResource(rstype, rsname)
  17.         ' Hook js function to the button.
  18.         Button1.Attributes.Add("onClick", "javascript:return test();")
  19.  
  20.     End Sub
  21. End Class 
This is the jstest.js content:

Expand|Select|Wrap|Line Numbers
  1.  function test()
  2. {
  3.    alert("Hello World");
  4.    return(false);
This is not working. Internet explorer just indicates 'Error on the page' on the status bar and there is just this message on the detail error information: 'Object expected'... Can any body help me to make this work? am i missing something?.

I need it to be this way cause i have several javascript files with functios that i need to hook to some other controls in my web user components.

Hope anybody can help me with this...
Thanks in advance.
Aug 28 '07 #1
3 3444
nateraaaa
663 Expert 512MB
I have a web user control with a button on it and i want to hook a javascript function to this button.

This is my web user control code:

Expand|Select|Wrap|Line Numbers
  1. <Assembly: WebResource("jstest.js", "text/javascript")> 
  2. Partial Class WebUserControl
  3.     Inherits System.Web.UI.UserControl
  4.  
  5.     Protected Sub Page_Load(ByVal sender As Object, ByVal e As    System.EventArgs) Handles Me.Load
  6.  
  7.         ' Define the resource name and type.
  8.         Dim rsname As String = "jstest.js"
  9.         Dim rstype As Type = Me.GetType()
  10.         ' Get a ClientScriptManager reference from the Page class.
  11.         Dim cs As ClientScriptManager = Page.ClientScript
  12.         '  Get the web resource url.
  13.         Dim ResourceURL As String = cs.GetWebResourceUrl(rstype, rsname)
  14.         cs.RegisterClientScriptInclude(rstype, "myscript", ResourceURL)
  15.         ' Register the client resource with the page.
  16.         cs.RegisterClientScriptResource(rstype, rsname)
  17.         ' Hook js function to the button.
  18.         Button1.Attributes.Add("onClick", "javascript:return test();")
  19.  
  20.     End Sub
  21. End Class 
This is the jstest.js content:

Expand|Select|Wrap|Line Numbers
  1.  function test()
  2. {
  3.    alert("Hello World");
  4.    return(false);
This is not working. Internet explorer just indicates 'Error on the page' on the status bar and there is just this message on the detail error information: 'Object expected'... Can any body help me to make this work? am i missing something?.

I need it to be this way cause i have several javascript files with functios that i need to hook to some other controls in my web user components.

Hope anybody can help me with this...
Thanks in advance.
In your html code for the button do you have a runat="server" tag?

Nathan
Aug 28 '07 #2
Lucas
4
In your html code for the button do you have a runat="server" tag?

Nathan
Yes i do. And to lengthen my message to at least 20 characters this is my web user control client side code showing the runat="server" tag:

Expand|Select|Wrap|Line Numbers
  1. <%@ Control Language="VB" AutoEventWireup="false" CodeFile="WebUserControl.ascx.vb" Inherits="WebUserControl" %>
  2. <asp:TextBox ID="TextBox1" runat="server" Style="z-index: 100; left: 26px; position: absolute;
  3.     top: 26px" Width="178px">Press Button</asp:TextBox>
  4. <asp:Button ID="Button1" runat="server" Style="z-index: 102; left: 143px; position: absolute;
  5.     top: 51px" Text="Click" Width="52px" />
Any idea?...
Aug 28 '07 #3
Lucas
4
I found a solution to my problem. It is not exactly what i wanted but it's working. Instead of using the javascript file as an embedded resource, this aproach uses it as a linked resource. For whoeverneedingsomethinglikethis's sake here is the code to add javascript to a web user control as mentionated:

Web control class code:

Expand|Select|Wrap|Line Numbers
  1. Partial Class WebUserControl
  2.     Inherits System.Web.UI.UserControl
  3.  
  4.     Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  5.  
  6.         ' Define the resource name and type.
  7.         Dim rsname As String = "jstest.js"
  8.         Dim rstype As Type = GetType(WebUserControl)
  9.         ' Get a ClientScriptManager reference from the Page class.
  10.         Dim cs As ClientScriptManager = Page.ClientScript
  11.         cs.RegisterClientScriptInclude(rstype, "myscript", rsname)
  12.         ' Hook js function to a button.
  13.         Button1.Attributes.Add("onClick", "javascript:return test();")
  14.  
  15.     End Sub
Lucas.
Aug 29 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by Håkan | last post: by
3 posts views Thread by fnustle | last post: by
reply views Thread by =?Utf-8?B?TG93bGFuZGVy?= | last post: by
2 posts views Thread by =?Utf-8?B?UGFi?= | last post: by
reply views Thread by suresh191 | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.