Solved

ASP .Net C# Alternating Style for each row

Posted on 2013-02-06
12
338 Views
Last Modified: 2013-02-23
I know that you can do this with Jquery, but I'm looing for a server script to apply alternating row class for the below C# script.  I think I'm almost there, but can't quite figure out what is needed.

        if (!Page.IsPostBack)
        {

            XDocument xmlDoc = XDocument.Load(Server.MapPath("directory.xml"));

            var persons = from person in xmlDoc.Descendants("Person")
                          select new
                          {
                              name = person.Element("name").Value,
                              rank = person.Element("rank").Value,
                              agency = person.Element("agency").Value,
                              title = person.Element("title").Value,
                              phone = person.Element("phone").Value,
                              email = person.Element("email").Value,
                              office = person.Element("office").Value,
                              address1 = person.Element("address1").Value,
                              city = person.Element("city").Value,
                              st = person.Element("st").Value,
                              zip = person.Element("zip").Value,

                          };

            litXMLData.Text = "<table id='directory' class='info' summary='List of Officers'><caption>List of Officers</caption><thead>";
            litXMLData.Text = litXMLData.Text + "<tr><th scope='col'>Rank and Name<br />Title</th><th scope='col'>Agency</th><th scope='col'>Address</th><th scope='col'>Phone<br />Email</th></tr></thead><tbody>";
            foreach (var person in persons)
            {

                //If the it is odd
                //if ((persons.Index % 2) == 0)
                    

                litXMLData.Text = litXMLData.Text + "<tr class='odd'><th scope='row'>" + person.rank + " " + person.name + "<br />" + person.title + "</th>";
                litXMLData.Text = litXMLData.Text + "<td>" + person.agency + "</td>";
                litXMLData.Text = litXMLData.Text + "<td>" + person.address1 + "<br />" + person.city + ", " + person.st + person.zip + "</td>";
                litXMLData.Text = litXMLData.Text + "<td>" + person.phone + "<br />" + person.email + "</td></tr>";

                //If it is even
               // else
                   


                litXMLData.Text = litXMLData.Text + "<tr class='even'><th scope='row'>" + person.rank + " " + person.name + "<br />" + person.title + "</th>";
                litXMLData.Text = litXMLData.Text + "<td>" + person.agency + "</td>";
                litXMLData.Text = litXMLData.Text + "<td>" + person.address1 + "<br />" + person.city + ", " + person.st + person.zip + "</td>";
                litXMLData.Text = litXMLData.Text + "<td>" + person.phone + "<br />" + person.email + "</td></tr>";

            }
            litXMLData.Text = litXMLData.Text + "</tbody></table>";

            if (litXMLData.Text == "")
                litXMLData.Text = "No Results.";

        }

Open in new window

0
Comment
Question by:atljarman
  • 7
  • 3
  • 2
12 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 38860005
You've got the right idea with the comments you've put in:  Do a modulus operation using 2 as the divisor to determine if you've got an even or odd number. The apply the style accordingly.

The only thing I'd suggest is that as currently written, you've got two pieces of logic to keep in sync. Why not extract the bit that is "variable"--in this case, the CSS class--to a variable? Then just apply the variable in one place.

e.g.

        if (!Page.IsPostBack)
        {

            XDocument xmlDoc = XDocument.Load(Server.MapPath("directory.xml"));

            var persons = from person in xmlDoc.Descendants("Person")
                          select new
                          {
                              name = person.Element("name").Value,
                              rank = person.Element("rank").Value,
                              agency = person.Element("agency").Value,
                              title = person.Element("title").Value,
                              phone = person.Element("phone").Value,
                              email = person.Element("email").Value,
                              office = person.Element("office").Value,
                              address1 = person.Element("address1").Value,
                              city = person.Element("city").Value,
                              st = person.Element("st").Value,
                              zip = person.Element("zip").Value,

                          };

            litXMLData.Text = "<table id='directory' class='info' summary='List of Officers'><caption>List of Officers</caption><thead>";
            litXMLData.Text = litXMLData.Text + "<tr><th scope='col'>Rank and Name<br />Title</th><th scope='col'>Agency</th><th scope='col'>Address</th><th scope='col'>Phone<br />Email</th></tr></thead><tbody>";
            
            string[] evenOdd = { "odd", "even" };
            
            foreach (var person in persons)
            {
                // Use the modulus result to index the "evenOdd" array above
                
                litXMLData.Text = litXMLData.Text + "<tr class='" + evenOdd[persons.Index % 2] + "'><th scope='row'>" + person.rank + " " + person.name + "<br />" + person.title + "</th>";
                litXMLData.Text = litXMLData.Text + "<td>" + person.agency + "</td>";
                litXMLData.Text = litXMLData.Text + "<td>" + person.address1 + "<br />" + person.city + ", " + person.st + person.zip + "</td>";
                litXMLData.Text = litXMLData.Text + "<td>" + person.phone + "<br />" + person.email + "</td></tr>";
            }

            litXMLData.Text = litXMLData.Text + "</tbody></table>";

            if (litXMLData.Text == "")
                litXMLData.Text = "No Results.";

        }

