?
Solved

ASP .Net C# Alternating Style for each row

Posted on 2013-02-06
12
Medium Priority
?
340 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
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 1000 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 1000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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. …
FAQ pages provide a simple way for you to supply and for customers to find answers to the most common questions about your company. Here are six reasons why your company website should have a FAQ page
This tutorial walks through the best practices in adding a local business to Google Maps including how to properly search for duplicates, marker placement, and inputing business details. Login to your Google Account, then search for "Google Mapmaker…
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

801 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