Dynamically generated webforms?

  • Thread starter Thread starter Dave
  • Start date Start date
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
 
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
 
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
 
Back
Top