Using <%= myCtrl.ClientID %> outside of the aspx page in javascript function?

I have a page Default.aspx and i have a lots of of JavaScript stuff with variable using <%= myCtrl.ClientID %>.

It's works fine if all javascript are inside the aspx page, but is there a way to put all javascript in a separate file "ex: Default.aspx.js" and continue using <% = %> statement?

Thanks
LVL 10
jabcocoAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

prairiedogCommented:
As long as you include the .js file in the aspx page header, it should work.
0
tiagosalgadoCommented:
I don't think that works.
Best way is passing your <%= myCrtl.ClientID %> as a parameter of your function.
0
prairiedogCommented:
It seems that the autor is using <%= myCrtl.ClientID %>, and everything is fine with it now. The author does not want to have all JavaScript code embeded in the aspx page, so an included .js file shuld work as the embedded JavaScript code as long as the .js file is linked in the aspx page's <head> section.
0
Amazon Web Services

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

jabcocoAuthor Commented:
prairiedog, don't seem to work..... if i put the function direct in my page it works fine but when the function is into a .js file... it's not working..
here is my code example
JavaScriptTest.aspx
---------------------
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="JavaScriptTest.aspx.cs" Inherits="JavaScriptTest" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script src="JavaScriptTest.aspx.js" type="text/javascript"></script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="showLabelInnerHtml();return false;" />
        <asp:Label ID="Label1" runat="server" Text="My Label Text"></asp:Label>
    </div>
    </form>
</body>
</html>
 
JavaScriptTest.aspx.js
-----------------------
function showLabelInnerHtml() {
    var lblID = '<%= Label1.ClientID %>';
    alert(lblID);
    alert(document.getElementById(lblID).innerHTML);
}

Open in new window

0
prairiedogCommented:
If you use .js file, you need to use the actual control ID, in your case "Label1", instead of "Label1.ClientID".
 
function showLabelInnerHtml() {
    var lblID = 'Label1';
    alert(lblID);
    alert(document.getElementById(lblID).innerHTML);
}
0
prairiedogCommented:
The bottom line: when JavaScript code with server-side code is embedded in aspx page, ASP.NET will execute the server-side code upon rendering, so everything is fine. However, if the same code is included in .js file, ASP.NET will not execute the server-side code, and it errors out.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jabcocoAuthor Commented:
Thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.