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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Something like this should work
<script type="text/javascript" language="javascript">         function funDoSomething()     {         var strMessage = <%= "'" + str + "'"%>;         alert( strMessage );     }  </script>

Open in new window

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.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
What was wrong with my answer , did you try it?
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.