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 ScriptManager
Your 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
:
âś… 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
5. Error Handling with Alert
ScriptManager.RegisterStartupScript(
this.Page,
typeof(string),
"alert",
"alert('" + ex.Message.ToString() + "');",
true
);
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);
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>";
}
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
Feature | alert() | alertify.alert() | confirm() |
---|
Type | Built-in JS | External JS Library | Built-in JS |
UI | Old, blocking | Modern, customizable | Old, blocking |
Customization | ❌ No | ✅ Yes (themes, buttons) | ❌ No |
Callback | ❌ No | ✅ Yes (with function) | ✅ Returns boolean |
Redirect Support | Only with extra JS | Easy (inside callback) | Easy (via true/false) |
Use Case | Quick info | User-friendly notifications | User 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.