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?
 
prairiedogConnect With a Mentor Commented:
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
 
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
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

 
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
 
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
 
jabcocoAuthor Commented:
Thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.