Timer countdown sql, asp.net c#, javascript
i have the next javascript code:
<script language="JavaScript">
TargetDate = "12/31/2020 5:00 AM";
BackColor = "palegreen";
ForeColor = "navy";
CountActive = true;
CountSt开发者_JAVA百科epper = -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? i have sql table with the desgin: id, auctionEndTime .... how i'm connect that to the targetdate? it is possible?
Javascript
runs at clientside
so it can't access the database directly. 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. 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
.
You could use a property in code behind to fill up the javascript directly.
ASP.Net page:
<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; }
You can possibly set the value in a hidden
field after querying the Database and access the value from the field in your Javascript function
You could also use the ClientScript methods from the appropriate place in your code-behind:
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.
精彩评论