Skip Navigation LinksHome / Community & Support / Developer Productivity Tools Forums / ASP.NET AJAX > ListView > Edit and Insert template shown in listview after failed insert operation

Not answered Edit and Insert template shown in listview after failed insert operation

Feed from this thread
  • Richard avatar

    Posted on Dec 21, 2011 (permalink)

    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?

    Reply

  • Iana Tsolova Iana Tsolova admin's avatar

    Posted on Dec 27, 2011 (permalink)

    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

    Reply

  • Richard avatar

    Posted on Jan 5, 2012 (permalink)

    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;
                }
            }

    
    				

    Reply

  • Andrey Andrey admin's avatar

    Posted on Jan 6, 2012 (permalink)

    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

    Reply

Back to Top

Skip Navigation LinksHome / Community & Support / Developer Productivity Tools Forums / ASP.NET AJAX > ListView > Edit and Insert template shown in listview after failed insert operation
Related resources for "Edit and Insert template shown in listview after failed insert operation"

ASP.NET ListView Features  |  Documentation  |  Demos  |  Telerik TV  |  Self-Paced Trainer  |  Step-by-step Tutorial  ]