sydney wynn
sydney wynn

Reputation: 31

how to loop through some ASP.NET labels to set their attributes?

How do I do this in a loop.

protected void ddlTool_SelectedIndexChanged(object sender, EventArgs e)
{

    lblTool1.Visible = false;
    txtTool1.Visible = false;
    lblTool2.Visible = false;
    txtTool2.Visible = false;
    lblTool3.Visible = false;
    txtTool3.Visible = false;
    lblTool4.Visible = false;
    txtTool4.Visible = false;
    lblTool5.Visible = false;


    if (ddlTool.SelectedValue == "1")
    {
        lblTool1.Visible = true;
        txtTool1.Visible = true;
    }
    if (ddlTool.SelectedValue == "2")
    {
        lblTool1.Visible = true;
        txtTool1.Visible = true;
        lblTool2.Visible = true;
        txtTool2.Visible = true;
    }
    if (ddlTool.SelectedValue == "3")
    {
        lblTool1.Visible = true;
        txtTool1.Visible = true;
        lblTool2.Visible = true;
        txtTool2.Visible = true;
        lblTool3.Visible = true;
        txtTool3.Visible = true;
    }
    if (ddlTool.SelectedValue == "4")
    {
        lblTool1.Visible = true;
        txtTool1.Visible = true;
        lblTool2.Visible = true;
        txtTool2.Visible = true;
        lblTool3.Visible = true;
        txtTool3.Visible = true;
        lblTool4.Visible = true;
        txtTool4.Visible = true;
    }

Upvotes: 2

Views: 2864

Answers (5)

Chris Taylor
Chris Taylor

Reputation: 53709

Here is an example with no error checking, but should match your code functionality.

protected void ddlTool_SelectedIndexChanged(object sender, EventArgs e) 
{
  int selectedValue = int.Parse(ddlTool.SelectedValue.ToString());
  for (int i = 1; i <= 4; ++i)
  {
    bool state = i <= selectedValue;
    this.Controls["lblTool" + i.ToString()].Visible = state;
    this.Controls["txtTool" + i.ToString()].Visible = state;
  }
}

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1502386

Instead of having a separate variable for each textbox and label, have a collection of them - whether that's a List<T> or an array or whatever.

Then you can do:

// Potentially use int.TryParse here instead
int visibleLabels = int.Parse(ddlTool.SelectedValue);
for (int i = 0; i < labels.Count; i++)
{
    labels[i].Visible = (i < visibleLabels);
    textBoxes[i].Visible = (i < visibleLabels);
}

(Alternatively use two loops, one to set some Visible properties to true, and one to set some to false.)

Upvotes: 7

MusiGenesis
MusiGenesis

Reputation: 75346

foreach (Control ctrl in this.Controls)
{
    int index = (int)ctrl.Name.Substring(ctrl.Name.Length - 1);
    int maxIndex = (int)ddlTool.SelectedValue;
    ctrl.Visible = (index <= maxIndex);
}

Upvotes: 0

Greg D
Greg D

Reputation: 44086

Use a UserControl for each set of connected controls and then enable/disable the UserControl instead of all the component controls. This is classic, basic modularization of your user interface.

Note that this will still require a little "redundant" code because you're working with an unusual UI paradigm by enabling up-to the ddlTool's selected value of your control. E.g., create your user control that contains a single Label and TextBox. Call it LabeledTextBox or something similar. Then you'd create a collection of your labeled text boxes and enable them up to int.Parse(ddlTool.SelectedValue) - 1.

Upvotes: 1

Pierre-Alain Vigeant
Pierre-Alain Vigeant

Reputation: 23103

You can access a control by its name using

container.Controls["nameofcontrol"]

So technically, you could use this to lookup your control

(Untested code)

for(int index = 1; index <= Convert.ToInt32(ddlTool.SelectedValue); index++)
{
    this.Controls["lblTool" + index.ToString()].Visible = true;
    this.Controls["txtTool" + index.ToString()].Visible = true;
}

Upvotes: 2

Related Questions