473,385 Members | 1,766 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,385 software developers and data experts.

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

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

Apr 14 '06 #1
4 1794
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

Apr 14 '06 #2
On 14 Apr 2006 09:38:39 -0700, BL*********@LYCOS.COM wrote:
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
Apr 15 '06 #3
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

May 2 '06 #4
myStr = Request.Form("MyTextBox" & Index)

"BL*********@LYCOS.COM" wrote:

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

Jun 2 '06 #5

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

Similar topics

11
by: Jim | last post by:
Hi, I keep getting form results emailed to me that would indicate a form from my web site is getting submitted with all fields blank or empty, but my code should preventing users from proceeding...
2
by: news.cablevision.qc.ca | last post by:
Hi! I want, using a form, enter a number and then, read that number of records in a database and store information in variables. The only way I see to do this is to do a FOR...NEXT to the...
19
by: laurenq uantrell | last post by:
I'm using Access 2K. I'm hoping someone can tell me which method performs faster- (currently I'm using a mix of both methods) a.) creating custom properties and then calling functions to set and...
6
by: allyn44 | last post by:
HI--what I am trying to do is 2 things: 1. Open a form in either data entry mode or edit mode depending on what task the user is performing 2. Cancel events tied to fields on the form if I am in...
3
by: Susan Bricker | last post by:
Greetings. I have three forms that are open at the same time. They are related and cascading. The first form (frmEventAdd) is the anchor. Each event can have many Trials. The second form is...
2
by: Nick Gilbert | last post by:
Hi I have a number of pages where it is valid for the user to enter HTML. On these pages, I have turned off RequestValidation ("ValidateRequest = false" in the page directive) so that the...
9
by: Wayne Smith | last post by:
I've come up against a major headache that I can't seem to find a solution for but I'm sure there must be a workaround and I would really be grateful of any help. I'm currently building a web...
5
by: timber910 | last post by:
I posted this in the VB forum but I think its in the wrong place. So I thought I would try here. _________________________________________ Ok, I have a button on a form that I'm using to create...
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: 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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...

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.