0

calling a js function using onclick...

onclick="SubmitAge(66, 'ctl00_MainContent_arTo_upAgeRange')" 

function just calls an updatePanel from the client... but its saying object expected at the onclick= part!!!

here is the function, is there anything wrong with the code?

function SubmitAge(age, UpdatePanelID) {
    $get('HiddenAge').value = age;
    __doPostBack(UpdatePanelID);
}

EDIT: THE SUBMITAGE FUNCTION IS INSIDE A .JS FILE (AGERANGE.JS) AND ONLY WHEN MOVED HERE DOES IT STOP WORKING: HERE IS THE LINKING METHOD/HEADERS FROM THE ASCX USERCONTROL INWHICH IT IS ALL CONTAINED...

%@ Control Language="VB" ClassName="AgeRange" %

%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="AjaxCT" %

script src="AgeRange.js" type="text/javascript" /script

(arrow tags removed here as it wont display, hint: stackoverflow!!!)

im printing it like this from the server...

Public Sub AppendToSB(ByRef sb As StringBuilder, ByVal CurNo As Byte, Optional ByVal clickedNo As Byte = 0)
    Dim sConfirmClick = ""

    If clickedNo = CurNo Then   ' maybe dont make it clickable...
        sConfirmClick = "return confirm('The number " & CurNo.ToString & " is already selected, are you sure you want to select it again?');"
    End If

    sb.Append("<a href=""#"" onclick=""" & sConfirmClick & "SubmitAge(" & CurNo.ToString & ", '" & upAgeRange.ClientID &
     "')"" runat=""server"">" & CurNo.ToString & "</a>")

