473,386 Members | 1,973 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

I don't know in advance how many textboxes will appear. How do I get their values?

I've made a form with a variable number of textboxes. The user fills
them
out, and then I need to pick up the values he has filled in. The number
of textboxes vary depending on a value that the user filled in in
another page. But suppose there were 3 values. In that case, I know
the names of the
fields, they are
MyTextBox1
MyTextbox2
MyTextbox3
So my code has to do something like this:
For Index = 1 to 3
myStr = myStr & "MyTextBox" & Index & ".Text"
Next
Of course this does not work. I need some way of getting the value of
these fields. Is there a way to do that?
Thanks,
BD

May 3 '06 #1
5 1683
Hi Black,

You need to loop through the controls in the form and if it's a textbox, get
its value. Here's a little example using ASP.NET 2.0.

Let us know if it helps?

Ken
Microsoft MVP [ASP.NET]

<%@ Page Language="VB" trace="true" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs)

Dim txtbx As TextBox
Dim lit1 As Literal

Dim intCount As Integer
For intCount = 0 To 3
txtbx = New TextBox
txtbx.ID = "textbox" & intCount.ToString
txtbx.Text = "I'm number " & intCount.ToString
txtbx.EnableViewState = True

PlaceHolder1.Controls.Add(txtbx)
lit1 = New Literal
lit1.Text = "<br />"
PlaceHolder1.Controls.Add(lit1)
Next
End Sub

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As
System.EventArgs)
Dim txtbx As TextBox
Dim frm As HtmlForm
Dim plchlder As PlaceHolder
frm = Page.FindControl("form1")
If Not IsNothing(frm) Then
plchlder = frm.FindControl("placeholder1")
If Not IsNothing(plchlder) Then
For Each cntrl As Control In plchlder.Controls
If TypeOf cntrl Is TextBox Then
txtbx = CType(cntrl, TextBox)
Response.Write(txtbx.ID & "=" & txtbx.Text & "<br
/>")
End If
Next
End If
End If

End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:placeholder id="PlaceHolder1" runat="server"></asp:placeholder>
<br />
<br />
<asp:button id="Button1" runat="server" onclick="Button1_Click"
text="Button" /></div>
</form>
</body>
</html>
<BL*********@LYCOS.COM> wrote in message
news:11**********************@y43g2000cwc.googlegr oups.com...
I've made a form with a variable number of textboxes. The user fills
them
out, and then I need to pick up the values he has filled in. The number
of textboxes vary depending on a value that the user filled in in
another page. But suppose there were 3 values. In that case, I know
the names of the
fields, they are
MyTextBox1
MyTextbox2
MyTextbox3
So my code has to do something like this:
For Index = 1 to 3
myStr = myStr & "MyTextBox" & Index & ".Text"
Next
Of course this does not work. I need some way of getting the value of
these fields. Is there a way to do that?
Thanks,
BD

May 3 '06 #2
Ken,
Your code to add a variable number of controls to a page does work.
The part that loops through the controls also works.
However, when I combine your code with my page, it does not work. I
have my controls in a dynamically created table. I need the table,
because I will have several columns of controls. But several things go
wrong. First of all, when I click on Submit, all my textboxes
disappear. The page doesn't maintain them. Secondly, if I try and do
a "for each" loop through the controls, I get no values. (No textboxes
are found in the form). Thirdly, if I try to find a textbox control
like this:
txtbx = place_holder.FindControl("TextBox2")
That also fails. This last method would be useful, because I need
every row of listboxes together, so I can insert them in a row in a
database table.
Anyway, if you are interested in pursuing this, my asp page with all
its code is below, its fairly simple, but it does not work.
BD
<%@Page Language="VB" debug="true" %>
<html>
<head>
<title>Enter Number of Rooms for each day</title>
</head>

<script language="VB" runat="server">
Sub Page_Load(Source As Object, E As EventArgs)
dim tRow as TableRow
dim tCell as TableCell
Dim Index as integer
dim NumDays as integer
dim roomTextBox as TextBox
dim Table2 as Table

If Not Page.IsPostBack Then

'''''''''''''''''''''''''''''''
Table2 = new Table
Table2.BorderWidth = System.Web.UI.WebControls.Unit.Pixel(1)
tRow = new TableRow()
tCell = new TableCell()
tCell.Text = "<b>Textboxes</b>"
tRow.Cells.Add(tCell)
Table2.Rows.Add(tRow)

'''''''''''''''''''''''''''''''''
NumDays = 3 ' in reality this would be variable
For Index = 0 to NumDays
tRow = new TableRow()

''''''''''''''''''''''''''''
roomTextBox = new TextBox()
roomTextBox.ID = "TextBox" & index
roomTextBox.Text = ""
roomTextBox.Columns = 4
roomTextBox.MaxLength = "4"
roomTextBox.EnableViewState = True

tCell = new TableCell()
tCell.Controls.Add(roomTextBox)
tRow.Cells.Add(tCell)
''''''''''''''''''''''''''''
Table2.Rows.Add(tRow)

Next
PlaceHolder1.Controls.Add(table2)
End If
End Sub
'''''''''''''''''''''''''''''''''''''''''''''''
Sub SubmitClick(objSender As Object, objArgs As EventArgs)
dim NumDays as integer
dim Index as integer
dim ctl as Control
dim strTextBoxContents as String
dim strTextBoxID as string
dim strTest as string
dim place_holder as PlaceHolder
dim frm as HtmlForm
dim txtbx as TextBox

If Page.IsValid() and Page.IsPostBack Then
frm = Page.FindControl("form1")
place_holder = frm.FindControl("placeholder1")
txtbx = place_holder.FindControl("TextBox0")
if txtbx is Nothing then
strTest = "Textbox0 is Nothing " & "<br>"
Else
strTest = "Textbox0.ID is " & txtbx.ID & "<br>"
End If
for each ctl in place_holder.controls
If typeof ctl is textbox then
txtbx = ctype(ctl, TextBox)
strTest = strTest & txtbx.id & "<br>"
end if
next
LabDiagnostic.Text = strTest '!!!
End If
End Sub
</script>

<body bgcolor="#FFFFFF">

<form METHOD="POST" id="form1" runat="server">
<center>
<asp:Label id="LabDiagnostic" runat="server" />
<asp:PlaceHolder id="PlaceHolder1" runat="server"></asp:PlaceHolder>

<table border="1">
<tr><td align="center" colspan="2">
<asp:Button id="Button1" runat="server" onclick="SubmitClick"
text="Submit" />
</td></tr>
</table>

</center>
</form>
</body>
</html>
*********************************

May 3 '06 #3
Any time you add controls dynamically you need to recreate them again on
every postback.
If they are not recreated then the values returned from the client are lost.

You need to walk through the controls in your table find the textboxes and
get their values.
there is no solution that fits all.

SA
<BL*********@LYCOS.COM> wrote in message
news:11*********************@i39g2000cwa.googlegro ups.com...
Ken,
Your code to add a variable number of controls to a page does work.
The part that loops through the controls also works.
However, when I combine your code with my page, it does not work. I
have my controls in a dynamically created table. I need the table,
because I will have several columns of controls. But several things go
wrong. First of all, when I click on Submit, all my textboxes
disappear. The page doesn't maintain them. Secondly, if I try and do
a "for each" loop through the controls, I get no values. (No textboxes
are found in the form). Thirdly, if I try to find a textbox control
like this:
txtbx = place_holder.FindControl("TextBox2")
That also fails. This last method would be useful, because I need
every row of listboxes together, so I can insert them in a row in a
database table.
Anyway, if you are interested in pursuing this, my asp page with all
its code is below, its fairly simple, but it does not work.
BD
<%@Page Language="VB" debug="true" %>
<html>
<head>
<title>Enter Number of Rooms for each day</title>
</head>

<script language="VB" runat="server">
Sub Page_Load(Source As Object, E As EventArgs)
dim tRow as TableRow
dim tCell as TableCell
Dim Index as integer
dim NumDays as integer
dim roomTextBox as TextBox
dim Table2 as Table

If Not Page.IsPostBack Then

'''''''''''''''''''''''''''''''
Table2 = new Table
Table2.BorderWidth = System.Web.UI.WebControls.Unit.Pixel(1)
tRow = new TableRow()
tCell = new TableCell()
tCell.Text = "<b>Textboxes</b>"
tRow.Cells.Add(tCell)
Table2.Rows.Add(tRow)

'''''''''''''''''''''''''''''''''
NumDays = 3 ' in reality this would be variable
For Index = 0 to NumDays
tRow = new TableRow()

''''''''''''''''''''''''''''
roomTextBox = new TextBox()
roomTextBox.ID = "TextBox" & index
roomTextBox.Text = ""
roomTextBox.Columns = 4
roomTextBox.MaxLength = "4"
roomTextBox.EnableViewState = True

tCell = new TableCell()
tCell.Controls.Add(roomTextBox)
tRow.Cells.Add(tCell)
''''''''''''''''''''''''''''
Table2.Rows.Add(tRow)

Next
PlaceHolder1.Controls.Add(table2)
End If
End Sub
'''''''''''''''''''''''''''''''''''''''''''''''
Sub SubmitClick(objSender As Object, objArgs As EventArgs)
dim NumDays as integer
dim Index as integer
dim ctl as Control
dim strTextBoxContents as String
dim strTextBoxID as string
dim strTest as string
dim place_holder as PlaceHolder
dim frm as HtmlForm
dim txtbx as TextBox

If Page.IsValid() and Page.IsPostBack Then
frm = Page.FindControl("form1")
place_holder = frm.FindControl("placeholder1")
txtbx = place_holder.FindControl("TextBox0")
if txtbx is Nothing then
strTest = "Textbox0 is Nothing " & "<br>"
Else
strTest = "Textbox0.ID is " & txtbx.ID & "<br>"
End If
for each ctl in place_holder.controls
If typeof ctl is textbox then
txtbx = ctype(ctl, TextBox)
strTest = strTest & txtbx.id & "<br>"
end if
next
LabDiagnostic.Text = strTest '!!!
End If
End Sub
</script>

<body bgcolor="#FFFFFF">

<form METHOD="POST" id="form1" runat="server">
<center>
<asp:Label id="LabDiagnostic" runat="server" />
<asp:PlaceHolder id="PlaceHolder1" runat="server"></asp:PlaceHolder>

<table border="1">
<tr><td align="center" colspan="2">
<asp:Button id="Button1" runat="server" onclick="SubmitClick"
text="Submit" />
</td></tr>
</table>

</center>
</form>
</body>
</html>
*********************************

May 3 '06 #4
Hi Black (it would be nicer to address a real name),

I made some changes to your code that I think is close to what you're after.
Notice that I give the table an ID so it is easier to find and I loop
through all of the rows of the table and cells within the rows and controls
within the cells looking for a textbox.

Let us know if this helps?

Ken
Microsoft MVP [ASP.NET]

<%@ page debug="true" trace="true" language="VB" %>

<html>
<head>
<title>Enter Number of Rooms for each day</title>
</head>

<script runat="server" language="VB">
Sub Page_Load(ByVal Source As Object, ByVal E As EventArgs)
Dim tRow As TableRow
Dim tCell As TableCell
Dim Index As Integer
Dim NumDays As Integer
Dim roomTextBox As TextBox
Dim Table2 As Table

'''''''''''''''''''''''''''''''
Table2 = New Table
Table2.ID = "Table2"
Table2.BorderWidth = System.Web.UI.WebControls.Unit.Pixel(1)
tRow = New TableRow()
tCell = New TableCell()
tCell.Text = "<b>Textboxes</b>"
tRow.Cells.Add(tCell)
Table2.Rows.Add(tRow)

'''''''''''''''''''''''''''''''''
NumDays = 3 ' in reality this would be variable
For Index = 0 To NumDays
tRow = New TableRow()

''''''''''''''''''''''''''''
roomTextBox = New TextBox()
roomTextBox.ID = "TextBox" & Index
roomTextBox.Text = ""
roomTextBox.Columns = 4
roomTextBox.MaxLength = "4"
roomTextBox.EnableViewState = True

tCell = New TableCell()
tCell.Controls.Add(roomTextBox)
tRow.Cells.Add(tCell)
''''''''''''''''''''''''''''
Table2.Rows.Add(tRow)

Next
PlaceHolder1.Controls.Add(Table2)

End Sub
'''''''''''''''''''''''''''''''''''''''''''''''
Sub SubmitClick(ByVal objSender As Object, ByVal objArgs As EventArgs)
Dim NumDays As Integer
Dim Index As Integer
Dim ctl As Control
Dim strTextBoxContents As String
Dim strTextBoxID As String
Dim strTest As String
Dim place_holder As PlaceHolder
Dim frm As HtmlForm
Dim txtbx As TextBox

If Page.IsValid() And Page.IsPostBack Then
frm = Page.FindControl("form1")
place_holder = frm.FindControl("placeholder1")
txtbx = place_holder.FindControl("TextBox0")
If txtbx Is Nothing Then
strTest = "Textbox0 is Nothing " & "<br>"
Else
strTest = "Textbox0.ID is " & txtbx.ID & "<br>"
End If
Dim tbl2 As Table
tbl2 = place_holder.FindControl("Table2")
If Not IsNothing(tbl2) Then
For Each rw As TableRow In tbl2.Rows
For Each cll As TableCell In rw.Cells
For Each obj As Object In cll.Controls
If TypeOf obj Is TextBox Then
txtbx = CType(obj, TextBox)
strTest = strTest & txtbx.ID & "=" &
txtbx.Text & "<br>"
End If

Next
Next
Next
End If
LabDiagnostic.Text = strTest '!!!
End If
End Sub
</script>

<body bgcolor="#FFFFFF">
<form id="form1" runat="server" method="POST">
<center>
<asp:label id="LabDiagnostic" runat="server"></asp:label>
<asp:placeholder id="PlaceHolder1"
runat="server"></asp:placeholder>
<table border="1">
<tr>
<td align="center" colspan="2">
<asp:button id="Button1" runat="server"
onclick="SubmitClick" text="Submit" />
</td>
</tr>
</table>
</center>
</form>
</body>
</html>

<BL*********@LYCOS.COM> wrote in message
news:11*********************@i39g2000cwa.googlegro ups.com...
Ken,
Your code to add a variable number of controls to a page does work.
The part that loops through the controls also works.
However, when I combine your code with my page, it does not work. I
have my controls in a dynamically created table. I need the table,
because I will have several columns of controls. But several things go
wrong. First of all, when I click on Submit, all my textboxes
disappear. The page doesn't maintain them. Secondly, if I try and do
a "for each" loop through the controls, I get no values. (No textboxes
are found in the form). Thirdly, if I try to find a textbox control
like this:
txtbx = place_holder.FindControl("TextBox2")
That also fails. This last method would be useful, because I need
every row of listboxes together, so I can insert them in a row in a
database table.
Anyway, if you are interested in pursuing this, my asp page with all
its code is below, its fairly simple, but it does not work.
BD
<%@Page Language="VB" debug="true" %>
<html>
<head>
<title>Enter Number of Rooms for each day</title>
</head>

<script language="VB" runat="server">
Sub Page_Load(Source As Object, E As EventArgs)
dim tRow as TableRow
dim tCell as TableCell
Dim Index as integer
dim NumDays as integer
dim roomTextBox as TextBox
dim Table2 as Table

If Not Page.IsPostBack Then

'''''''''''''''''''''''''''''''
Table2 = new Table
Table2.BorderWidth = System.Web.UI.WebControls.Unit.Pixel(1)
tRow = new TableRow()
tCell = new TableCell()
tCell.Text = "<b>Textboxes</b>"
tRow.Cells.Add(tCell)
Table2.Rows.Add(tRow)

'''''''''''''''''''''''''''''''''
NumDays = 3 ' in reality this would be variable
For Index = 0 to NumDays
tRow = new TableRow()

''''''''''''''''''''''''''''
roomTextBox = new TextBox()
roomTextBox.ID = "TextBox" & index
roomTextBox.Text = ""
roomTextBox.Columns = 4
roomTextBox.MaxLength = "4"
roomTextBox.EnableViewState = True

tCell = new TableCell()
tCell.Controls.Add(roomTextBox)
tRow.Cells.Add(tCell)
''''''''''''''''''''''''''''
Table2.Rows.Add(tRow)

Next
PlaceHolder1.Controls.Add(table2)
End If
End Sub
'''''''''''''''''''''''''''''''''''''''''''''''
Sub SubmitClick(objSender As Object, objArgs As EventArgs)
dim NumDays as integer
dim Index as integer
dim ctl as Control
dim strTextBoxContents as String
dim strTextBoxID as string
dim strTest as string
dim place_holder as PlaceHolder
dim frm as HtmlForm
dim txtbx as TextBox

If Page.IsValid() and Page.IsPostBack Then
frm = Page.FindControl("form1")
place_holder = frm.FindControl("placeholder1")
txtbx = place_holder.FindControl("TextBox0")
if txtbx is Nothing then
strTest = "Textbox0 is Nothing " & "<br>"
Else
strTest = "Textbox0.ID is " & txtbx.ID & "<br>"
End If
for each ctl in place_holder.controls
If typeof ctl is textbox then
txtbx = ctype(ctl, TextBox)
strTest = strTest & txtbx.id & "<br>"
end if
next
LabDiagnostic.Text = strTest '!!!
End If
End Sub
</script>

<body bgcolor="#FFFFFF">

<form METHOD="POST" id="form1" runat="server">
<center>
<asp:Label id="LabDiagnostic" runat="server" />
<asp:PlaceHolder id="PlaceHolder1" runat="server"></asp:PlaceHolder>

<table border="1">
<tr><td align="center" colspan="2">
<asp:Button id="Button1" runat="server" onclick="SubmitClick"
text="Submit" />
</td></tr>
</table>

</center>
</form>
</body>
</html>
*********************************

May 3 '06 #5
BD wrote:
Thank you Ken, your code worked, and so I can now use a variable number
of table rows of controls in an asp.net page. Now my challenge is to
put in validation controls for each textbox control.
Ken Cox - Microsoft MVP wrote:
Hi Black (it would be nicer to address a real name),

I made some changes to your code that I think is close to what you're after.
Notice that I give the table an ID so it is easier to find and I loop
through all of the rows of the table and cells within the rows and controls
within the cells looking for a textbox.

Let us know if this helps?

Ken
Microsoft MVP [ASP.NET]


May 4 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Lizard King | last post by:
....which control has focus at a given moment? This is what I want. I have two textboxes in a form: one for vendor and one for items. Now, if am at the vendor field and I press the vendor...
303
by: mike420 | last post by:
In the context of LATEX, some Pythonista asked what the big successes of Lisp were. I think there were at least three *big* successes. a. orbitz.com web site uses Lisp for algorithms, etc. b....
0
by: TF | last post by:
Hi, Scenario: I have few TextBoxes on a WinForm, bound to a DataSet (using DataBindings collection). Initially only schema is filled in DataSet (using FillSchema() method), then a new row is...
11
by: ian.davies52 | last post by:
Is there anything I can do about the apparent limit on the number of textboxes that have calculations as their control source on a form or report in ms-access? I have a query that pulls together...
2
by: JC | last post by:
Hi, I am a novice at .net and have created a page of text boxes that are populated from a database. I want the user to be able to change the values in the box then submit the page and have these...
4
by: Jason M | last post by:
Hi, Im very new to c#, so forgive me if this is a really stupid question. Im trying to create a form for entering purchase requests. For each line item I have a quantity, a description unit cost...
2
by: the friendly display name | last post by:
Hello newsgroup.. Following problem: I have two buttons on a page, If I click on button1, an additional textbox should be dynamical created and added to a panel. If I click on button2, one...
5
by: \A_Michigan_User\ | last post by:
I'm using asp.net/vb.net/ado.net to create a very simple user interface. (Everything works if I use STATICALLY created textBoxes... but not when I make them DYNAMICALLY.) 1. Execute a SQL...
2
by: technocraze | last post by:
Hi community experts, I am encountering a problem to display the corresponding values of the combo box into the textboxes. I have chose option 1 & 3 for this implementation but neither is working....
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.