0

I have a conditional statement written in JavaScript but Im using a Gridview in asp.net and need assistance converting this to C# or VB. Also if you can assist me on where to implement the code with the page it would be appreciated.

The code below is comparing the "scheduledTime" variable(TIMESTAMP) to the "currentTime"(system clock), and would return a background on that row.

var currentTime = new Date(); /* not sure if this is the correct time object to use */
var scheduledTime = scheduledTime();

if (scheduledTime >= 15mins) {
return 'background-color:red;'
} else if (scheduledTime > 15mins <= 30mins) {
return 'background-color:yellow;'
} else if (scheduledTime > 30mins <= 2hours) {
return 'background-color:green;'
} else if (scheduledTime > 2hours) {
return 'background-color:none;
}

Thanks so much!

2
  • That's not valid Javascript. Commented Jan 2, 2013 at 18:24
  • Why can't you just put code that looks exactly like this in the PageLoad function? Instead of return just set the attribute on the grid control Commented Jan 2, 2013 at 18:26

1 Answer 1

1

This should be the equivalent version:

protected void grid_RowDataBound(Object sender, GridViewRowEventArgs e)
{

 if(e.Row.RowType == DataControlRowType.DataRow)
 {
   if((DateTime.Now - DateTime.Parse((e.Row.DataItem as DataRowView)["scheduledTime "])).TotalMinutes<=15)
       e.Row.BackColor = System.Drawing.Color.Red;
   else if  //... etc
 }

And in your Gridview markup, simply add an OnRowDataBound handler:

<asp:gridview runat="server" id="yourGrid" OnRowDataBound="grid_RowDataBound" ...>
Sign up to request clarification or add additional context in comments.

3 Comments

lcarus, thanks for the help. In my example my javascript is not valid/correctly written but you see what I am trying to do, I appreciate your review.
@crh313 my code is the C# equivalent of what you attempted to write in your code. Was my answer helpful or not?
@lcarus - for the last else if statement I have >30min<=2hrs, Im not sure the "TotalMinutes" would work here.

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.