Use ScriptManager.RegisterStartupScript rather than Page.ClientScript.RegisterStartupScript with an ajax aspx page to add javascript

Category Javascript Sub Category General
Bookmark and Share
When using ajax aspx pages use RegisterStartupScript. Using techniques such as response.write to add scripts on an event postback will not render the page correctly.
Example of showing an alert box. This could be called inside for example a button click event.
Copy Code
Dim AlertMessage As String AlertMessage = String.Concat("alert('Fields empty.\n Total cannot exceed £", MaxAmount, "');") 'NOTE Response.Write("<script>" & AlertMessage & "</script>") will not show correctly 'Use example below ScriptManager.RegisterStartupScript(Page, Page.GetType, "AlertMessage", AlertMessage, True)


Note the method to register javascript has been deprecated to the last version below.
Otherwise when working with ajax pages it will not fire.
Copy Code
Private Sub AlertWindow(ByVal Message As String) Message = Message.Replace("'", "\'") Message = Message.Replace(Convert.ToChar(10), "\n") Message = Message.Replace(Convert.ToChar(13), "") Message = String.Format("window.onload = alert('{0}');", Message) 'Page.ClientScript.RegisterStartupScript(Me.GetType, "PopUpAlert", Message, True) 'Page.ClientScript.RegisterClientScriptBlock(Me.GetType, "PopUpAlert", Message, True) ScriptManager.RegisterStartupScript(Me, Me.GetType, "PopUpAlert", Message, True) End Sub
Share the love
Bookmark and Share