0

I have a user control called header.aspx. In my user control if I do this it will work.

<script type="text/javascript">
    function greeting(){
        Alert("hi");
    }
</script>

 <asp:button id="button1" OnClientClick="greeting" /> </asp:button>

I am using this user control in a page called default.aspx. I tried using src="~scripts/foo.js". If I do that it does not work. The question is pretty simple I guess. How would I call a java script function in a user control which is stored in an external location( not in the page. Located in the scripts folder). Thanks in advance.

1
  • Issue may be in the path src="~scripts/foo.js" you are specifying. Commented Jun 19, 2014 at 4:29

6 Answers 6

2

As I can understand this is clearly a path issue. Just follow these steps might help you.

  1. Create a .js file first. Put your code and save it in the folder you want it to.
  2. Now Drag and Drop the js file inside the head section of your html code from the Solution Explorer window. This will give you the correct path for the js file.

The above steps is what I follow, when I create an external js file for my controls.

Also make sure you call your function in this manner also suggested by others Else your function won't get call:

<asp:button id="button1" OnClientClick="greeting();" /> </asp:button>
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks for the reply. Just to double check. This procedure would work for a user control too right?
yes it will. I use this my my list box controls too.
upvoted. It is better approach to use external js file.
@tranttrum, if this answer was helpful, please accept it as an answer, so that it can be helpful to others in future.
@HassanNisar but you are increasing a http request is not it?
|
2

Just use the code below:

<script src="<%: ResolveUrl("~/Scripts/foo.js") %>"></script>

Comments

2

Script: test.js

    function greeting() {
    alert("hi");
    return false;
}

user control: <asp:Button ID="button1" OnClientClick="return greeting()" runat="server" Text="click" />

Page:

    <head runat="server">
    <title></title>
    <script src="test.js"></script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:temp ID="temp" runat="server" />
    </div>
    </form>
</body>
</html>

This should work now.

3 Comments

Thanks for the reply. Why should we use return greeting() not just greeting?
@trant trum, there is no need for return in your code, as its only alert you need, but in case if your javascript function is returning some values than you need to use return.
No need to write return if you are not returning the value. In my code i have returned false from the function, so i have used return greeting().
1
 <asp:button id="button1" OnClick=" javascript : greeting();" /> </asp:button>

try to use it. havent trie but i think it should work.

Comments

0

First of all you need to create a seprate javascript file and then add this in your page in this way. add this tag in the tag of your page.

use

OnClientClick="greeting()"

you missed "()" in OnClientClick="greeting"

Please see the whole html code:

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script src="scripts/foo.js"></script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:button id="button1" runat="server" OnClientClick="greeting()" Text="hit" />
    </div>
    </form>
</body>
</html>

Thanks.

4 Comments

I tried that. But I get an exception saying the javascript function is not found.
is javascript file is in "script" function? if it's so then use the updated line.
@Raja. Script in the path is not function. It is folder.
also use alert instead of Alert.
0

External javascript file reference:

<script src="yourpath/yourfilename.js"></script>

Button Control

<asp:Button ID="button1" OnClientClick="greeting();" runat="server" Text="click" />

Comments

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.