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

How would I generate text fields for user input in ASP.NET C#?

Hi,
I'm relatively new at this language, so I've been trying to find parallels
to problems I've run into in the past.

This is the hypothetical problem:
I want to write a CD inventory application. There is a page where it asks
the user for the number of tracks on a CD. After they enter the number of
tracks, I would like to generate that number of text fields on the page (i.e.
if user enters 30, I want to generate 30 text fields), so the user can enter
track titles.

I haven't found any ASP.NET books that talks about this yet, so I asked
around for ideas.

Here is what I heard:
1) Generate the entire form in Javascript and then submit the form. Let the
server-side now deal with it. (I don't like this idea too much, since I
don't want to assume that Javascript is working on the client.)

2) Create a <asp:table> in the aspx page with a column to have an
<asp:textbox> in it. In the code-behind, add new rows to that Table object

3) Try to create a textBox object and add it to Page.Controls collection.
(But I don't have control over the layout anymore.

When I tried to solve this problem in PHP, Coldfusion, or XSL, I could loop
the HTML textfield within a code block. However, this model does not
separate code from presentation any more. Are the above 3 suggestions the
best answers to this problem?

Are there any books or websites that might talk about such topics?

Thank you for your time.
Nov 17 '05 #1
2 2355
Hi there,

Add this inside your aspx file:

<asp:TextBox ID="TextBox1" runat="server" /><br />
<asp:Button ID="Button1" runat="server" Text="Button"
OnClick="Button1_Click" /><br /><br />
<asp:PlaceHolder ID="PlaceHolder1" runat="server" />

Add this method inside your code-behid file:

protected void Button1_Click(object sender, EventArgs e)
{
// Read the number of cd's
int numberOfCd = Convert.ToInt32(TextBox1.Text);

// Add textboxes for tracks
for (int i = 0; i < numberOfCd; i++)
{
// Create textbox
TextBox cdTrack = new TextBox();
cdTrack.ID = "track" + i.ToString();

// Create newline
LiteralControl newline = new LiteralControl();
newline.Text = "<br />";

// Add textbox
PlaceHolder1.Controls.Add(cdTrack);
PlaceHolder1.Controls.Add(newline);
}
}

Hope this helps,
Arjen



"Stephen Tang" <Stephen Ta**@discussions.microsoft.com> schreef in bericht
news:8B**********************************@microsof t.com...
Hi,
I'm relatively new at this language, so I've been trying to find parallels
to problems I've run into in the past.

This is the hypothetical problem:
I want to write a CD inventory application. There is a page where it asks
the user for the number of tracks on a CD. After they enter the number of
tracks, I would like to generate that number of text fields on the page
(i.e.
if user enters 30, I want to generate 30 text fields), so the user can
enter
track titles.

I haven't found any ASP.NET books that talks about this yet, so I asked
around for ideas.

Here is what I heard:
1) Generate the entire form in Javascript and then submit the form. Let
the
server-side now deal with it. (I don't like this idea too much, since I
don't want to assume that Javascript is working on the client.)

2) Create a <asp:table> in the aspx page with a column to have an
<asp:textbox> in it. In the code-behind, add new rows to that Table
object

3) Try to create a textBox object and add it to Page.Controls collection.
(But I don't have control over the layout anymore.

When I tried to solve this problem in PHP, Coldfusion, or XSL, I could
loop
the HTML textfield within a code block. However, this model does not
separate code from presentation any more. Are the above 3 suggestions
the
best answers to this problem?

Are there any books or websites that might talk about such topics?

Thank you for your time.

Nov 17 '05 #2
Hi Arjen,
I didn't know about the Placeholder class. I just read up on Placeholder's
class definition, and what you describe in the code is what I want to do.
And it's a lot cleaner and less complex.

Thank you! I'll go try this out.

"Arjen" wrote:
Hi there,

Add this inside your aspx file:

<asp:TextBox ID="TextBox1" runat="server" /><br />
<asp:Button ID="Button1" runat="server" Text="Button"
OnClick="Button1_Click" /><br /><br />
<asp:PlaceHolder ID="PlaceHolder1" runat="server" />

Add this method inside your code-behid file:

protected void Button1_Click(object sender, EventArgs e)
{
// Read the number of cd's
int numberOfCd = Convert.ToInt32(TextBox1.Text);

// Add textboxes for tracks
for (int i = 0; i < numberOfCd; i++)
{
// Create textbox
TextBox cdTrack = new TextBox();
cdTrack.ID = "track" + i.ToString();

// Create newline
LiteralControl newline = new LiteralControl();
newline.Text = "<br />";

// Add textbox
PlaceHolder1.Controls.Add(cdTrack);
PlaceHolder1.Controls.Add(newline);
}
}

Hope this helps,
Arjen



"Stephen Tang" <Stephen Ta**@discussions.microsoft.com> schreef in bericht
news:8B**********************************@microsof t.com...
Hi,
I'm relatively new at this language, so I've been trying to find parallels
to problems I've run into in the past.

This is the hypothetical problem:
I want to write a CD inventory application. There is a page where it asks
the user for the number of tracks on a CD. After they enter the number of
tracks, I would like to generate that number of text fields on the page
(i.e.
if user enters 30, I want to generate 30 text fields), so the user can
enter
track titles.

I haven't found any ASP.NET books that talks about this yet, so I asked
around for ideas.

Here is what I heard:
1) Generate the entire form in Javascript and then submit the form. Let
the
server-side now deal with it. (I don't like this idea too much, since I
don't want to assume that Javascript is working on the client.)

2) Create a <asp:table> in the aspx page with a column to have an
<asp:textbox> in it. In the code-behind, add new rows to that Table
object

3) Try to create a textBox object and add it to Page.Controls collection.
(But I don't have control over the layout anymore.

When I tried to solve this problem in PHP, Coldfusion, or XSL, I could
loop
the HTML textfield within a code block. However, this model does not
separate code from presentation any more. Are the above 3 suggestions
the
best answers to this problem?

Are there any books or websites that might talk about such topics?

Thank you for your time.


Nov 17 '05 #3

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

Similar topics

1
by: Øyvind Isaksen | last post by:
Hello! I need to dynamic generate a SQL statement based on how many images a user select to upload. Here you see an example with 2 images. It can be up to 50 images and I dont want to write...
1
by: Ken K | last post by:
I need to get some simple text input from the user with a vb.net form(not asp.net). Using the input value I need to generate a GET or a POST to a web server. Considering I build a vb.net form,...
4
by: WB | last post by:
Hi, How can I generate web controls such as textboxes and drop-menus on the fly? My web application allows users to fill out PDF forms online. There are many PDF forms, and my application...
1
by: lennyw | last post by:
Hi I'm trying to use XSLT to do an xml to xml transformation where the output xml contains summary data on the information in the input xml. I've succesfully done a Muenchian grouping of the...
1
by: peck2000 | last post by:
Related to my earleir post ... this is the same project to re-purpose the Classifieds application in BEGINNING ASP 3.0 (Wrox) to a comicbook database ... This is a brainteaser that should have...
3
by: cleary1981 | last post by:
Hi, I know how to generate xml from php but is there an easier way to generate xml from my code? <?php $quote=$_GET; //echo $quote; require "config.php"; $q1 = mysql_query("SELECT * FROM...
1
by: divyac | last post by:
I am doing an inventory project using PHP in which there is a text box.The customer id should be entered in that text box.If that customer id is valid,another text box should be generated in which...
7
by: kirkgilbert | last post by:
I am trying to do an onchange event in a form using a text field. The form is tied to a record set that is part of a repeated region. One the first record when I edit the data it works perfectly. ...
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
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.