Array Question???

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Can someone please tell me how I can automatically generate the textbox
numbers in this statement...

EG:
- tbMenuItem1.text, tbMenuItem2.text etc etc...
- tbMenuItem(i) doesn't work

I would actually also like to create the FOR statement so it automatically
checked how many textboxes there was in the form so it new the number of
times to loop and create the correct number of SQL parameters...

Any help appritiated...

<CODE>

For i As Integer = 0 To 11
'Gets values of menu text textbox and sends it to the database... For
menu text
cmd.Parameters.Add(New SqlParameter("@mItem" & (i),
SqlDbType.NVarChar, 255)).Value = tbMenuItem1.Text
Next
 
Tim::.. said:
Can someone please tell me how I can automatically generate the textbox
numbers in this statement...

EG:
- tbMenuItem1.text, tbMenuItem2.text etc etc...
- tbMenuItem(i) doesn't work

I would actually also like to create the FOR statement so it automatically
checked how many textboxes there was in the form so it new the number of
times to loop and create the correct number of SQL parameters...

Any help appritiated...

<CODE>

For i As Integer = 0 To 11
'Gets values of menu text textbox and sends it to the database... For
menu text
cmd.Parameters.Add(New SqlParameter("@mItem" & (i),
SqlDbType.NVarChar, 255)).Value = tbMenuItem1.Text
Next

I would recommend storing the number of textboxes you have on the webform in
ViewState which will make things easier. I don't have VS in front of me, so
this is untested, but should work.

========= code below =========

Dim intTextBoxCount As Integer = ViewState("TextBoxCount")

For i As Integer = 0 To intTextBoxCount - 1

cmd.Parameters.Add(New SqlParameter("@mItem" & i.ToString(), _
SqlDbType.NVarChar, 255)).Value = _
CType(Page.FindControl("tbMenuItem" & i.ToString()), TextBox).Text

Next

Good luck,
Ben
 
Tim::.. said:
Thanks...

But how does the ViewState count the testboxes???

It depends, but I'm guessing that you are creating the textboxes, so if you
create 5 of them (for example), then you can store the number 5 in ViewState
right after you've created them. i.e.:

ViewState("TextBoxCount") = 5

Probably the easiest way to do this is when you are creating and adding the
textboxes to your webform, use an integer variable to keep track of how many
textboxes you have created. Something like:

Dim intCount As Integer

For i As Integer = 0 To 4
' Your code that creates a textbox goes here
intCount += 1
Next

.... at this point, intCount contains the number of textboxes you have
created. So just assign intCount to ViewState("TextBoxCount").

Hopefully this will work for your situtation.

Ben
 
Thanks for the help Ben!


Ben Amada said:
It depends, but I'm guessing that you are creating the textboxes, so if you
create 5 of them (for example), then you can store the number 5 in ViewState
right after you've created them. i.e.:

ViewState("TextBoxCount") = 5

Probably the easiest way to do this is when you are creating and adding the
textboxes to your webform, use an integer variable to keep track of how many
textboxes you have created. Something like:

Dim intCount As Integer

For i As Integer = 0 To 4
' Your code that creates a textbox goes here
intCount += 1
Next

.... at this point, intCount contains the number of textboxes you have
created. So just assign intCount to ViewState("TextBoxCount").

Hopefully this will work for your situtation.

Ben
 
Hi Tim


foreach(Control ctl in this.Controls)
{
if ctl.GetType().IsInstanceOfType(TextBox)
{
//do something....
}
}
 
Back
Top