30

I am using a detail-view and would like to display an alert-box at the end of my code block that says:

Thank you! Your data has been inserted successfully.

Is there a simple way to do this from the C# code behind of my ASP.NET web pages?

Hazel へいぜる
  • 2,751
  • 1
  • 12
  • 44
moe
  • 5,149
  • 38
  • 130
  • 197

8 Answers8

83

After insertion code,

ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Record Inserted Successfully')", true);
Mudassir Hasan
  • 28,083
  • 20
  • 99
  • 133
25
Response.Write("<script>alert('Data inserted successfully')</script>");
9

Write this line after your insert code

 ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Insert is successfull')", true);
Amit Singh
  • 8,039
  • 20
  • 29
6

You can create a global method to show message(alert) in your web form application.

public static class PageUtility
{
    public static void MessageBox(System.Web.UI.Page page,string strMsg)
    {
        //+ character added after strMsg "')"
        ScriptManager.RegisterClientScriptBlock(page, page.GetType(), "alertMessage", "alert('" + strMsg + "')", true);

    }
}

webform.aspx

protected void btnSave_Click(object sender, EventArgs e)
{
    PageUtility.MessageBox(this, "Success !");
}
antonio
  • 548
  • 8
  • 16
sebu
  • 2,824
  • 1
  • 30
  • 45
3

If you don't have a Page.Redirect(), use this

Response.Write("<script>alert('Inserted successfully!')</script>"); //works great

But if you do have Page.Redirect(), use this

Response.Write("<script>alert('Inserted..');window.location = 'newpage.aspx';</script>"); //works great

works for me.

Hope this helps.

Ahmad Tijani
  • 392
  • 3
  • 10
1
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Record Inserted Successfully')", true); 

You can use this way, but be sure that there is no Page.Redirect() is used. If you want to redirect to another page then you can try this:

page.aspx:

<asp:Button AccessKey="S" ID="submitBtn" runat="server" OnClick="Submit" Text="Submit"
                                        Width="90px" ValidationGroup="vg" CausesValidation="true" OnClientClick = "Confirm()" />

JavaScript code:

function Confirm()
{
   if (Page_ClientValidate())
   {
      var confirm_value = document.createElement("INPUT");
      confirm_value.type = "hidden";
      confirm_value.name = "confirm_value";
      if (confirm("Data has been Added. Do you wish to Continue ?"))
      {
         confirm_value.value = "Yes";
      }
      else
      {
         confirm_value.value = "No";
      }
      document.forms[0].appendChild(confirm_value);
   }
}

and this is your code behind snippet :

protected void Submit(object sender, EventArgs e)
{
   string confirmValue = Request.Form["confirm_value"];
   if (confirmValue == "Yes")
   {
      Response.Redirect("~/AddData.aspx");
   }
   else
   {
      Response.Redirect("~/ViewData.aspx");
   }
}

This will sure work.

Ahmed Salman Tahir
  • 1,783
  • 1
  • 17
  • 26
Mayank
  • 141
  • 1
  • 1
  • 12
  • 1
    I don't think this answers the question because the requirement is to have a message after the data is saved successfully, whereas your approach asks for confirmation before saving data – Ahmed Salman Tahir Apr 19 '15 at 06:53
0

Hey Try This Code.

ScriptManager.RegisterStartupScript(Page, Page.GetType(), "Alert", "Data has been saved", true);

Cheers

Neeraj Dubey
  • 4,401
  • 8
  • 30
  • 49
-2

You can use Message box to show success message. This works great for me.

MessageBox.Show("Data inserted successfully");