Reputation: 4807
My C# web app requires the user to enter automobile information. The user can add information for one auto, or more (max 20).
My page has these text fields: Car Number, Car Make, Car Year, Mileage, VIN I have a button that allows the user to "Add More Cars" The idea is to allow the user to add more than one car.
When the user clicks "Add More Cars", how can I dynamically display the text boxes, and keep track of how many cars the user has added (in order to load them to an array and write to database)
Any other ideas that would make this objective easier? The control will only be used to enter data, not to view any records.
Upvotes: 2
Views: 869
Reputation: 3445
I like using a placeholder and adding user controls. It works quite nicely and is pretty modular. Here is a quick example where I add a few controls based on a list. You will notice I pass an object to my user control, allowing it to hide or show itself, display certain options etc based on permissions. You could easily modify this code to add upon an event:
for (int i = 0; i < comments.Count; i++)
{
controls.Comment commentCtrl = (controls.Comment)Page.LoadControl("~/userControls/Comment.ascx");
commentCtrl.ID = "commentUserCtrl" + i;
commentCtrl.setComment((myCommentObj)comments[i]);
placeHolderComments.Controls.Add(commentCtrl);
}
Upvotes: 1
Reputation: 2090
you could do the following:
Further more you could make a user control (ASCX file) of the text box for more control like handling events or adding a validator for each text box. You will then have to load the user control with LoadControl("YourTextBox.ascx") each time you add a new one to the container.
Upvotes: 1
Reputation: 50728
Use a ListView to show a list of the cars entered. Create an InsertItem template that has the template to insert a new car with, and on insert, have it update the DB by reloading the records, allowing them to insert. You can wrap this with an UpdatePanel to make it asynchronous.
OR
If you want a pure client-side approach, use JavaScript to programmably create each entry, and then use a common scheme for creating the controls. You can then check the form collection for posted values and extract all of the values, or stream the data back to teh server via web services from the client.
HTH.
Upvotes: 0