开发者

JavaScript question

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" %

%@ Regist开发者_如何学JAVAer 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


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"); 


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