If you use the above methods to invoke functions already defined in the aspx page, then you need to be aware of where those functions are defined, because if you try to invoke the functions and they have not yet been created, then you will get a null object message.
Even if you use the Client.registerstartupscript which places your script just above the closing form tag, if the functions you are invoking are also defined in this block of code, your code will produce an error.
So make sure your functions are already defined relative to where your server side script code is placed within the web form.
The safest place to put these functions is in the header block.
I just ran across a situation where all the javascript functions were placed below the closing form tag, thus no matter which method I used to invoke the function, it had not been defined before it was invoked, causing a null object reference. I had to move the closing form tag after the script and then it all worked as advertised
ReplyDelete
ReplyDeleteDim CSTag As String
Dim CSScript As String
Dim CSType As Type
Dim CSM As ClientScriptManager
Private Sub ShowAlert(ByVal vMessage As String)
Dim sMessage As String
CSTag = "ShowAlert"
CSType = Me.GetType()
If CSM Is Nothing Then
CSM = Page.ClientScript
End If
sMessage = vMessage.Replace(vbCrLf, "")
CSScript = String.Format("ShowAlert('{0}');", sMessage)
If Not CSM.IsStartupScriptRegistered(CSType, CSTag) Then
ClientScript.RegisterStartupScript(CSType, CSTag, CSScript, True)
End If
End Sub