?
Solved

How to pass C# variable to javascript variable?

Posted on 2008-11-05
2
Medium Priority
?
537 Views
Last Modified: 2013-12-17
Hi

In VC# code, it gets the current date time from system


protected System.DateTime CurDateTime;

protected string SysDateTime;



this.CurDateTime = System.DateTime.Now;

this.SysDateTime = this.CurDateTime.ToString("month dd, yyyy HH:mm:ss");

I have a javascript variable needs to get the SysDateTime value in aspx page.

var currenttime = <%= SysDateTime %>;

But, it cannot get it, how should I write it?

0
Comment
Question by:techques
2 Comments
 
LVL 7

Accepted Solution

by:
aherps earned 100 total points
ID: 22884154
Instead of: var currenttime = <%= SysDateTime %>;

Try: var currenttime = "<%= SysDateTime %>";
0
 
LVL 25

Assisted Solution

by:Lee Savidge
Lee Savidge earned 100 total points
ID: 22884167
Hi,

I would put the javascript in the codebehind and then build it that way. Then when the page is built server side, the script is generated and sent clientside ready populated. Take a look at http://msdn.microsoft.com/en-us/library/system.web.ui.page.registerstartupscript(VS.71).aspx

Page.RegisterStartupScript and Page.RegisterClientScriptBlock both emit javascript to the client side but are built server side which means you have full control over it.

Thanks,

Lee
0

Featured Post

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Simulator games are perfect for generating sample realistic data streams, especially for learning data analysis. It is even useful for demoing offerings such as Azure stream analytics, PowerBI etc.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

615 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question