简体   繁体   English

计时器倒数SQL,ASP.NET C#,JavaScript

[英]Timer countdown sql, asp.net c#, javascript

i have the next javascript code: 我有下一个JavaScript代码:

 <script language="JavaScript">
  TargetDate = "12/31/2020 5:00 AM";
  BackColor = "palegreen";
  ForeColor = "navy";
  CountActive = true;
  CountStepper = -1;
  LeadingZero = true;
  DisplayFormat = "%%D%% Days, %%H%% Hours, %%M%% Minutes, %%S%% Seconds.";
  FinishMessage = "the auction end"
  </script>
   <script language="JavaScript" src="countdown.js"></script>

my question is... how to do that the TargetDate will get the datetime from sql table? 我的问题是...该怎么做,TargetDate将从sql表中获取日期时间? i have sql table with the desgin: id, auctionEndTime .... how i'm connect that to the targetdate? 我有带有设计的sql表:id,auctionEndTime ....我如何将其连接到targetdate? it is possible? 有可能的?

Javascript runs at clientside so it can't access the database directly. Javascriptclientside运行,因此无法直接访问数据库。 However, you could call a service from javascript and that service could fetch the record from the database in JSON format or may be in your custom format. 但是,您可以从javascript调用服务,并且该服务可以以JSON格式或自定义格式从数据库中获取记录。 However, If you need to fetch the value only once that is when page loads, so you could add a hidden field to the page, sets the hidden field value to whatever value you like to at serverside, fetch the hidden field value from javascript and set it to TargetDate . 但是,如果只需要在页面加载时获取一次该值,则可以向页面添加hidden字段,将隐藏字段值设置为您希望在服务器端使用的任何值,然后从javascript和将其设置为TargetDate

查询数据库后,可以在hidden字段中设置值,然后从Javascript函数中的字段访问值

You could also use the ClientScript methods from the appropriate place in your code-behind: 您还可以在代码后面的适当位置使用ClientScript方法:

ClientScript.RegisterClientScriptBlock(this.GetType(), 
                                       "AuctionTargetDateScript", 
                                       string.Format("TargetDate = '{0}';", TargetDateFromDB), 
                                       true);

Have a look at this MSDN page for more details on the methods availabe via ClientScript. 请访问此MSDN页面,以获取有关通过ClientScript可用的方法的更多详细信息。

You could use a property in code behind to fill up the javascript directly. 您可以在后面的代码中使用属性来直接填充javascript。

ASP.Net page: ASP.Net页:

<script language="JavaScript">
  TargetDate = "<% = TargetDate %>"; /*this is a property in code behind*/
  BackColor = "palegreen";
  ForeColor = "navy";
  CountActive = true;
  CountStepper = -1;
  LeadingZero = true;
  DisplayFormat = "%%D%% Days, %%H%% Hours, %%M%% Minutes, %%S%% Seconds.";
  FinishMessage = "the auction end"
</script>
<script language="JavaScript" src="countdown.js"></script>

Page code behind:

public string TargetDate{
  // Build code to get date from database
  string sql = "SELECT targetDate from Events where Event_ID = 1309";
  // execute sql
  // ...
  return dbvalue;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM