Variable Type?

  • Thread starter Thread starter Jeff Johnson
  • Start date Start date
J

Jeff Johnson

Hi Everyone,

I have a page with numerous label controls with the Visible attribute set to
false.

On page_load I loop through an array and depending on what is returned for
each label, I want to set the Visible attribute to true as well as a number
of other attributes.

Instead of having a lengthy switch statement listing all of the label
controls, I wanted to know if I could use a variable for the name of these
label controls and set their attributes in a loop.

What type of variable could I use to accomplish this? I've tried object
without success.

Thanks,

Jeff
 
Jeff,

I would use the "as" statement for this. Basically, you would loop
through all of your controls, and in the loop, you do:

// Get the label. pobjControl is the current control in the enumeration.
Label pobjLabel = pobjControl as Label;

// If the control is a label, then pobjLabel will not be null here, and the
Visible property can be set.
if (pobjLabel != null)
// Set the visible property.
pobjLabel.Visible = false;

Hope this helps.
 
Jeff Johnson said:
I have a page with numerous label controls with the Visible attribute set to
false.

On page_load I loop through an array and depending on what is returned for
each label, I want to set the Visible attribute to true as well as a number
of other attributes.

Instead of having a lengthy switch statement listing all of the label
controls, I wanted to know if I could use a variable for the name of these
label controls and set their attributes in a loop.

What type of variable could I use to accomplish this? I've tried object
without success.

The best way of doing this is to have the label controls in an array to
start with, rather than in individual variables.
 
Hi Jeff,

Beside Jon's suggestion you can also use the Page.Control collection to
iterate in the controls of the page, this will be better if you do not have
another use of the labels, in such escenario you don;t have to declare them
in the code behind page, hence decreasing the code to write/maintain.

It will be somethig like this:
foreach( Control control in Controls )
{
if ( control is Label )
{
//see if it needs to be processed.
}

}


Now as you probable have more labels in the page you will need to have a
form of identify those that needs to be changed. Maybe using a template name
: DynamicLabel1 , DynamicLabel2 , etc

Cheers,
 
Back
Top