Click here to Skip to main content
15,892,809 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have made onlineExam system.In ExamQuestionsPaper One Question and their options will be display. Below that Question,i have taken 50 Labels.This Labels Says That in This Exam there are 50 Questions.Now If Question 1 is answred by User,then Label1 (From 50 Labels)becomes Green color.
For that I have to Use For Loop.
for (i = 0; i < 50; i++)
                        {
                            //Here Id is QuestionNumber  
                            if (Id == Convert.ToInt16(Lable1.Text))
                            {
                                Lable1.ForeColor = Color.Green;
                            }
                        }


My Problem is How To Find Label ID in FOR Loop.In My code I have written Label1.But I have To find Lable From 50 Lables.

What is Solution of this Problem.
Posted

Well you can get all the controls from the Page. so when you get the controls by their type you can change their color.
Here is a link which explains how you can do it:
Get all controls in web page[^]
A similar code will be:
C#
public void GetUserControls(ControlCollection controls)
{
    foreach (Control ctl in controls)
    {
        if (ctl is UserControl)
        {
            // Do whatever.
        }

        if (ctl.Controls.Count > 0)
            GetUserControls(ctl.Controls);
    }
}

And you can use it as:
C#
GetUserControls(Page.Controls);


Good luck,
OI
 
Share this answer
 
A for loop will be resource intensive i would suggest

C#
//will return all Controls in  ctrlToSearch.Controls whose name is nameofControl
private Control[] FindControlsByName(Control ctrlToSearch, string nameofControl)
{
return ctrlToSearch.Controls.Find(nameofControl, true);
}
 
Share this answer
 

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