array of textboxes

S

samneefs

I'm wondering if there's a way to create an array of textboxes, or loop
through textboxes. The textboxes are in an ASP.NET page, but the
looping happens through Visual Basic.
On page load, textboxes are dynamically created like this:

While Not rs.eof
Response.Write("<tr><td><input type='text' name='article'" &
numberOfRows & "' value='0'></td></tr>")
numberOfRows += 1
rs.movenext()
End While


Next, when you hit a button ("Order"), I want to check which of these
textboxes has a value different than 0.
The goal is to allow people to order a certain number of items (a
simple webshop, basically).
A possible way to do this:

Dim i As Integer, txt As String
For i = 0 To numberOfRows
txt = "article" & i
If txt.value <> "0" Then
Session.Contents("product" & i) = txt.value
End If
Next

So I want to check for each textbox if it contains the value '0', and,
if not, add the number of items you want to a session variable. Of
course, I still need to assign the article code of it too, but that's
for afterwards (unless someone's got a suggestion...). As you can
guess, it doesn't work at all like this. So in this newsgroup I
found something like this:

For Each ctl As Control In Page.Controls
If TypeOf (ctl) Is TextBox And ctl.ID <> "txtaccount" And ctl.ID <>
"txtpaswoord" Then
If ctl <> "0" Then
session.contents(ctl.id) = ctl.value
End If
End If
Next

However, ctl.value etc. doesn't work, either. If anyone has an idea,
please tell. I don't have any idea how to do arrays in asp.net though,
as my knowledge of it is still very limited, so please don't use too
difficult explanations.

Thanks a lot!
 
S

Scott Allen

HI Samneefs:

For Each ctl As Control In Page.Controls
If TypeOf (ctl) Is TextBox And ctl.ID <> "txtaccount" And ctl.ID <>
"txtpaswoord" Then
If ctl <> "0" Then
session.contents(ctl.id) = ctl.value
End If
End If
Next

If you coherce the control object into being a TextBox type, then you
can get the Text property directly. First check to see if the control
is a TextBox (If TypeOf Ctl Is TextBox), and use DirectCast to change
the type to TextBox.

Note: you will need to check the Controls property of each control if
the TextBox controls are nested.

HTH,
 
S

samneefs

Many thanks for your help.
I've looked up the DirectCast function in MSDN, and tried to apply it
(to be honest I don't really understand it completely, mostly why I
need to put a variable before it (as in dim a as b = DirectCast(c,d)).

What I've got now looks like this:

For Each ctl As Control In Page.Controls
If TypeOf (ctl) Is TextBox And ctl.ID <> "txtaccount" And ctl.ID <>
"txtpaswoord" Then
'txtaccount & txtpaswoord are not part of the textboxes we want to
check for values

Dim txt As TextBox = DirectCast(ctl, TextBox)
If txt.Text <> "0" Then
'Session.Contents(txt.ID) = txt.Text
'aantalSessionVariables += aantalSessionVariables

Me.cmdBestellen.Text = "ok"
End If
End If
Next

I wanted to change the caption of cmdBestellen (meaning
cmdPlaceYourOrder in English) to see if it worked, but it didn't (the
caption didn't change).
Any suggestion to what could be wrong?

(Also, do you (or anyone else) know if it's possible to create an array
of session variables? I'd like to assign both the id and the value of a
textbox in an array's "row", if possible).

Many thanks for your suggestions, it's greatly appreciated!
 
S

Scott Allen

I suspect Page.Controls only holds the form control, and the textboxes
are another level lower (in Page.Controls(0).Controls). You'll have to
keep moving down the control hierarchy - know what I mean?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,738
Latest member
JinaMacvit

Latest Threads

Top