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.RegisterStartupScriptto 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 (
> 0rows affected), we call a JavaScript functionorder();.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 Detailsupdated 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.

Join the conversation! Your thoughts help the community grow.