create vb array of webform textbox controls?

R

Rich

Hello,

I have a webform with 10 textbox (input) controls that I
would like to loop through to add data to a datasource.
In vb.net I say Dim arrTxt As Textbox() = New Textbox()
{txt1, txt2,...} --- then loop through the array to put
data from textboxes into a datarow

I am trying to do this in Asp.Net but does not seem to be
working out. First question is if this is possible to
do? Question2 is if yes, could someone suggest the corect
way to do this? Maybe an array of objects? Or is there a
better way? Or am I limited to

Dim DR As Datarow
DR = DS1.Tables("tbl1").NewRow
DR(0) = txt0.Text
DR(1) = txt1.Text
....

Thanks,
Rich
 
M

Marina

Something like:
Dim tArray(2) As Textbox

Then, you instantiate every slott in tArray as a new textbox, and set its
properties.

Then, you had to add every textbox you have just created to the page itself,
or to another container element on the page (like a panel).

At this point though, I am not sure what having the textboxes in an array
buys you. You can just create the textboxes and add them to the page in a
loop.
 
R

Rich

Thank you for your reply. Basically, I have a database
(sql Server 2k) and I want to add a new row to a table in
the DB. I am using a dataAdapter object and a dataset
object. In vb.net I use a datarow to add data from a form
to the dataset and then update the db with
dataAdapter.Update(dataset1,"tbl1"). I am thinking this
is how you do it with Asp.Net also. So in vb.net I loop
through my array of textboxes. Isn't that a way I could
do it with Asp.net?

So what you suggest is this?
Dim tArray(2) As Textbox
tArray(0) = New txt0
tArray(1) = New txt1
tArray(2) = New txt2

wouldn't that be similar to
Dim tArray As Textbox = New Textbox() {txt0, txt1, txt2}

I am just asking because I am still kind of new to this
stuff and still trying to sort out the logic.

Thanks
Rich
 
M

Marina

tArray(0) = New txt0
would only be valid, if 'txt0' was a class inheriting from the Textbox
class. Since I assume you don't have a separate class for each of your
textboxes, this wouldn't compile.
It would be more like: tArray(0) = New Textbox

I guess I am still not following why you need arrays, or why you are
manually adding textboxes to a grid? I am not really understanding what you
are trying to do at all, but I hope that helped.
 
R

Rich

I have a webform (aspx page). On this page I have 10
textbox controls, txt0, txt1, txt2, ...txt9
I want to retrieve data from these textboxes that a user
has entered from the web and store this data in a table in
a database. Right now I am doing this:

Dim dr As DataRow
dr = dataset1.Tables("tbl1").NewRow
dr(0) = txt0.text
dr(1) = txt1.text
dr(2) = txt2.text
....
dataset1.Tables("tbl1").Rows.Add(dr)
dataAdapter.Update(dataset1, "tbl1")

I was hoping I could use an array of textboxes that I
could loop through instead of listing each textbox as
above. Is there a better way to add data to a table in a
database from an aspx page than this?
 
M

Marina

I see what you are saying, yes, you could then define the array as you have
done, and populate it with references to your textboxes that are already one
the page. You could then loop through it and populate it that way.

Also, remember, there is a FindControl method, that can find a control given
a name. So you could call FindControl("txt" & i), in order to get the i'th
textbox, and then get its text property.

Also, it seems that it might be easier to just construct the SQL statement
to add the row, then going through all the trouble of data adapters, etc.
 
R

Rich

Thank you for this reply. The FindControl method sound
kind of interesting. I may experiment with that. And,
truth be told, I am just staring out with the dotnet
environment. Yes, sql would be easier. old asp is
easier :). But I have to start migrating things to
asp.net. Right now I only know how to do data transfer
using dataAdapters, datasets, and soforth (using visual
studio 2003).

So, with FindControl, I could create a string array with
the actual textbox names (not really txt0, txt1, txt2,
more like txtID, txtfirstName, txtLastName,
txtPhone, ...). Just writing directly to a datarow per
textbox

dr(0) = txtID.text
dr(1) = txtFirstName.Text
....

this is working, but not too sophisticated. I will try
FindControl. But also, may I ask how you could set up an
array of textboxes?

Dim tArray(9) As Textbox
tArray(0) = ? txtID new txtID, Form1.txtID

I am actually having a problem here. Ideally I would like
to be able to do this:

Dim tArray As TextBox() = New Textbox() {txtID,
txtFirstName, ...}

But I don't know if System.Web.UI.Page supports this. Is
there something (namespace) that I need to import?

Thanks again for your help,
Rich
 
R

Rich

Thanks. Yes, Page.Controls. I think that is what I was
looking for. Since my controls have all kinds of names I
could just place those in a string array and loop through
that using Page.Controls. Asp.Net will take a little
getting used to for me (my regular asp proficiency was
kind of average, so I am still learning it all).

