1

I'm dynamically building html table from c# code. Here is a portion of my code:

listHTML.Append("<td onClick='GoToHourlyReport("
    + Convert.ToDateTime(dr["IntervalStartTime"]).ToString("yyyy-MM-dd")
    + ","
    + deptId
    + ");' align='center' valign='middle' class='graph_red_grid_text'>"
    + Convert.ToDateTime(dr["IntervalStartTime"]).ToString("yyyy-MM-dd hh:mm:ss")
    + "</td>"
);

On onclick of the td I'm trying to pass a date to a JavaScript function.

onClick='GoToHourlyReport("
    + Convert.ToDateTime(dr["IntervalStartTime"]).ToString("yyyy-MM-dd")
    + ","
    + deptId
    + ");' 

But when I pass a date like 2012-10-01, I get a value 1999 inside JavaScript function all the time. Can any one throw some light about what I'm doing wrong?

Here is the js function

    function GoToHourlyReport(date, deptId) {
    window.location.href = "CallAverageHourlyReport_BW.aspx?Date=" + date + "&Queue=" + deptId;
}
2
  • Well, it would be beneficial to see your JavaScript too. Commented Dec 6, 2012 at 13:33
  • Sounds more like the javascript function (which isn't shown) is manipulating the date. Commented Dec 6, 2012 at 13:34

2 Answers 2

4

View the page source, you will see your problem. You are missing quotes in the generated code.

In short you are doing

alert(2012-10-01);

not

alert("2012-10-01");

Add an escaped "

listHTML.Append("<td onClick='GoToHourlyReport(\"" + Convert.ToDateTime(dr["IntervalStartTime"]).ToString("yyyy-MM-dd") + "\",\"" + deptId + "\");'...
Sign up to request clarification or add additional context in comments.

3 Comments

well how to add " before date. I tried by adding a ' but it was rendering ? mark
Add an escaped " to surround the strings. Or use a string builder!
@jade, please mark this as the answer, both for the contributor and the community. Thanks!
1

You need to pass the date as a string, otherwise it gets interpreted as a number (2,010 minus 10 minus 1 = 1,999):

listHTML.Append("<td onClick=\"GoToHourlyReport('"
    + Convert.ToDateTime(dr["IntervalStartTime"]).ToString("yyyy-MM-dd")
    + "',"
    + deptId
    + ");\" align='center' valign='middle' class='graph_red_grid_text'>"
    + Convert.ToDateTime(dr["IntervalStartTime"]).ToString("yyyy-MM-dd hh:mm:ss")
    + "</td>"
);

This should generate:

<td onclick="GoToHourlyReport('2010-10-01', 1)" ...>

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.