Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / web / ASP.NET

Using C# code inside JavaScript code

4.57/5 (5 votes)
26 Aug 2010CPOL 32.7K  
if you are working in a page that has a master page, and you want to reference the server controls from JavaScript you must use their ClientID instead of the ID property, so in order to use C# code within JavaScript code, you will have to surround your C# code with the <%= and %>, and this is the example.

var txtBox = document.getElementById(‘<%=txtName.ClientID%>’); 


remember this code is available only if your JavaScript code was in an .aspx file not a .js file

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)