• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1151
  • Last Modified:

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
0
jabcoco
Asked:
jabcoco
  • 4
  • 2
1 Solution
 
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
jabcocoAuthor Commented:
Thanks
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now