How to display save confirmation message on my Web form when user saves new form by clicking Save button

Go To


I am very new to ASP.NET. I am working on some other developer's Web form. I have been asked to fix a bug which says:

No success message is displayed when user clicks Save button after adding information to the form (first time or while updating)

There is a Label at the top of the page with ID lblMsgs

<asp:Label ID="lblMsgs" runat="server">

I have added this to the Code behind in the appropriate place:

lblMsgs.Text = "Message Text";

Now, my question is how do I modify the HTML or code behind to make sure that when Save button is clicked, the code checks that the data is saved to the database and displays save successful message on the Web form.

Can you please help by giving example of the code using my label ID above?

Thank you in advance.

2012-04-03 23:22
by user1311563


That's a pretty vague question, but if your looking for data validation you should look at the built in ASP.NET Validators. This can do client/server side data validation. As for returning a message to the user based on a successful update to the DB, that is going to be solution specific depending on what type of data layer your using.

2012-04-03 23:27
by Zachary
Your right. My question was not clear. I have modified it. I am not really validating data but only making sure it is saved to DB. The additional information helps. Thank you - user1311563 2012-04-04 02:53
Normally your code that writes to the DB (e.g. ADO.NET, EF, Linq2Sql, etc...) will throw an error if it's unable to write to the DB. You can wrap it in a TryCatch, and if it throws an error and fails to write to the DB you can update the lblMsg. You can also query the DB after the create/update, if you want to verify your change has been written to the DB - Zachary 2012-04-04 05:17


On your Save button handler, you can add validation logic if whether the data is valid or not.

protected void ValidateSave(object o, EventArgs e){
    if (Page.IsValid) {
         // Save to DB and notify of update status
         var success = SomeDBMethod.Update...
         if (success){
             DoAlert("Saved Successfully!");
    else { lblMsgs.Text = "Failed"; }

// Call client-side alert
private void DoAlert(string message) {
    ClientScriptManager cs = Page.ClientScript;

    var x = "alertScript";
    if (!cs.IsStartupScriptRegistered(cstype, x))
      String script = string.Format"alert('{0}', message);";
      cs.RegisterStartupScript(cstype, x, script, true);
2012-04-03 23:31
by Dennis Rongo
Thank you so much for your help with that. I think that's the answer to the part I forgot to ask. I might have phrased my question wrong. But it does help. Next I want to display a message like 'Save Successful' if the data is saved to DB. Can you please tell what else I should add to the code - user1311563 2012-04-04 02:34
I updated my response above to include an alert - Dennis Rongo 2012-04-04 05:40