Dynamically generated webforms?

D

Dave

Hi!

I'm an experienced C++ developer, but am new to ASP.NET. I need to develop
a database querying application that we need here, and would like to try to
do it in ASP.NET and C#.

The thing that I'm not sure is doable, is that I'd like to have support for
buttons on the form that, when clicked, will add new text input fields into
the form. So, the form needs to be dynamically "growable", using
self-modifying code within the ASP itself.

Can this be done?

TIA

- Dave
 
M

Manohar Kamath

Yes you can. You can use the Controls.Add() method to add new controls to
the page.

Dim newText As New TextBox()
newText.ID = "myID"

Page.Controls.Add(newText)

One thing to remember is to re-create these controls on a postback, and be
able to maintain the state. For that, there is a great article at:

http://www.codeproject.com/aspnet/retainingstate.asp
 
K

Karl Seguin

Yes. Fairly easily..here's some pseudo/real code:

void button_click(args){
TextBox t = new TextBox();
t.id = "userName";
APlaceHolderOnYourPage.Controls.Add(t);
}

You'll find the tricky thing is that those controls aren't "permenantly"
added to the page. That is, if you click the button again, or another
button, you need to re-add all items that were added. Denis Bauer has a
placeholder that is meant to help you do that (never used it)
http://www.denisbauer.com/ASPNETControls/DynamicControlsPlaceholder.aspx or
you can simply track everything you added in the viewstate and then readd
those items before adding anything new (which is what his control probably
does for you).

Karl
 

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,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top