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

B

BLACKDOG157

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
 
K

Ken Cox - Microsoft MVP

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>
 
B

BLACKDOG157

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>
*********************************
 
M

MSDN

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
 
K

Ken Cox - Microsoft MVP

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>
 
B

BLACKDOG157

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.
 

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

Forum statistics

Threads
473,743
Messages
2,569,477
Members
44,898
Latest member
BlairH7607

Latest Threads

Top