thusharaK thusharaK - 6 months ago 14
jQuery Question

Dynamic alert box in ASP.net C#

How to create dynamic alert box in ASP.net C# using a database listener which can be triggered without using a button click?

I have a ASP.NET web site and I need to create a custom alert box which can be triggered without using a button click but using a database listener.(I'm using PostgreSQL database.) since I'm new to ASP.NET C# i have no idea how to create it. this alert box should pop up while we are browsing through the website and whenever database updated by a particular value which is updated in the database by a external process.

What I want to know is how to create a database listener in ASP.NET, if you have example codes, please be kind enough to share that.

Answer

In your code behind file you can add the below function

private void ShowMessage(string msg){
string myScript = "\n<script type=\"text/javascript\">\n";
myScript += "alert(" + msg + ");";
myScript += "\n\n </script>";
Page.ClientScript.RegisterStartupScript(this.GetType(), "myKey", myScript, true);
}

call the above function where ever you want to show alert with your custom message.

Add the below code at client side, means in your designer page under tag

$(document).ready(function() {

Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);

});

function EndRequestHandler(sender, args) {

}