5

I'm developing an asp.net page that allows users to enter / edit multiple records. I am using a Repeater control to display the entry fields for each record. I would like to have an "Add" button that when clicked, inserts a new, blank set of entry fields. The user should be able to enter the new record, then click the add button multiple times and continue adding new records. Then, when the user clicks the Save button, those new records will be saved along with the changes to the existing records. If the user does not click Save, the records should be discarded and not written to the database.

Is there any way to implement this "add" functionality with the Repeater control?

1
  • From experience, this isn't an easy one to put together, mainly because of the event cycle... you really want dynamic controls to be added as part of the Init, but that is way before the Click event of the button, etc. Not easy, but do-able Commented Jul 12, 2012 at 14:44

1 Answer 1

1

There is a rather simple solution for this. Assuming you have some form of View Model that temporarily holds the data for the whatever the user is entering, you can simply add an additional "blank" ViewModel to the collection that the repeater uses as it's datasource. On save, you could simply save all the modifications to the database that exist in your View Model. A possible example would be:

    protected void btnAdd_OnCommand(object sender, EventArgs e)
    {
        List<Item> items = (List<Item>)this.ItemRepeater.DataSource;
        items.Add(new Item() { Id = -1 });
        this.ItemRepeater.DataBind();
    }

    protected void btnSave_OnCommand(object sender, EventArgs e)
    {
        List<Item> items = (List<Item>)this.ItemRepeater.DataSource;

        foreach (Item itm in items)
        {
            if (itm.Id == -1)
            {
                //Save New Item
            }
            else
            {
                //Update existing item
            }
        }
    }

The major caveat here is that you must repopulate the ItemRepeater's datasource with the View Models updated with any changes the user made (that you would wish to keep/show).

Sign up to request clarification or add additional context in comments.

6 Comments

The .DataBind will lose the existing values. You can use my "add one row per round trip" solution and only really save when they click such a button. That's how my existing implementation using this functionality works.
@MarkHurd I think you misread my example. If you notice, I'm assuming the user is populating the DataSource appropriately. I am then retrieving the datasource, and modifying it, and re-databinding. This will cause the additional item to be DataBound along with the original items. The only trick here left for implementation is storing the objec that is the DataSource in stateful storage between postbacks.
Can you confirm there is a way to DataBind and not lose the live existing changes? Or are you just saying "always store the live changes so the DataBind rebinds the current values"?
You must keep the state of your view models synchronized with the state of the view. I'm not sure there's a good reason (in my provided approach) to NOT do this.
Yeah, it's just a different approach I suppose. If you don't .DataBind on PostBack, the "live" settings are retained for you.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.