ScriptManager, Alert, Alertify, and Confirmation in ASP.NET + JavaScript

Leave a Comment

When you’re building ASP.NET WebForms applications, showing feedback messages to users is crucial. You may use:

  • JavaScript’s built-in dialogs (alert, confirm)

  • Third-party libraries (like alertify.js)

  • ASP.NET’s ScriptManager.RegisterStartupScript to trigger these scripts after server-side operations.

Let’s break this down step by step.

1. ScriptManager.RegisterStartupScript – The Bridge

if (SqlHelper.ExecuteNonQuery(sqlcon, CommandType.Text, sqlBidD, param3) > 0)
{
    ScriptManager.RegisterStartupScript(this, this.GetType(), "success", "order();", true);
}
else
{

    string script = "alertify.alert('" + password Expired + "');";
    ScriptManager.RegisterStartupScript(this, this.GetType(), "alertifyScript", script, true);
}

Explanation

  • SqlHelper.ExecuteNonQuery(...) → Executes a SQL query (insert/update/delete).

  • If success (> 0 rows affected), we call a JavaScript function order();.

  • If failure, we show an alertify alert with error message.

  • ScriptManager.RegisterStartupScript(...) ensures that JavaScript executes after page refresh/postback.

Without ScriptManagerYour JavaScript may not run after a postback in an UpdatePanel.

2. order() function with Alertify

function order() {
    alertify.alert(
        "Application placed successfully.  Please approve it to confirm your application.",
        function (e) {
            if (e) {
                window.history.pushState(null, "", window.location.href);
                window.location = '/orders.aspx';
            } else {
                window.location = '/ClientPages.aspx';
            }
        }
    );
}

Key points

  • alertify.alert(message, callback) → shows a custom styled alert box.

  • The callback function checks e:

    • If user clicks OK → Redirect to /orders.aspx.

    • Else → Redirect to /ClientPages.aspx.

alertify looks modern and customizable compared to plain alert.

3. Example. Normal JavaScript Alert via ScriptManager

if (SqlHelper.ExecuteNonQuery(SqlCon, CommandType.Text, sqlins) > 0)
{
    ScriptManager.RegisterStartupScript(this, this.GetType(), "success", "alert('Client details added in Master.');", true);
}
  • Shows a simple alert("Clientdetails added in Master.").

  • Good for basic messages, but blocking & ugly UI.

4. Redirect with Alert

ScriptManager.RegisterStartupScript(
    this.Page,
    this.Page.GetType(),
    "Msge",
    "alert('Client Details updated successfully');window.location='/Admin/ClientPages.aspx';",
    true
);

Here

  • First shows alert("Client Details updated successfully").

  • Then redirects to /Admin/ClientPages.aspx.

5. Error Handling with Alert

ScriptManager.RegisterStartupScript(
    this.Page,
    typeof(string),
    "alert",
    "alert('" + ex.Message.ToString() + "');",
    true
);
  • Displays server-side error message inside a JavaScript alert.

6. Custom JS Function via ScriptManager

function HideTable() {
    if (detectmob()) {
        document.getElementById("ipotable").style.display="none";
        document.getElementById("tblNodata").style.display="none";
    }
}
ScriptManager.RegisterStartupScript(this.Page, typeof(string), "hide", "HideTable();", true);
  • Calls custom JS function after server-side event.

  • Example: Hide tables if user is on mobile.

7. Thank You Message + Redirect

ScriptManager.RegisterStartupScript(
    this,
    typeof(string),
    "Message",
    "alert('Thank you for providing details. Our Representative will contact you soon.');window.location='/'",
    true
);

Shows message and redirects to homepage /.

8. Plain JavaScript Alert (Client-side)

function Validatecatelog() {
    if ($(".totalclass").text() == "0") {
        alert("Please select API");
        return false;
    }
}
<asp:Button ID="btn_showprice" runat="server" Text="Next" CssClass="price_btn" OnClientClick="return Validatecatelog();"  />
if (dstotal.Tables[0].Rows.Count > 0)
            {
                totaldata = "<span class='totalclass'>" + dstotal.Tables[0].Rows[0]["total"].ToString() + "</span>";
            }
  • Uses alert() directly in JS validation.

  • Blocks execution until user clicks OK.

9. Confirmation Box

let text = "It seems your account is not associated with the provided UPI ID.\n Are you sure you want to proceed?";
if (confirm(text)) {
    document.getElementById("clupiname").innerText = cname1;
    offline_2lcondition();
    return true;
} else {
    return false;
}
  • confirm(message) → Yes/No dialog.

  • Returns true if OK clicked, false if Cancel clicked.

  • Useful for critical actions (delete, update, proceed checks).

Difference: Alert vs Alertify vs Confirm

Featurealert()alertify.alert()confirm()
TypeBuilt-in JSExternal JS LibraryBuilt-in JS
UIOld, blockingModern, customizableOld, blocking
CustomizationNoYes (themes, buttons)No
Callback NoYes (with function)Returns boolean
Redirect SupportOnly with extra JSEasy (inside callback)Easy (via true/false)
Use CaseQuick infoUser-friendly notificationsUser decisions (Yes/No)

Conclusion

  • Use alert() for basic notifications (fast but outdated UI).

  • Use alertify.alert() for modern, user-friendly alerts with callbacks.

  • Use confirm() when you need a Yes/No choice.

  • Always wrap your script inside ScriptManager.RegisterStartupScript in ASP.NET to make sure it works after postbacks.

Previous PostOlder Post Home

0 comments:

Post a Comment