Solved

ASP .Net C# Alternating Style for each row

Posted on 2013-02-06
12
334 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 74

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
 
LVL 74

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 74

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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
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…
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

705 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now