Referring to controls via loops - comments and suggestions

  • Thread starter Thread starter Steve Le Monnier
  • Start date Start date
S

Steve Le Monnier

If you have a number of controls be they TextBoxes, Combo Boxes or Labels;
is there a way to refer to the controls in a loop via their name, given the
fact that the names are the same with the exception of a number at the end
e.g.

Label1
Label2
Label3

I know that you can use the controls within the Controls Collection, but can
one append the loop counter on the end and valuate this as a statement a bit
like this e.g.

Label + i.ToString()

Comments and suggestion gratefully received.

Steve Le Monnier
 
Hi,
If you know the names of the control at compile time, then using the
loop is of no use as every time you have to find the control using
Page.FindControl() method and then typecast it to appropriate control
type. I think in this case if you access the control directly then that
will be a lot more efficient.
But if you have generated them at runtime then may be you have to go by
this way only.

HTH,
angrez
 
Steve Le Monnier said:
If you have a number of controls be they TextBoxes, Combo Boxes or Labels;
is there a way to refer to the controls in a loop via their name, given the
fact that the names are the same with the exception of a number at the end
e.g.

Label1
Label2
Label3

I know that you can use the controls within the Controls Collection, but can
one append the loop counter on the end and valuate this as a statement a bit
like this e.g.

Label + i.ToString()

Comments and suggestion gratefully received.

What is it that you are trying to achieve by this? There may be another
approach.
 
I was trying to reduce the amount of code to write when preparing the
controls and they all need to behave the same way, hence tryin to refer to
each one via a loop.

I know this isn't the in thing to say, but VB6 allowed you to append strings
to the end of a control and then evaluate the entire statement as a command
line and not text.

That said, I have found a better way of doing it and that is loading all my
controls into an array and then referencing them via the array e.g.

_lnkCostCenters[0] = this.lnkCostCentre1;

_lnkCostCenters[1] = this.lnkCostCentre2;

_lnkCostCenters[2] = this.lnkCostCentre3;

for ( int i = 0; i < _controlCount; i++ )

{

_lnkCostCenters.Text = "Department";

}

Cheers

Steve Le Monnier
 
Steve Le Monnier said:
What is it that you are trying to achieve by this? There may be another
approach.

I was trying to reduce the amount of code to write when preparing the
controls and they all need to behave the same way, hence tryin to refer to
each one via a loop.

I know this isn't the in thing to say, but VB6 allowed you to append strings
to the end of a control and then evaluate the entire statement as a command
line and not text.

That said, I have found a better way of doing it and that is loading all my
controls into an array and then referencing them via the array e.g.

_lnkCostCenters[0] = this.lnkCostCentre1;

_lnkCostCenters[1] = this.lnkCostCentre2;

_lnkCostCenters[2] = this.lnkCostCentre3;

for ( int i = 0; i < _controlCount; i++ )

{

_lnkCostCenters.Text = "Department";

}


Ah, I see. You could always subclass the control.
 
Back
Top