Open in new window

0
 

Author Comment

by:atljarman
ID: 38860211
Just tried that and for some reason the page errored out. I can't debug because i don't have my development machine here.  Any thoughts what might be causing it to error out?  Using Visual for Applications, I'm not seeing any unclosed brackets or quotes.
0
 

Author Comment

by:atljarman
ID: 38860213
Do I need to import a namespace to use the array.  I'm currently using:

using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Xml;
0
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 38860375
I didn't look too closely at your existing logic. Where is persons.Index coming from?
0
 

Author Comment

by:atljarman
ID: 38860505
I just pulled it from an example on the web.  I have no idea it that is correct.  Sorry about that.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 38860591
Let's try working with a for loop instead of a foreach loop:

        if (!Page.IsPostBack)
        {

            XDocument xmlDoc = XDocument.Load(Server.MapPath("directory.xml"));

            var persons = from person in xmlDoc.Descendants("Person")
                          select new
                          {
                              name = person.Element("name").Value,
                              rank = person.Element("rank").Value,
                              agency = person.Element("agency").Value,
                              title = person.Element("title").Value,
                              phone = person.Element("phone").Value,
                              email = person.Element("email").Value,
                              office = person.Element("office").Value,
                              address1 = person.Element("address1").Value,
                              city = person.Element("city").Value,
                              st = person.Element("st").Value,
                              zip = person.Element("zip").Value,

                          };

            litXMLData.Text = "<table id='directory' class='info' summary='List of Officers'><caption>List of Officers</caption><thead>";
            litXMLData.Text = litXMLData.Text + "<tr><th scope='col'>Rank and Name<br />Title</th><th scope='col'>Agency</th><th scope='col'>Address</th><th scope='col'>Phone<br />Email</th></tr></thead><tbody>";
            
            string[] evenOdd = { "odd", "even" };
            
            for (int i = 0; i < persons.Length; i++)
            {
                var person = persions[i];
                
                // Use the modulus result to index the "evenOdd" array above
                
                litXMLData.Text = litXMLData.Text + "<tr class='" + evenOdd[i % 2] + "'><th scope='row'>" + person.rank + " " + person.name + "<br />" + person.title + "</th>";
                litXMLData.Text = litXMLData.Text + "<td>" + person.agency + "</td>";
                litXMLData.Text = litXMLData.Text + "<td>" + person.address1 + "<br />" + person.city + ", " + person.st + person.zip + "</td>";
                litXMLData.Text = litXMLData.Text + "<td>" + person.phone + "<br />" + person.email + "</td></tr>";
            }

            litXMLData.Text = litXMLData.Text + "</tbody></table>";

            if (litXMLData.Text == "")
                litXMLData.Text = "No Results.";

        }

Open in new window

0
 

Author Comment

by:atljarman
ID: 38860984
Still no luck
0
 

Author Comment

by:atljarman
ID: 38861069
I noticed that person was misspelled.  I changed it with no improvement.  Is there a namespace I'm supposed to reference for use of arrays?
0
 
LVL 53

Assisted Solution

by:COBOLdinosaur
COBOLdinosaur earned 250 total points
ID: 38861213
If you are going to put it on a web page, why do all that on the server instead of using css:

tr:nth-of-type(odd) {any property settings you need}
And
tr:nth-of-type(even) {any property settings you need}

Will let you apply whatever need to be done.

If you need to set cells within the rows then you can use
tr:nth-of-type(odd)>td {any property settings you need}

Cd&
0
 

Author Comment

by:atljarman
ID: 38861235
COBOLdinosaur,

Not sure how to implement your suggestion.   So if the table has a class of datatables then it would look something like:

table.datatables tr:nth-of-type(odd) {background-color: white}
And
table.datatables tr:nth-of-type(even) {background-color: blue;}
0
 
LVL 53

Accepted Solution

by:
COBOLdinosaur earned 250 total points
ID: 38861704
Actually for a background color it works better if you apply it to the td:

table.datatables tr:nth-of-type(odd)>td {background-color: white;}
And
table.datatables tr:nth-of-type(even)>td {background-color: blue;}

Either in a stylesheet or in a style tag in the head of the page


Cd&
0
 

Author Closing Comment

by:atljarman
ID: 38922135
Thanks for your help
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
pressing download button in c# 7 36
Need help with another query 10 39
SP converting date time to date and time separately 2 20
Secure log in 'box' 4 14
When it comes to write a Context Sensitive Help (an online help that is obtained from a specific point in state of software to provide help with that state) ,  first we need to make the file that contains all topics, which are given exclusive IDs. …
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This tutorial demonstrates how to identify and create boundary or building outlines in Google Maps. In this example, I outline the boundaries of an enclosed skatepark within a community park.  Login to your Google Account, then  Google for "Google M…
The is a quite short video tutorial. In this video, I'm going to show you how to create self-host WordPress blog with free hosting service.

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question