Change background color of a last 2 rows of repeater.One yellow ,one blue

I would like to change the background color of the 2 lasts rows in a repeater control. These are the total values.
zachvaldezAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

zachvaldezAuthor Commented:
Here, I was able to get the number of rows using..

 int lvl = rptForeCast.Items.Count;

since I store the variable, how can I change the background of the rows of the repeater.
lvl  is the last row. change to yellow
lvl-1 is the second to last change to blue
0
Robberbaron (robr)Commented:
do you call the rptForeCast.DataBind() from Page_Load ?

if not, appears best to do so.  https://stackoverflow.com/questions/219467/asp-net-repeater-itemdatabound-happening-after-prerender-event

this makes it load before the PreRender event.

then, in the PreRender event of rptForeCast try this

    protected void rptForeCast_PreRender(object sender, EventArgs e)
    {
        int lvl = rptForeCast.Items.Count;
        RepeaterItem lastrow = rptForeCast.Items[lvl-1];
        foreach (Control ctl in lastrow.Controls)
        {
            if (ctl.GetType() == typeof(Label)) {
                Label td = (Label)ctl;
                td.BackColor = Color.Yellow;
            }
        }

        lastrow = rptForeCast.Items[lvl - 2];
        foreach (Control ctl in lastrow.Controls)
        {
            if (ctl.GetType() == typeof(Label))
            {
                Label td = (Label)ctl;
                td.BackColor = Color.Blue;
            }
        }
    }

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
zachvaldezAuthor Commented:
no, I call the rptforecast databind inside the  dropdown_selectedchangedIndex event
0
OWASP: Avoiding Hacker Tricks

Learn to build secure applications from the mindset of the hacker and avoid being exploited.

zachvaldezAuthor Commented:
Thanks, I added your code in the selectedchangedEvent  hoping it would pick up from there but is not rendering the colors. Here is the code


    dr = acct.GetGLForecastInfo(acctselected, fiscalyear);

            if (dr.HasRows)
            {
                rptForeCast.DataSource = dr;
                rptForeCast.DataBind();

                dr.Close();

                int lvl = rptForeCast.Items.Count;

                RepeaterItem lastrow = rptForeCast.Items[lvl - 1];
                foreach (Control ctl in lastrow.Controls)
                {
                    if (ctl.GetType() == typeof(Label))
                    {
                        Label td = (Label)ctl;
                        td.BackColor = System.Drawing.Color.Yellow;
                    }
                }

                lastrow = rptForeCast.Items[lvl - 2];
                foreach (Control ctl in lastrow.Controls)
                {
                    if (ctl.GetType() == typeof(Label))
                    {
                        Label td = (Label)ctl;
                        td.BackColor = System.Drawing.Color.Blue;
                    }
                }


            }

        }

Open in new window

0
Robberbaron (robr)Commented:
is the item count correct after databind ?

do you have labels in your repeater or some other control ?  my code only changes background of labels but should work for textboxes as well.

what happens if you copy the section of code to PreRender event ?
0
zachvaldezAuthor Commented:
This is one thing I couldn't figure out quickly is to bring out the events of a repeater or any control in the page.
IN previous MS Visual Studio , all I have to do was to select the control and view code to expose the events. How do you do it for example, to quickly expose the repeater pre render event? Just a question.
0
zachvaldezAuthor Commented:
I think I'm very close to it. This code colors yellow the last row but does not work for the row before the last row to red

 
{
                        HtmlTableRow row1 = (HtmlTableRow)(ri.FindControl("row_itemtemplate"));

                        RepeaterItem item = row1.NamingContainer as RepeaterItem;
                        int index = item.ItemIndex;
                       
                        if(index==lastrow.ItemIndex-1)
                        { row1.Attributes.Add("style", "background-color:red"); }
                        if (index == lastrow.ItemIndex)
                        { row1.Attributes.Add("style", "background-color:yellow"); }

Open in new window

0
Robberbaron (robr)Commented:
to see events, select the control in design mode, then the properties window, change to Events.
selecting events view
in what event are you trying to set the colors ?  is lastrow ever greater than index ?
0
zachvaldezAuthor Commented:
last row and next to last row
0
Robberbaron (robr)Commented:
sry dont understand..

in what event are you trying to set the colors ?  is lastrow ever greater than index ?

post the code of the event where the colors are being set.  
have you added a breakpoint on it to see what the values of index is ?
0
zachvaldezAuthor Commented:
thanks!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.