65.9K
CodeProject is changing. Read more.
Home

Using C# code inside JavaScript code

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.57/5 (5 votes)

Aug 26, 2010

CPOL
viewsIcon

32681

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