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?
 
Robberbaron (robr)Connect With a Mentor 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
 
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
 
zachvaldezAuthor Commented:
no, I call the rptforecast databind inside the  dropdown_selectedchangedIndex event
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.