I've got old code that I am working on, and I'd like to replace some depreciated methods like this one:
Public Shared Sub cmnCloseForm(ByRef pgTemp As System.Web.UI.Page, Optional ByVal sMsg$ = "", Optional ByVal sExtraJava$ = "")
' Variables
Dim sJava$
' New Variable
Dim sBld As StringBuilder = New StringBuilder()
' Create Java String
sJava = IIf(sMsg <> "", "alert('" + sMsg + "'); ", "") + _
IIf(sExtraJava <> "", sExtraJava, "") + _
"self.close();"
' Append Strings
sBld.Append("<script language='javascript'>")
sBld.Append(sJava)
sBld.Append("</script>")
' Register String
pgTemp.RegisterStartupScript("closing", sBld.ToString)
End Sub
That creates a dialog box on the web page that looks like this:
I tried replacing it with ScriptManager.RegisterStartupScript:
Public Shared Sub cmnCloseForm(ByRef pgTemp As System.Web.UI.Page, Optional ByVal sMsg$ = "", Optional ByVal sExtraJava$ = "")
' Variables
Dim sJava$
' New Variable
Dim sBld As StringBuilder = New StringBuilder()
' Create Java String
sJava = IIf(sMsg <> "", "alert('" + sMsg + "'); ", "") + _
IIf(sExtraJava <> "", sExtraJava, "") + _
"self.close();"
' Append Strings
sBld.Append("<script language='javascript'>")
sBld.Append(sJava)
sBld.Append("</script>")
' Register String
Dim page As Page = CType(HttpContext.Current.CurrentHandler, Page)
ScriptManager.RegisterStartupScript(page, page.GetType(), "closing", sBld.ToString(), True)
End Sub
The new code above does this (notice the text added to the dialog box):
How do I correctly replace this depreciated code?
I simplified the code and used ClientScriptManager.RegisterStartupScript, which seems to be made to replace the deprecated method.
Here is my resulting code:
Public Shared Sub cmnCloseForm(ByRef pgTemp As System.Web.UI.Page, Optional ByVal sMsg$ = "", Optional ByVal sExtraJava$ = "")
' Variables
Dim sJava As String
If String.IsNullOrEmpty(sMsg) Then
sJava = String.Format("{0} self.close();", sExtraJava)
Else
sJava = String.Format("alert('{0}'); {1} self.close();", sMsg, sExtraJava)
End If
' Register String
pgTemp.ClientScript.RegisterStartupScript(pgTemp.GetType(), "closing", String.Format("<script language='javascript'>{0}</script>", sJava))
End Sub