In this post I will show you how to generate javascript alert from code behind
using System.Web; using System.Text; using System.Web.UI; /// /// A JavaScript alert /// public static class Alert { public static void Show(string message) { // Cleans the message to allow single quotation marks string cleanMessage = message.Replace("'", "\\'"); string script = "<script type=\"text/javascript\">alert('" + message + "');</script>"; // Gets the executing web page Page page = HttpContext.Current.CurrentHandler as Page; // Checks if the handler is a Page and that the script isn't allready on the Page if (page != null && !page.ClientScript.IsClientScriptBlockRegistered("alert")) { page.ClientScript.RegisterClientScriptBlock(typeof(Alert), "alert", script); } } }
How to use
protected void btnTest_Click(object sender, EventArgs e) { try { Alert.Show(" saved"); } catch (Exception ex) { Alert.Show(ex.Message); } }


2 comments:
This code not working.
@apps:Thanks for finding the issue in the code.I have updated the code.
Post a Comment