Many thanks for this suggestion. I think this one should
do it. Although, FindControls still seems interesting.

Rich
-----Original Message-----
Rich, I concur that you want to end up with an array of
textboxes so you can iterate them generically, but you've
already got that "array" (it's a collection) in
page.controls.
Maybe something like this.

' To build the controls...
dim txt as textbox
For i As Integer = 0 To 9
txt = New TextBox
txt.Text = "My initial value"
Page.Controls.Add(txt) --> or even,
page.controls.add(new textbox) if you don't need to set
other properties
Next

' To get data from the controls...
For Each ctl As Control In Page.Controls
If TypeOf (ctl) Is TextBox Then
' Map my textbox values back to the datarow, etc.
End If
Next

If you don't want to treat all the textboxes in this same
way, or you want to save a few cycles by not having to
skip over the labels, etc., you need some other container,
such as a panel as Marina suggests, just so you have a way
to iterate them (you could also use a naming scheme in the
id, etc.).
hth,

Bill

----- Rich wrote: -----

I have a webform (aspx page). On this page I have 10
textbox controls, txt0, txt1, txt2, ...txt9
I want to retrieve data from these textboxes that a user
has entered from the web and store this data in a table in
a database. Right now I am doing this:

Dim dr As DataRow
dr = dataset1.Tables("tbl1").NewRow
dr(0) = txt0.text
dr(1) = txt1.text
dr(2) = txt2.text
....
dataset1.Tables("tbl1").Rows.Add(dr)
dataAdapter.Update(dataset1, "tbl1")

I was hoping I could use an array of textboxes that I
could loop through instead of listing each textbox as
above. Is there a better way to add data to a table in a
database from an aspx page than this?

- ----Original Message-----
tArray(0) = New txt0
would only be valid, if 'txt0' was a class
inheriting
from the Textbox
class. Since I assume you don't have a separate
class for
each of your
textboxes, this wouldn't compile.
It would be more like: tArray(0) = New Textbox
arrays, or
why you are
manually adding textboxes to a grid? I am not
really
understanding what you
are trying to do at all, but I hope that helped.
 
G

Guest

You'll find that FindControl *is* way interesting, you just likely don't need it in this case. For one, FindControl is great when you have a datalist/datagrid with a bunch of textboxes, buttons, etc., and on selection of one of those lines you want to get at the control to do something. You can either use the ordinal value of the cells collection and pray you remember right (and that another programmer hasn't slipped in a new column, etc.) or you can use FindControl. Good luck

----- Rich wrote: ----

Thanks. Yes, Page.Controls. I think that is what I was
looking for. Since my controls have all kinds of names I
could just place those in a string array and loop through
that using Page.Controls. Asp.Net will take a little
getting used to for me (my regular asp proficiency was
kind of average, so I am still learning it all)

Many thanks for this suggestion. I think this one should
do it. Although, FindControls still seems interesting

Ric
-----Original Message----
Rich, I concur that you want to end up with an array of
textboxes so you can iterate them generically, but you've
already got that "array" (it's a collection) in
page.controls
dim txt as textbo
For i As Integer = 0 To
txt = New TextBo
txt.Text = "My initial value
Page.Controls.Add(txt) --> or even,
page.controls.add(new textbox) if you don't need to set
other propertie
Nex
For Each ctl As Control In Page.Control
If TypeOf (ctl) Is TextBox The
' Map my textbox values back to the datarow, etc
End I
Nex
way, or you want to save a few cycles by not having to
skip over the labels, etc., you need some other container,
such as a panel as Marina suggests, just so you have a way
to iterate them (you could also use a naming scheme in the
id, etc.)
hth
Bil
----- Rich wrote: ----
I have a webform (aspx page). On this page I have
10
textbox controls, txt0, txt1, txt2, ...txt
I want to retrieve data from these textboxes that a user
has entered from the web and store this data in a table in
a database. Right now I am doing this
Dim dr As DataRo
dr = dataset1.Tables("tbl1").NewRo
dr(0) = txt0.tex
dr(1) = txt1.tex
dr(2) = txt2.tex
...
dataset1.Tables("tbl1").Rows.Add(dr
dataAdapter.Update(dataset1, "tbl1"
I was hoping I could use an array of textboxes that
I
could loop through instead of listing each textbox as
above. Is there a better way to add data to a table in a
database from an aspx page than this
----Original Message----
tArray(0) = New txt
would only be valid, if 'txt0' was a class
inheriting
from the Textbo
class. Since I assume you don't have a separate
class for
each of you
textboxes, this wouldn't compile
It would be more like: tArray(0) = New Textbo
arrays, or
why you ar
manually adding textboxes to a grid? I am not
really
understanding what yo
are trying to do at all, but I hope that helped
 

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

Top