0

i have the next javascript code:

 <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? i have sql table with the desgin: id, auctionEndTime .... how i'm connect that to the targetdate? it is possible?

4 Answers 4

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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;
}

3 Comments

As a side-note, I'd like to add that it would be nice to fetch the value in ´Page_Load´ and set it to a private variable. The private variable would then be called through the property.
Thank you very much... because i regular to use LINQ to SQL .. i don't know how to build here the code to get date from database. my table: Timer and the column name: endTime i will very appricate if you give me that last help... thank you anyway !
@Bside: Take a look at this article
0

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

2 Comments

Its sounds intresting... can you explain more detailed?
@Bside, you would use an <asp:Hidden/> control in your page and set the value of it from the server side. You could then get the value of the control via JavaScript and store it in your TargetDate variable.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.