Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reference ASP:Text Controls Programmatically

Status
Not open for further replies.

Steve101

Programmer
Mar 29, 2002
1,473
AU
I am sure that the answer to this is quite simple, but I am just not seeing it:

I have a number of text controls on a web page; for example:
<asp:TextBox ID="ETAC1" runat="server"></asp:TextBox>
<asp:TextBox ID="ETAC2" runat="server"></asp:TextBox>
<asp:TextBox ID="ETAC3" runat="server"></asp:TextBox>
<asp:TextBox ID="ETAC4" runat="server"></asp:TextBox>
Whilst I can happily reference these individually; for example:
ETAC1.Text = 10
ETAC2.Text = 20
ETAC3.Text = 30
ETAC4.Text = 40
I have not been able to figure how I can reference these controls programmatically. For example, I would like to do something like:
For i = 1 to 4
thisControlId = "ETAC" & i
Controls(ThisControlId).Text = i*10 ' <-----------
Next
Unfortunately I am unable to figure out the syntax for the above hilited row. Some help would really be appreciated.

Also, I note when I inspect the HTML of the page, that I am using ASP controls without them being surrounded by a <FORM> ... </FORM> tag pair. Is this the reason why I only get a value of 1 returned when I interrogate me.Controls.Count. Do I need these tags to do what I want to do, (or is it just good practice anyway).

Thanks in advance,


Steve Lewy
Solutions Developer
SimplyData
simplydata.com.au
(dont cut corners or you'll go round in circles)
 
Allthough you should post in ASP.Net forum, here it is:

dim i as integer
dim cn as string
for i=1 to 4
cn="ETAC" & i.tostring
ctype(me.findcontrol(cn),textbox).text=10*i
next


Hope This Helps :)
 
TipGiver,

Have tried this. It returns the exception:

"NullReferenceException was unhandled by user code"
"Object Reference not set to an instance of an object"

This is the code snippet I was testing on:
' ETAC1.Text = "123" '<--- This works fine

cn = "ETAC1"
CType(Me.FindControl(cn), TextBox).text = "XXXXX" <----- This doesnt

Any idea of how to resolve this?
Cheers,


Steve Lewy
Solutions Developer
SimplyData
simplydata.com.au
(dont cut corners or you'll go round in circles)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top