Link to home
Start Free TrialLog in
Avatar of atljarman
atljarman

asked on

ASP .Net C# Alternating Style for each row

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

Avatar of kaufmed
kaufmed
Flag of United States of America image

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

Avatar of atljarman
atljarman

ASKER

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.
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;
I didn't look too closely at your existing logic. Where is persons.Index coming from?
I just pulled it from an example on the web.  I have no idea it that is correct.  Sorry about that.
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

Still no luck
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?
SOLUTION
Avatar of COBOLdinosaur
COBOLdinosaur
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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;}
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks for your help