Here is a simpler version. Using the
overload method[
^](parameter
addScriptTags
), you can reduce the code.
private void ShowMessage(string Message)
{
if (!ClientScript.IsClientScriptBlockRegistered("MyMessage"))
{
ClientScript.RegisterClientScriptBlock(this.GetType(), "MyMessage", "alert('" + Message + "');", true);
}
}
public void btnTest_Click(Object sender, EventArgs e)
{
ShowMessage("Hello World");
}
For AJAX -
Updatepanel
, use the below one:
ScriptManager.RegisterClientScriptBlock(this.Page, typeof(UpdatePanel), "MyMessage", Message, true);
EDIT
The
alert
keyword moved into general function
ShowMessage
.
Further Reading