charlie
charlie

Reputation: 323

Convert javascript condition to C# ASP.Net solution

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!

Upvotes: 0

Views: 171

Answers (1)

Icarus
Icarus

Reputation: 63966

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" ...>

Upvotes: 1

Related Questions