I would like to share this with everyone because I had a very hard time finding answers and please feel free to correct me if I am wrong.
I was stock for two days trying to figure out how to handle a Formview view state when insertion fails. Every time my insert fails, I would lose all the user's data.
The only way I was able to retain this data was to move the code form the CSLA data source InsertObject event handler to the FormView ItemInserting event handler and then I set e.cancel = true if the effected rows returned from the Save() helper method is 0.
Protected Sub fvProjectInsert_ItemInserting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.FormViewInsertEventArgs) Handles fvProjectInsert.ItemInserting
Dim RowEffected As Integer = 0
Try
Dim obj As ProjectBOobj = ProjectBO.NewProjectBO()
Csla.Data.DataMapper.Map(e.Values
)RowEffected = SaveProject(obj)
If RowEffected = 0 Thene.Cancel =
True End If Catch ex As Data.SqlClient.SqlException Me.lblError.Text = ex.Messagee.Cancel =
True Catch ex As Csla.DataPortalException Me.lblError.Text = ex.BusinessException.Messagee.Cancel =
True Catch ex As Exception Me.lblError.Text = ex.Messagee.Cancel =
True End Try End Sub
I can hardly believe that this is the optimal solution. I'm having the same situation when my code catches a rules exception and I want to display the original data in my formview.
Has anyone else encountered this?
Did you just replace the code in the InsertTemplate on the form or did you change the initial mode to edit? Or both?
When I changed the initial mode to edit, I got a blank screen. Obviously, the code in UpdateObject needs to be changed too. Anybody got a sample? Where I'm a little lost is how/why the datasource gets fooled by the change in mode. I see that the "insert" or "update" buttons are how the events get started, but I don't think I really understand what needs to be done (differently) in the UpdateObject so that the initial insert can be performed.
All this being said, has no progress been made on this issue since last year. I've seen one extensive thread discussing this a year ago and, since it is such a vital part of data entry, I would have thought some changes would have been made. Anybody know of anything?
Moe,
Thanks for clearing up some of my confusion. I created the ItemInserting and it works as advertised.
My only problem now is the code doesn't know what to do once the save is successful! Ideally, I want the saved record displayed in formview readonly mode. I guess my question is: what code do I use to re-display the form and where should I place it?
av_harris,
I don't know how you are switching between your templates but what I have done is as fallows,
In my <ItemTemplate> which is my read only view, I have three buttons
<
asp:Button ID="Button2" runat="server" CausesValidation="False" CommandName="Edit"Text="Edit" /><
asp:Button ID="Button3" runat="server" CausesValidation="False" Text="New" CommandName="New" /><
asp:Button ID="Button4" runat="server" CausesValidation="False" CommandName="Delete" Text="Delete" OnClick="DeleteButton_Click" OnClientClick="return confirm('Are you certain you want to delete this project?');" />In <EditItemTemplate>
<asp:Button ID="UpdateButton2" runat="server" Text="Save" CommandName="Update" />
<
asp:Button ID="UpdateCancelButton2" runat="server" Text="Cancel" CommandName="Cancel" OnClick="UpdateCancelButton_Click" />In <InsertItemTemplate>
<asp:Button ID="InsertTempBtnInsert" runat="server" Text="Insert" CommandName="Insert" /><
asp:Button ID="InsertTempBtnCancel" runat="server" Text="Cancel" CommandName="Cancel" />Whatever "CommandName" you assign to your button, your Form view should handle by default without having to write any server side code.
Now when your save is sucessful (don't set e.cancel = True) the form view will automatically switch back to the default mode (which is by default <ItemTemplate>). if you set your default mode to <EditItemTemplate> or whatever then you need to explicitly call the fallowing method inside your ItemUpdating event handler and below your update method call,
FormView1.ChangeMode(FormViewMode.ReadOnly)
I hope this answers your question!
Good Luck,
Moe
Moe,
Thanks for all the help. I think I have it under control now. As usual, it is the combination of something new (CSLA) and something incredibly convoluted (.Net events!) that makes implementation of any solution more difficult than it needs to be.
Copyright (c) Marimer LLC