How to display an alert box from C# in ASP.NET?
Asked Answered
S

9

31

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?

Saintly answered 4/5, 2013 at 4:3 Comment(2)
Look for Client.RegisterScriptBlock(.....)Dripstone
Does this answer your question? Calling JavaScript Function From CodeBehindBollix
E
85

After insertion code,

ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Record Inserted Successfully')", true);
Eda answered 4/5, 2013 at 4:8 Comment(1)
this worked like a charm. thanks everyone for the quick help.Saintly
C
28
Response.Write("<script>alert('Data inserted successfully')</script>");
Charlenacharlene answered 17/9, 2015 at 8:46 Comment(1)
nice because I can check if there's a value in a certain variable for checkingKookaburra
K
9

Write this line after your insert code

 ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Insert is successfull')", true);
Krefetz answered 4/5, 2013 at 4:7 Comment(0)
T
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 !");
}
Tegument answered 22/7, 2018 at 7:9 Comment(0)
O
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.

Orphaorphan answered 17/8, 2018 at 2:20 Comment(0)
S
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.

Samaveda answered 2/5, 2014 at 11:38 Comment(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 dataCommines
D
0

Hey Try This Code.

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

Cheers

Dawndawna answered 4/5, 2013 at 4:54 Comment(0)
B
0

You can try the following one with customized way, in asp.net C#,

<style>
    .heading2 {
        font-weight: bold;
        font-size: medium;
        font-style: italic;
        color: midnightblue;
    }

    #customAlert {
        display: none;
        position: fixed;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
        padding: 20px;
        background-color: #fff;
        border: 1px medium #ccc;
        box-shadow: 0 0 10px rgba(0, 0, 0, 0.5);
        z-index: 1000;
        text-align: center;
    }

    #overlay {
        display: none;
        position: fixed;
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;
        background-color: rgba(0, 0, 0, 0.5);
        z-index: 999;
    }

    #alertSymbol {
        font-size: 36px;
        color: #FF6347; /* Coral color, you can change this */
    }

    #alertMessage {
        margin-top: 10px;
        font-size: 18px;
        color: #333;
    }

    #closeButton {
        margin-top: 20px;
        padding: 10px;
        background-color: #007BFF; 
        color: #fff;
        border: none;
        cursor: pointer;
    }
</style>

Script method:-

<script type="text/javascript">
    function showCustomAlert() {
        var customAlert = document.getElementById('customAlert');
        var overlay = document.getElementById('overlay');

        customAlert.style.display = 'block';
        overlay.style.display = 'block';
    }

    function hideCustomAlert() {
        var customAlert = document.getElementById('customAlert');
        var overlay = document.getElementById('overlay');

        customAlert.style.display = 'none';
        overlay.style.display = 'none';
    }
</script> 

Action PopupMessge

 <div class="pos-rel">
     <div id="customAlert">
         <div id="alertSymbol">&#9888;</div>
         <!-- Unicode character for warning symbol -->
         <div id="alertMessage">
             <asp:Label ID="alertMessageM" runat="server" Text="Message" CssClass="heading2"></asp:Label>
         </div>
         <button id="closeButton" onclick="hideCustomAlert()">Close</button>
     </div>

     <div id="overlay"></div>
 </div>
Basilbasilar answered 8/12, 2023 at 12:4 Comment(0)
B
-2

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

MessageBox.Show("Data inserted successfully");

Bidding answered 22/9, 2015 at 19:2 Comment(3)
Message.Show is not for asp.netPamphlet
this is not an answer to the questionIdomeneus
It is not related to the question, but answered my question. Thanks!Lehman

© 2022 - 2025 — McMap. All rights reserved.