This is a migrated thread and some comments may be shown as answers.

Edit and Insert template shown in listview after failed insert operation

3 Answers 99 Views
ListView
This is a migrated thread and some comments may be shown as answers.
Richard
Top achievements
Rank 1
Richard asked on 21 Dec 2011, 11:42 PM
We have a RadListview which is binding to a DataSource control and have item, edit and insert templates.
The datasource control takes care of selecting the object when the page loads and does the data bind.  The datasource control (actually a CslaDataSource control) binds to our business layer objects which are enumerable, and we just show one "record" in the listview.
Really we are making use of the templating to show display, edit and insert modes for our record/object.

We also tend to have our own user controls also in the templates and pass the business object down to them so they can bind to their respective controls.

All works well.

Except we have one slight issue.  When inserting a new record and attempting a save, the datasource control xxx_Update method gets fired and if the business object throws an error (typically as some business rule is broken and it cannot be saved yet), the RowsAffected is zero and we set a PageHasErrors flag.

The page posts back and in our Page_PreRender event we try and switch the listview back into Insert mode as we want to keep it there in that template as the save failed.  Unfortunately this doesn't quite work as we end up with the display item template shown first and then the insert template below.

It almost looks like it has created 2 records in the listview.

Any ideas?

3 Answers, 1 is accepted

Sort by
0
Iana Tsolova
Telerik team
answered on 27 Dec 2011, 12:43 PM
Hello,

You can try handling the ItemInserted and ItemUpdated event of the RadListView which fire after automatic data insert/update is performed (through DataSource control). And there when exception is thrown, or based on some custom conditions set the KeepInInsertMode or KeepInEditMode flags to true:
void lv_ItemUpdated(object sender, RadListViewUpdatedEventArgs e)
{
    e.KeepInEditMode = true;
}
 
void lv_ItemInserted(object sender, RadListViewInsertedEventArgs e)
{
    e.KeepInInsertMode = true;
}

Give it a try and let me know if this works for you.

Greetings,
Iana Tsolova
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now
0
Richard
Top achievements
Rank 1
answered on 05 Jan 2012, 06:25 PM
This worked but the values in the controls are lost.  The form remains in the insert template.

Are my ItemCreated and/or ItemDataBound events causing the problem now?

protected void TemplatesHolder_ItemCreated(object sender, Telerik.Web.UI.RadListViewItemEventArgs e)
        {
            if (e.Item.ItemType == RadListViewItemType.DataItem || 
                e.Item.ItemType == RadListViewItemType.InsertItem ||
                e.Item.ItemType == RadListViewItemType.EditItem)
            {
                //get current item.
                RadListViewDataItem currentItem = (RadListViewDataItem)e.Item;
 
                //and pass it to usercontrols so they can bind it initially.
                MainControl = ((Main)e.Item.FindControl("MainData"));
 
                if (!IsPostBack)
                    MainControl.Model = FactoryManager.GetModel();
                else if (currentItem.DataItem != null && MainControl.Model == null)
                    MainControl.Model = (POSTracker.Library.BusinessCase)currentItem.DataItem;
            }
        }
 
        protected void TemplatesHolder_ItemDataBound(object sender, Telerik.Web.UI.RadListViewItemEventArgs e)
        {
 
            if (e.Item.ItemType == RadListViewItemType.DataItem ||
               e.Item.ItemType == RadListViewItemType.InsertItem ||
               e.Item.ItemType == RadListViewItemType.EditItem)
            {
                //get current item.
                RadListViewDataItem currentItem = (RadListViewDataItem)e.Item;
 
                //and pass it to usercontrols so they can bind it initially.
                MainControl = ((Main)e.Item.FindControl("MainData"));
 
                if (!IsPostBack)
                    MainControl.Model = FactoryManager.GetModel();
                else if (currentItem.DataItem != null && MainControl.Model == null)
                    MainControl.Model = (POSTracker.Library.BusinessCase)currentItem.DataItem;
            }
        }

0
Andrey
Telerik team
answered on 06 Jan 2012, 05:05 PM
Hello Richard,

It is normal to loose the user's data in the insert form after a postback has been performed. Edit item values are preserved because they are fetched from the database every time the item gets in Edit mode. But with insert item you could not automatically load the old values. However, you could create a hashtable to store the user input from the insert form, and then you could store this hashtable in the Session object. You could check this help topic for more information on getting user input in hashtable.

Additonally, you do not need to execute one and the same code in both ItemCreated and ItemDataBound events. You could place your code either in the ItemDataBound event or in the ItemCreated.

Give this suggestion a try and check if it works for you.


All the best,
Andrey
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now
Tags
ListView
Asked by
Richard
Top achievements
Rank 1
Answers by
Iana Tsolova
Telerik team
Richard
Top achievements
Rank 1
Andrey
Telerik team
Share this question
or