In this asp.net tutorial you will learn how to pass the variable value from asp.net code behind to javascript. It is quite easy and handy. You must know how to do so because in certain cases you may have to pass value from asp.net code behind to javascript. Let's have a look over how to do so
How to pass the variable value from asp.net code behind to javascript
<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>How to pass the variable value from asp.net code behind to javascript</title> <script type="text/javascript"> //Getting variable from asp.net code behind var myMessage = "<%=myMessageFromCodeBehind %>"; alert(myMessage); </script> </head> <body> <form id="form1" runat="server"> <div> </div> </form> </body> </html>Let's have a look over .cs side
//Declaring protected type string variable protected string myMessageFromCodeBehind; protected void Page_Load(object sender, EventArgs e) { //Assigning Value to variable myMessageFromCodeBehind = "Happy coding, keep coding"; }I have declared a protected type string variable myMessageFromCodeBehind in asp.net code behind, assign a value to that variable and get it on my .aspx page using javascript given below
var myMessage = "<%=myMessageFromCodeBehind %>";
alert(myMessage);
So this is the proper way to pass the variable value from asp.net code behind to javascript.