views:

224

answers:

1

Hi All,

I have a TextBox entry field where the user will enter a integer value. And then there is a "Create" button, which when clicked upon must generate a Table with 2 columns : "Name" and "Email" being the column headers.

I want each row to have a textbox in each of these columns.

All of this has to happen after the button is clicked. I have discovered that if you dynamically add a control in ASP.NET(I am using C#) then the controls are lost during postback. And I don't know how to prevent that from happening.

Can somebody please give me some ideas regarding how to go about adding rows dynamically to a table (I tried using the asp.net Server side table control but ran into the "lost-during-postback" problem - can I try with something else like a gridview ? but afaik a GV will not work without data bound to it )

Point to note is that my table has textboxes for user entry and it is not for showing data ..rather it is for accepting data from the user which will be later used to persist details to the database.

A: 

Dynamic Controls

That's involved. Here's an interesting article on the issue of dynamic controls.

I think normally if you create dynamic controls then you're responsible for recreating them on postback; however the article contains a way to use the Init event if it's copacetic with your app.

Edit:

or Regular ASP.NET Controls

You can use data bound ASP.NET controls like: DataList, Repeater, GridView, etc. (You can put text boxes and all other kinds of controls into them for repeating in each row - or "item") and bind values to it. If you need to perform some processing on the user input first, then generate an internal Array, List, Dictionary, DataTable etc. (your choice) and bind that data as a data source to the ASP.NET control of choice.

aspnetControl.DataSource = myDataTable;
aspnetControl.DataBind();

or

aspnetControl.DataSourceId = "name_of_control";

There are various ways to assign a data source.

This way you won't run into the same problems as with dynamic control creation; however there is a lot to learn to facilitate this way too.

John K
Thanks but isn't there a simpler way to do this ?
Sandeep K Ram
I made an edit although I don't know if it's any simpler. I will leave it to somebody else to provide either a more detailed answer or an easier answer altogether.
John K