Solved

ASP .Net C# Alternating Style for each row

Posted on 2013-02-06
12
337 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying 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

Any business that wants to seriously grow needs to keep the needs and desires of an international audience of their websites in mind. Making a website friendly to international users isn’t prohibitively expensive and can provide an incredible return…
Because your company can’t afford for you to make SEO mistakes, you’ll want to ensure you’re taking the right steps each and every time you post a new piece of content. This list of optimization do’s and don’ts can help you become an SEO wizard.
This video teaches users how to migrate an existing Wordpress website to a new domain.
The viewer will learn how to count occurrences of each item in an array.

856 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