Bhav
Bhav

Reputation: 2207

Populate labels in a loop

I have 5 labels titled lblQuestion1, lblQuestion2, lblQuestion3... and as part of the loop below, when i=0, I want lblQuestion1.Text = reader["answer1"].ToString(); ...

i=1 --> lblQuestion2.Text = reader["answer1"].ToString(); ...

i=2 --> lblQuestion3.Text = reader["answer1"].ToString(); ......

However, this doesn't work so can someone suggest an alternative method.

    for (int i = 0; i < 5; i++)
    {
        try
        {
            conn.Open();

            string cmdText = "SELECT * FROM questions ORDER BY RAND() LIMIT 1";
            MySqlCommand cmd = new MySqlCommand(cmdText, conn);

            reader = cmd.ExecuteReader();

            if (reader.Read())
            {
                if (!(list.Contains(reader["question_id"].ToString())))
                {
                    list.Add(reader["question_id"].ToString());
                    //lblQuestion[i+1].Text = reader["answer1"].ToString();
                }
            }
            else
            {
                lblError.Text = "(no questions found)";
            }
            reader.Close();
        }
        catch
        {
            lblError.Text = "Database connection error - failed to insert record.";
        }
        finally
        {
            conn.Close();
        }
    }

Upvotes: 1

Views: 1322

Answers (2)

Mark Hall
Mark Hall

Reputation: 54562

To answer your comment to my Comment. Since FindControl takes a string as an input you would just create the string with the component name you are looking for. Be aware that you have to use the FindControl Method of the container that your labels are in.

From Link(emphasis mine):

Use FindControl to access a control from a function in a code-behind page, to access a control that is inside another container, or in other circumstances where the target control is not directly accessible to the caller. This method will find a control only if the control is directly contained by the specified container; that is, the method does not search throughout a hierarchy of controls within controls. For information about how to find a control when you do not know its immediate container, see How to: Access Server Controls by ID.

so something like this should work for you.

 ((Label) this.FindControl("lblQuestion" + (i+1))).Text = reader["answer" + (i+1)].ToString();

Upvotes: 1

Andrew Morton
Andrew Morton

Reputation: 25066

You can put references to the labels in an array and access them with that. I put five labels on a form (leaving their names as the defaults) and used this code as an example:

private void SetLabelsText()
{
    // Put references to the labels in an array
    Label[] labelsArray = { label1, label2, label3, label4, label5 };
    for (int i = 0; i < labelsArray.Count(); i++)
    {
        labelsArray[i].Text = "I am label " + (i + 1).ToString();
    }
}

private void Form1_Load(object sender, EventArgs e)
{
    SetLabelsText();
}

Upvotes: 3

Related Questions