Click here to Skip to main content
15,885,365 members
Please Sign up or sign in to vote.
2.33/5 (2 votes)
See more:
Hi,

I have problem to add styles in for loop.

I want to print 10 names using for loop. I want to change color 8th number name.

Next page refreshing I want change the color next name dynamically. I mean next page load I will change color 2nd number name.

Here is my code changes:

C#
int currentpg = 3;
int currentpage = 7;
int RowCount = 130;
for (int i = currentpg; i < (RowCount / 10); i++)
{
    LinkButton lnk = new LinkButton();
    if (currentpage != 0)
    {
        if (currentpg == currentpage)
        {
            lnk.Style.Add("fone-size", "14px");
            lnk.Style.Add("color", "red");
            //lnk.Attributes.Add("Style", "color:#F24F09");

            lnk.Font.Bold = true;
        }
    }

    lnk.Click += new EventHandler(lbl_Click);
    lnk.ID = "lnkPage" + (i + 1).ToString();
    lnk.Text = (i + 1).ToString();
    plcPaging.Controls.Add(lnk);
    Label spacer = new Label();
    spacer.Text = " ";
    plcPaging.Controls.Add(spacer);
}


Please Help me.

Thank you.
Posted
Updated 19-Jun-14 20:38pm
v2
Comments
Thanks7872 20-Jun-14 2:20am    
And we are suppose to guess the code you have used?

So move the colour outside the loop, and change it when teh counter hits your "magic number":
C#
string styleColour = "red";
for (int i = currentpg; i < (RowCount / 10); i++)
{
    if (i == magicNumber) styleColour = "green";
    LinkButton lnk = new LinkButton();
    if (currentpage != 0)
    {
        if (currentpg == currentpage)
        {
            lnk.Style.Add("fone-size", "14px");
            lnk.Style.Add("color", styleColour);
 
Share this answer
 
for (int i = currentpg; i < (RowCount / 10); i++)
            {
                
                LinkButton lnk = new LinkButton();
                if (currentpage != 0)
                {
                    if ((i+1) == currentpage)
                    {
                        lnk.Style.Add("fone-size", "14px");
                        lnk.Style.Add("color", "#10B1CA");
                        //lnk.Attributes.Add("Style", "color:#F24F09");

                        lnk.Font.Bold = true;
                    }
                }
            
                lnk.Click += new EventHandler(lbl_Click);
                lnk.ID = "lnkPage" + (i + 1).ToString();
                lnk.Text = (i + 1).ToString();
                plcPaging.Controls.Add(lnk);
                Label spacer = new Label();
                spacer.Text = "&nbsp;";
                plcPaging.Controls.Add(spacer);
            }
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900