473,406 Members | 2,345 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Referring to controls via loops - comments and suggestions


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
Nov 17 '05 #1
8 1392
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

Nov 17 '05 #2
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

Nov 17 '05 #3
In message <#D*************@TK2MSFTNGP12.phx.gbl>, Steve Le Monnier
<st*********@nospam.please.hotmail.com> writes

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.

--
Steve Walker
Nov 17 '05 #4
In message <#D*************@TK2MSFTNGP12.phx.gbl>, Steve Le Monnier
<st*********@nospam.please.hotmail.com> writes

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.

--
Steve Walker
Nov 17 '05 #5

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[i].Text = "Department";

}

Cheers

Steve Le Monnier
"Steve Walker" <st***@otolith.demon.co.uk> wrote in message
news:2+**************@otolith.demon.co.uk...
In message <#D*************@TK2MSFTNGP12.phx.gbl>, Steve Le Monnier
<st*********@nospam.please.hotmail.com> writes

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.

--
Steve Walker

Nov 17 '05 #6

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[i].Text = "Department";

}

Cheers

Steve Le Monnier
"Steve Walker" <st***@otolith.demon.co.uk> wrote in message
news:2+**************@otolith.demon.co.uk...
In message <#D*************@TK2MSFTNGP12.phx.gbl>, Steve Le Monnier
<st*********@nospam.please.hotmail.com> writes

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.

--
Steve Walker

Nov 17 '05 #7
In message <#m**************@TK2MSFTNGP14.phx.gbl>, Steve Le Monnier
<st*********@nospam.please.hotmail.com> writes
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[i].Text = "Department";

}


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

--
Steve Walker
Nov 17 '05 #8
In message <#m**************@TK2MSFTNGP14.phx.gbl>, Steve Le Monnier
<st*********@nospam.please.hotmail.com> writes
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[i].Text = "Department";

}


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

--
Steve Walker
Nov 17 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: Alf P. Steinbach | last post by:
The third part of my attempted Correct C++ tutorial is now available, although for now only in Word format (use free Open Office if no Word), and also, it's not yet been extensively reviewed -- ...
0
by: Steve Le Monnier | last post by:
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...
8
by: Invalidlastname | last post by:
Hi, We are developing an asp.net application, and we dynamically created certain literal controls to represent some read-only text for certain editable controls. However, recently we found an issue...
24
by: Kunal | last post by:
Hello, I need help in removing if ..else conditions inside for loops. I have used the following method but I am not sure whether it has actually helped. Below is an example to illustrate what I...
4
by: Lewis Holmes | last post by:
Hi I have the following situation in one of my asp.net pages. The user can add multiple table rows to a form by selecting a button. These rows can contain asp.net controls. When this button is...
4
by: TS | last post by:
i am wondering why the name of the controls are listed in parameters of request object instead of ID. The name always uses the default '$' for encapsulations and the ID uses '_'. When i use...
5
by: gerry | last post by:
I am trying to create a custom container control that will only ever contain a specific type of control. At design time, when a control of a different type is added to the container I would like...
6
by: =?Utf-8?B?TWFyaw==?= | last post by:
Hi... Tried posting this on Build Controls but then saw that's a pretty slow group... Kind of a typical request from product management - they want to be able to swap in different 3rd party...
4
by: Ken Fine | last post by:
I've been living with a frustrating issue with VS.NET for some months now and I need to figure out what the problem is. Hopefully someone has run into the same issue and can suggest a fix. I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.