How to Pass a Variable to a Pop Up Box in C#/JavaScript/ASP .NET From C#

I am attempting to modify a web page that requires a string to be passed to it based on information that is retrieved from a database query within the C# portion of the web site coding. I was wondering if someone could tell me how I would pass or retrieve a variable inside of the ASP .NET file from the C# application. In other words, let's say I wanted to display the string variable results on the aspx page. How would I get the value of results from the C# page over to the ASP .NET page to be displayed in the JavaScript error message <script type = "text/javascript"> alert(<DIsplay the contents of the results variable from the C# page>)</script>

Any help that you could provide would be greatly appreciated.
Who is Participating?
If I understand you well, what you need is to put some server value into your client code.

On your .aspx page you'll have your javascript code. This code is posted to client computer when the aspx page executes and the javascript code inside is executed as soon as javascript parser on client browser starts to recognize it.

So, on your .aspx page's code you can do something like:

<script type="text/javascript">
      alert('There is <%= whateverServerVariable %> value');

When the page renders, <%= whateverServerVariable %> will be replaced by the value of the variable on server side, posting to your page something like:

<script type="text/javascript">
   alert('There is 47 value');

This is easy but a bit ugly.

It's better to post the value of your server variable into a hidden element inside the .aspx page (for example an input type="hidden") and access that value directly from the javascript code. The problem here is that you need to ensure that your javascript code doesn't start executing before the hidden element is loaded, or the value it returns will be undefined.
Something like this should work
<script type="text/javascript" language="javascript">         function funDoSomething()     {         var strMessage = <%= "'" + str + "'"%>;         alert( strMessage );     }  </script>

Open in new window

What was wrong with my answer , did you try it?
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.