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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

control arrays

Status
Not open for further replies.

faisalsham

Programmer
Mar 11, 2005
18
0
0
US
Hello all, I'm new to ASP. I have a text box control array created dynamically and I need to find a way to refer to the nth text box. I'm not sure how to do that. The code below just creates the text boxes and stores some value in them. Now how can I refer back to the nth text box through code? Thanks in advance.

Here's my code:
Code:
<%
while not objrs.EOF
  EntityPK = objrs.fields("ENTITY")
%>
<input type = "text" name="PK" value="<%=EntityPK%>">
<%
objrs.MoveNext
wend
%>
 
Are all of your textboxes named "PK"? Might it not be somewhat easier to refer to them in some sort of order that would differentiate them, such as "PK1", "PK2", etc...? Something like this, perhaps?
Code:
<%
dim i
i = 0
while not objrs.EOF
  EntityPK = objrs.fields("ENTITY")

response.write "<input type = ""text"" name=""PK"" value=""" & EntityPK & i & """>"

objrs.MoveNext
i = i + 1
wend
%>

------------------------------------------------------------------------------------------------------------------------
"Men occasionally stumble over the truth, but most of them pick themselves up and hurry off as if nothing ever happened."
- Winston Churchill
 
Do you want to reference them after the form is posted or in the same page right after you create them?

If it's the latter than you are out of luck. Once you write data to the response buffer (Response.Write) you can't modify it, it's either on it's way to the user's browser or waiting in the buffer to be flushed to the user. Your only option is to output it the way you want the first time around.

If it's the former than your in luck. Any time your form submits multiple values the browser sends them as a comma-space delimited string. So you can either use For Each or Split to loop or create an array of the values. In this case you might be looking for Split:
Code:
[b]NextPage.asp[/b]

Dim arr_PK
arr_PK = Split(Request.Form("PK"),", ")

'output the 5th value
If UBound(arr_PK) >= 5 Then
   Response.Write arr_PK(5)
End If

One word of warning though: There is no guarantee that the browser will pass them back to you in the same order you wrote them. Usually it's a better idea to number the fields so you can be guaranteed that your referencing the one you think you are:
Code:
[b]FirstPage.asp[/b]
<%
'lots of code, a form tag, etc

Dim ctr
ctr = 0
while not objrs.EOF
   EntityPK = objrs.fields("ENTITY")
   Response.Write "<input type = ""text"" name=""PK_" & ctr & """ value=""" & EntityPK & """>"

   ctr = ctr + 1
   objrs.MoveNext
wend
Response.Write "<input type=""hidden"" name=""PK_count"" value=""" & ctr & """>"

'more code, end form tag, and so on


[b]NextPage.asp[/b]
Dim pk_count
pk_count = Request.Form("PK_count")

'loop through all of them
Dim ctr
For ctr = 0 to pk_count - 1
   Response.Write "#" & ctr & ": " & Request.Form("PK_" & ctr) & "<br/>"
Next

'or just access the 5th one
If pk_count >= 5 Then
   Response.Write Request.Form("PK_5")
End If


Hope this helps,
-T

signature.png
 
you can't really have a control array in ASP the same way you do with VB...

because to ASP it is just some text to output... ASP has no clue how the text will be interpretted by the browser... ASP has no clue about the DOM (cocument object model)... as far as ASP is concerned, the code you post might as well be this:
<%
dim i
i = 0
while not objrs.EOF
EntityPK = "someone set us up the bomb"
response.write EntityPK
objrs.MoveNext
i = i + 1
wend
%>

What I mean is that the stuff you write out in the loop has zero significance to ASP... it only matters to a web browser.

So see what Chopstik was getting at was changing the text so it will make more sense to the browser... another way to do it would be to use the id property of the textbox.
 
Thanks for the clarification... I also just realised that I did my code incorrectly... I meant to number the name property, but the premise remains the same...

------------------------------------------------------------------------------------------------------------------------
"Men occasionally stumble over the truth, but most of them pick themselves up and hurry off as if nothing ever happened."
- Winston Churchill
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top