1

I am getting a javascript validation error on my "Customer Search" page, when I click my "Search" button.

<asp:Button id="btnCustomerSearch" runat="server" onclientclick="return ValidateCustomer(1);" 
                                    Text="Continue" OnClick="btnCustomerSearch_Click" />  

The error message is:

Microsoft JScript runtime error: 'ValidateCustomer' is undefined  

however, in my ValidationFunctions.js, the ValidateCustomer function does exist.
How come the aspx cannot see that function (and how can I fix the issue) ?

Update:
The above issues are being caused as a result of the js file includes not being accurately referenced. However, I have an Alert messagebox in my code behind as indicated in this post and the alert message box don't appear as well. So this maybe a machine related issue.

How would I determine if the issue is machine related?

8
  • 2
    are you properly referencing your ValidationFunctions.js file? Commented Aug 14, 2012 at 14:52
  • Could you post that function here? Also, are you sure that this JS is linked at the page? Commented Aug 14, 2012 at 14:52
  • Are you sure you are referencing the script file correctly? Can you actually look at the source and navigate to it? Commented Aug 14, 2012 at 14:53
  • Can you also post the ValidateCustomer function? And also where it's referenced in your ASPX file? Commented Aug 14, 2012 at 14:55
  • This is a master page application. So I can see the <script type="text/javascript" src="Scripts/ValidationFunctions.js"></script> on the .Master file Commented Aug 14, 2012 at 14:56

1 Answer 1

2

This is a master page application. So I can see the <script type="text/javascript" src="Scripts/ValidationFunctions.js"></script> on the .Master file

That might be a problem if the page you are on is in a subdirectory different from where the MasterPage is on.

One alternative I prefer is to add the external javascript files inside the ScriptManager if I am using one:

<asp:ScriptManager ID="SM1" runat="server">
  <Scripts>
    <asp:ScriptReference Path="~/js/ValidationScript.js" />
  </Scripts>
</asp:ScriptManager>

This guarantees that the script reference is always resolved to the proper path because the ScriptManager is a service-side control and uses ResolveClientUrl to resolve the path ~/js/ValidationScript.js.

If this is not an option or does not apply, I would reference the external JS files from the master page as so:

<head runat="server" id="page_header">
  <script type="text/javascript" src='<%=ResolveClientUrl("~/js/Validation.js")%>'  />
</head>

This guarantees that js directory is always resolved correctly regardless of where the page or the control is located.

Sign up to request clarification or add additional context in comments.

2 Comments

wierd reaction.... when I include your script on the master page <script type="text/javascript" src='<%=ResolveClientUrl("~/js/Validation.js")%>' /> the application's start page does not load at all.... just keeps attempting to load but the start page never appears. When I change it back to my original script <script type="text/javascript" src="Scripts/ValidationFunctions.js"></script> the start page loads fine
@DotNetRookie very strange. I just tested in my PC and definitely works fine. <head runat="server"> <script type="text/javascript" src='<%=ResolveClientUrl("~/Scripts/jquery-1.4.1.js") %>' ></script> </head>. The script path resolves properly. Without ResolveClientUrl returns the incorrect path when the page is not in the same directory as the MasterPage.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.