Home » Programming / Coding

ASP .Net Code Within JavaScript

17 July 2009 7,297 views No Comment

Simple thing really, often times you might need to use a asp .net variable within javascript code. How you do that?

  • Declare the variable as protected(public will do as well)
  • Set the code on JavaScript to use the Asp .net Variable within JavaScript

Example:

Declare the variable as protected like this.

protected string myVar;

On your code you would initialize it somewhere. e.g. myVar=”Great webmaster forum webcosmoForums.com”;

Set the code on JavaScript to use the Asp .net Variable within JavaScript e.g.

<script type=”text/javascript”>

var aVar= ‘<%=myVar %>’;    /*= here is shorthand for Response.Write*/

alert(aVar);

</script>

1 Star2 Stars3 Stars4 Stars5 Stars (2 votes, average: 5.00 out of 5)
Loading ... Loading ...

Leave your response!

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

This is a Gravatar-enabled weblog. To get your own globally-recognized-avatar, please register at Gravatar.