[c#] How to display an alert box from C# in ASP.NET?

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?

This question is related to c# asp.net alert

The answer is


After insertion code,

ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Record Inserted Successfully')", true);

Write this line after your insert code

 ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Insert is successfull')", true);

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

MessageBox.Show("Data inserted successfully");


Response.Write("<script>alert('Data inserted successfully')</script>");

Hey Try This Code.

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

Cheers


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.


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 !");
}

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.


Examples related to c#

How can I convert this one line of ActionScript to C#? Microsoft Advertising SDK doesn't deliverer ads How to use a global array in C#? How to correctly write async method? C# - insert values from file into two arrays Uploading into folder in FTP? Are these methods thread safe? dotnet ef not found in .NET Core 3 HTTP Error 500.30 - ANCM In-Process Start Failure Best way to "push" into C# array

Examples related to asp.net

RegisterStartupScript from code behind not working when Update Panel is used You must add a reference to assembly 'netstandard, Version=2.0.0.0 No authenticationScheme was specified, and there was no DefaultChallengeScheme found with default authentification and custom authorization How to use log4net in Asp.net core 2.0 Visual Studio 2017 error: Unable to start program, An operation is not legal in the current state How to create roles in ASP.NET Core and assign them to users? How to handle Uncaught (in promise) DOMException: The play() request was interrupted by a call to pause() ASP.NET Core Web API Authentication Could not load file or assembly 'CrystalDecisions.ReportAppServer.CommLayer, Version=13.0.2000.0 WebForms UnobtrusiveValidationMode requires a ScriptResourceMapping for jquery

Examples related to alert

Swift alert view with OK and Cancel: which button tapped? Bootstrap Alert Auto Close How to reload a page after the OK click on the Alert Page How to display an alert box from C# in ASP.NET? HTML - Alert Box when loading page How to show an alert box in PHP? Twitter Bootstrap alert message close and open again How to handle login pop up window using Selenium WebDriver? How to check if an alert exists using WebDriver? chrome undo the action of "prevent this page from creating additional dialogs"