End Sub
Erx_VB.NExT.Coder
  • 4,838
  • 10
  • 56
  • 92
  • the problem is this part... onclick="SubmitAge(66, 'ctl00_MainContent_arTo_upAgeRange')" (this is as printed on the client source – Erx_VB.NExT.Coder Dec 30 '09 at 22:41
  • Does this really have anything to do with asp.net, C#, Ajax, or vb.net? – Tyler Dec 30 '09 at 22:45
  • Depending on the context of the question this might be a syntax mixup between ASP.NET server tags, JavaScript/HTML syntax, and C#/VB.NET syntax. We'll have to see some more code first. – Eilon Dec 30 '09 at 22:46
  • this is all as printed in the html source as i did a viewsource in internet explorer... @matrixfrog... and i am coding in vb/c# asp.net & ajax (updatepanel) so it is all relevant depending on teh questions i get – Erx_VB.NExT.Coder Dec 30 '09 at 22:54
  • Where is the JavaScript function defined? In the same page? In a referenced JS file? This error could happen if the SubmitAge method can't be found, and thus it would itself be null. – Eilon Dec 30 '09 at 22:56
  • added server printing method (edited) orig post – Erx_VB.NExT.Coder Dec 30 '09 at 22:56
  • in a references js file... and i've linked it... this is in a userControl... not linked from aspx container which houses the UserControl... aspx control has the user control inside it with no additional linking... i presume i dont need to re-link the .js file from the aspx page itself again? – Erx_VB.NExT.Coder Dec 30 '09 at 22:57
  • Also, I think that StackOverflow will show angle brackets if you put it inside a code tag. – Eilon Dec 30 '09 at 23:38

2 Answers2

1

Complete rewrite of my post after several clarifications:

The problem is that the ASPX page is referencing an ASCX user control that is located in a different folder. That ASCX control has an HTML <script> tag that is using a relative path to the JS file.

The solution is to correctly resolve the URL to the JS file by using some extra code:

<script src="<%= ResolveClientUrl("MyScriptLibrary.js") %>" type="text/javascript">
</script>

To prevent the script file from being referenced multiple times I recommend using the approaches specified in this other post: ASP.NET dynamically insert code into head

Here's what it looks like in the user control's code:

// Register a script reference: 
Page.ClientScript.RegisterClientScriptInclude(GetType(), "myLibraryScript", "~/Scripts/MyScriptLibrary.js"); 
Community
  • 1
  • 1
Eilon
  • 25,582
  • 3
  • 84
  • 102
  • its a html tag... so once i click the html tag in this case, it updates updatePanel from a js function inside a js file (submitAge) – Erx_VB.NExT.Coder Dec 30 '09 at 23:01
  • Try this, then: onclick="alert(SubmitAge)" And see what happens. That'll tell you if SubmitAge is defined. – Eilon Dec 30 '09 at 23:05
  • tried it, wow, the function fails, i wonder why? this is my link tag in the user control... <%@ Control Language="VB" ClassName="AgeRange" %> <%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="AjaxCT" %> – Erx_VB.NExT.Coder Dec 30 '09 at 23:11
  • now, AgeRange.js is in the same directory as AgeRange.ascx however, the container aspx file is in a directory above, surely this shouldn't matter? – Erx_VB.NExT.Coder Dec 30 '09 at 23:12
  • I think I'm even more confused now. Can you update the original post with more complete information? Please show as much code as you can from the ASPX, the code behind, the JavaScript files, and anything else that you have. – Eilon Dec 30 '09 at 23:12
  • done, ive updated the post... somethign to do with the .js file, stops working when js function is moved ot the js file – Erx_VB.NExT.Coder Dec 30 '09 at 23:27
  • Thanks for updating the post. It's still not clear to me exactly how the .JS file is being referenced (either directly or indirectly) from the main ASPX file. Are you saying that the ASPX is using an ASCX, and that the ASCX has a reference to the JS file? If so, please show that code and markup as well since at the moment that looks like the most likely cause of this issue. – Eilon Dec 30 '09 at 23:34
  • yes that is exactly how it is.... ascx has ref to js file, aspx does not... aspx just has the ascx usercontrol inside it... however, i still see the script js ref printed out in the html source (in the body) of the aspx output html... not the header – Erx_VB.NExT.Coder Dec 30 '09 at 23:36
  • Then maybe it is related to the script tag pointing at a path relative to the ASCX control and not relative to the ASPX page. To make sure it's always the right path do this: – Eilon Dec 30 '09 at 23:42
  • wow, that worked, thanks so much for your help... also found some other ways to do it but yours looks nicer... http://stackoverflow.com/questions/280201/external-js-file-in-web-user-control – Erx_VB.NExT.Coder Dec 30 '09 at 23:49
  • if i add the usercontrol twice in the page, this is being printed out twice in the body, what method would you recommend? – Erx_VB.NExT.Coder Dec 30 '09 at 23:51
0

here is how i decided to do it, ResolveClientURL is important, static link will not always work, also the if check will prevent the script being added several times in one page if you use the control multiple times on same page...

    If Not Page.ClientScript.IsClientScriptIncludeRegistered("AgeRangeJS") Then ' no point in registering it twice!
        ' AND its registered in the header, where it should be, not two copies in the body :)
        Page.ClientScript.RegisterClientScriptInclude("AgeRangeJS", ResolveClientUrl("AgeRange.js")) ' ResolveClientUrl("AgeRange.js")
    End If

Erx_VB.NExT.Coder
  • 4,838
  • 10
  • 56
  • 92
  • BTW there's no need to check for IsClientScriptIncludeRegistered. ASP.NET will automatically exclude duplicates. That method is almost never needed. It really only needs to be used if you need to make some *other* decision based on whether a script was already registered (though I have no idea when that would ever happen). – Eilon Dec 31 '09 at 00:35
  • ahh thanks eilon, will remove that... just wanted to say, using this without the resolveclienturl didn't work for me, even tho the orig poster i believe suggested this should work from the userControl itself... (unless im mistaken and they meant for it to work from the aspx page). – Erx_VB.NExT.Coder Dec 31 '09 at 01:10