How to display warning window from C # in ASP.NET?

I am using detail-view and would like a warning window to appear at the end of my code block:

Thanks! Your details have been successfully added.

Is there an easy way to do this from C# code behind my ASP.NET web pages?

+18
source share
8 answers

After entering the code

 ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Record Inserted Successfully')", true); 
+65
source
 Response.Write("<script>alert('Data inserted successfully')</script>"); 
+11
source

Write this line after the insert code

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

You can create a global method for displaying a message (alert) in a 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 !"); } 
+4
source
 ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Record Inserted Successfully')", true); 

You can use this method, but make sure that Page.Redirect() not used. If you want to redirect to another page, you can try the following:

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 the 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 work.

0
source

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

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

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

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

works for me.

Hope this helps.

0
source

Hey, try this code.

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

Greetings

-1
source

You can use the Message field to display a success message. This works great for me.

MessageBox.Show("Data inserted successfully");

-3
source

All Articles