Is it possible to make a variable number of form fields?

B

BLACKDOG157

I have to make a form with a variable number of textboxes. If, in the
previous screen, the user chose a 2 day hotel stay, I need to input the
number of rooms for 2 days. But if in the previous screen, he chose a
5 day hotel stay, then I need to input the number of rooms for 5 days.

I tried a 'for' loop - something like this
for Number = 1 to NumRooms
strDiv = strDiv & "<ASP:Textbox id="room" & Number runat=""server""
/>
Next
and then I did "DivResult.InnerHtml = strDiv"
But that does not work. When I do a 'view-source' I see that the
source has:
<ASP:Textbox id="room1" runat="server" />
<ASP:Textbox id="room2" runat="server" />
etc.
But that means that asp.net is not interpreting the strings.
Any help is appreciated.
-- BD
 
C

Chris Fulstow

Hi,

To add a variable number of TextBox controls at runtime, you should
first add a Placeholder control to your web form, then add your text
boxes dynamically in Page_Load, e.g.:

TextBox roomTextBox = new TextBox();
roomTextBox.ID = "TextBox" + counter.ToString();
roomTextBox.Text = "This is room " + counter.ToString();
placeholder.Controls.Add(roomTextBox);

HTH,
Chris
 
O

Otis Mukinfus

I have to make a form with a variable number of textboxes. If, in the
previous screen, the user chose a 2 day hotel stay, I need to input the
number of rooms for 2 days. But if in the previous screen, he chose a
5 day hotel stay, then I need to input the number of rooms for 5 days.

I tried a 'for' loop - something like this
for Number = 1 to NumRooms
strDiv = strDiv & "<ASP:Textbox id="room" & Number runat=""server""
/>
Next
and then I did "DivResult.InnerHtml = strDiv"
But that does not work. When I do a 'view-source' I see that the
source has:
<ASP:Textbox id="room1" runat="server" />
<ASP:Textbox id="room2" runat="server" />
etc.
But that means that asp.net is not interpreting the strings.
Any help is appreciated.
-- BD

I have an example of this at my web site:

http://www.otismukinfus.com/vault/vault.htm

Good luck with your project,

Otis Mukinfus
http://www.arltex.com
http://www.tomchilders.com
 
B

BLACKDOG157

I've made a form with a variable number of fields. The user fills them
out, and then I need to pick up the values he has filled in.
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
 
G

Guest

myStr = Request.Form("MyTextBox" & Index)

I've made a form with a variable number of fields. The user fills them
out, and then I need to pick up the values he has filled in.
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
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top