Thursday, 19 April 2012

show alert message from server side using javascript


VB

Page.ClientScript.RegisterStartupScript(Me.GetType(), "showalert", alert('Only alert Message');__doPostBack('__Page', '');", True)


C#
this.Page.ClientScript.
RegisterStartupScript(this.GetType(), Guid.NewGuid().ToString() , "alert('" + ex.Message + "');__doPostBack('__Page', 'EmptyPostback');", true);

__doPostBack('__Page', 'EmptyPostback'); is used to avoid alert message while click on browser back button.

When we register the client script with the alert, immediately follow the alert statement with one which will refresh the page. This ensures that the client script is removed from the page as soon as the OK is clicked, and if the user returns to the page the alert pop up does not reappear.


Register Javascript when using Update Panel

VB
ScriptManager.RegisterStartupScript(Me, [GetType](), "showalert", "alert('Only alert Message');__doPostBack('__Page', '');", True)
C#

ScriptManager.RegisterStartupScript(Page, Page.GetType(), Guid.NewGuid().ToString(),"alert('I am from the Server');__doPostBack('__Page', '');", true);

No comments:

Post a Comment