Link to home
Start Free TrialLog in
Avatar of zachvaldez
zachvaldezFlag for United States of America

asked on

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.
Avatar of zachvaldez
zachvaldez
Flag of United States of America image

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of Robberbaron (robr)
Robberbaron (robr)
Flag of Australia 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
no, I call the rptforecast databind inside the  dropdown_selectedchangedIndex event
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

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 ?
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.
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

to see events, select the control in design mode, then the properties window, change to Events.
User generated image
in what event are you trying to set the colors ?  is lastrow ever greater than index ?
last row and next to last row
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 ?
thanks!