Wednesday, March 28, 2012

Success Page or Feedback on Insert or Update

How do I make it so that when someone inserts a new record or updates a current record that it either takes them to a success page or shows feedback right in the page they are in. Right now when I do an update or insert, it goes into the database, but there is no feedback on the page indicating success or failure.

Hey,

Well, you could setup a multiview, which then you switch to the other multiview page, bind the single insert result, and let them know of the status; if there is an error with the result, you may want to stay in the current view and display the problem with an error message, allowing them to re-edit their information. But, it depends how you want to set it up. Do you want to see the initial result and have the information there, or do you want a separate confirmation page?

Ultimately, that is up to your design; it can be done however you prefer.


I am really new to ASP.NET 2. I have several forms throughout my site now, but none of them give feedback on insert or update. I am not sure what multiview is though. Can you direct me to a good tutorial on this? I appreciate the prompt response.

I would just be happy to even just have it say success under the insert or update button at this point.


Okay, I found the Multiview and read up on it a little. Never notived that button before. Big Smile

So, now I have this code:

 <asp:MultiView ID="MultiView1" runat="server"> <asp:View ID="View1" runat="server"> An error occured while submitting your form</asp:View> <asp:View ID="View2" runat="server"> You have successfully submitted your information</asp:View> </asp:MultiView>

I have a formview called FormView1 with an Insert Button. So, I have to tell it when it inserts correctly to show view two and when there is an error to show view 1 right?

Do I need to do something to the insert button or to the formview to make this happen?


Okay, I am reading now about this:

ItemInsertedOnItemInsertedFormViewInsertedEventArgs

Am I on the right track?


Hey,

Yes, though if that is all you are showing, you could also use a label for that instead, if that is easier for you. Basically override the FormView's ItemInserted/ItemDeleted, which has an e.Exception property, and if this object exists, an error occurred. Make sure to set e.ExceptionHandled to true with your approach. You'll also want to log it maybe, in case the problem isn't with the data, but with something else.


Whatever solution you decide to go with, please be sure to confirm that a page refresh does not cause a duplicate record to be inserted. Me personally, I have 1 confirmation page for my project and I perform a redirect on completion. The confirmation page uses session variables I set on the previous page to dictate how the confirmation page should appear. Seems to work well for me.


How do you do direct to the page once the form is submitted?

Okay, so I found this microsoft article and I think this is what I am trying to do:

http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.formviewinsertedeventargs.aspx

So, I have the insert form with a label named MessageLabel.

In my aspx.vb file I have the following code:

Sub FormView1_ItemInserted(ByVal senderAs Object,ByVal eAs FormViewInsertedEventArgs)If e.ExceptionIs Nothing Then If e.AffectedRows = 1Then MessageLabel.Text ="Record Inserted Successfully"Else MessageLabel.Text ="An error occured during the insert operation"End If Else MessageLabel.Text = e.Exception.Message e.ExceptionHandled =True e.KeepInInsertMode =True End If End Sub

I think this is what I need, but in my editor the MessageLabel.Text part is underlined and shows Name "MessageLabel" is not declared.

Can I get a little more help on this? Does it look like this is what I need to do?


Okay, I had to move the MessageLabel outside of my formview to get this working. I also had to add the following line to my FormView Tag:

OnItemInserted="FormView1_ItemInserted"

Hey,

Yes, but if MessageLabel isn't declared, what it's saying is there isn't a:

<asp:Label id="MessageLabel" runat="server"></asp:Label>

On the form. Is that incorrect?


Well, I had the MessageLabel in my insert view on the Form. When I had it like that, it showed that it was undeclared. Then after I moved it up above the formview then I wasn't getting that anymore.

Is there a way to make the whole page refresh though once it is inserted? I have a gridview at the top that shows the request and I don't want them to refresh the page to see the results because it might double post the insert form at the bottom of the page.


Hey,

MessageLabel in the formview is part of the formview; it's not its own control. Yes, that is why you got that. They shouldn't have to refresh. On FormView_ItemInserted, the gridview should be rebound by calling DataBind(). See my blog (http://dotnetslackers.com/community/blogs/bmains/default.aspx) as I wrote about that recently.

0 comments:

Post a Comment