Saturday, March 24, 2012

Me is not valid in a module - error message

Dear All,

I have this function in a class and I'd like to share it and so place it in a module.

' javaScript to refresh parent window and close the popped up window
Public Sub refreshParent(ByVal sender As System.Object, ByVal e As System.EventArgs)

Dim myJs1 As String = "<scriptlanguage=""JavaScript"">window.opener.Form1.submit();window.close();</script>"
If (Not Me.IsStartupScriptRegistered("Startup1")) Then
Me.RegisterStartupScript("Startup1", myJs1)
End If

End Sub

However when compiling my project I get an error message:

'Me' is not valid in a module.

Can someone let me know why this is and how I could solve this? Thanks!Me refers to the class the code is executing in. Code in a module is not inside an instantiated class so can't support Me.

Could do it like so:

Public Sub refreshParent(ByVal ThisPage As System.Web.Page)
Dim myJs1 As String = "<script language=""JavaScript"">window.opener.Form1.submit(); window.close();</script>"
If (Not ThisPage.IsStartupScriptRegistered("Startup1")) Then
ThisPage.RegisterStartupScript("Startup1", myJs1)
End If
End Sub

Then from your aspx page: refreshParent(Me)

Though i'd suggest putting the Javascript in an external .js file and using RegisterClientScriptInclude to pull it in... this way if you want to tweak the javascript, you won't have to recompile the application


gorgenyi wrote:


However when compiling my project I get an error message:

'Me' is not valid in a module.

Can someone let me know why this is and how I could solve this? Thanks!

Just to make things clear 'Me' in VB.NET and 'this' in C# refers to the current object in memory that was created from a class definition. In VB modules are global to an application and all methods and functions are Shared or static. Because of this, .NET doesn't create a object in memory everytime you call a method or function on your Module. So, you can't say 'Me' since there is no instance or object to reference.


Thanks for your suggestions, could you explain how to pull the code in from a seperate .js file?

As I can't seem to reference system.web.page in my module file so I get an error message that ThisPage is not declared.

Basically I have a number of subs containingJavaScript and I'd like to use them on many different pages so I'd like toshare them in my application.


What's the best way of doing this?

These are my js subs:


'javaScript to refresh parent window and close the popped up window

Public Sub refreshParent(ByVal sender As System.Object, ByVal e AsSystem.EventArgs)

Dim myJs1 As String = "<script language=""JavaScript"">window.opener.Form1.submit();window.close();</script>"

If (Not Me.IsStartupScriptRegistered("Startup1")) Then

Me.RegisterStartupScript("Startup1", myJs1)

End If

End Sub

'javaScript to pop up page from <a href></a> links

Public Sub popPageLink()

Dim jsPopPageLink As String = "<scriptlanguage=""JavaScript"">var newWin; functionpopPagejsf(strPageUrl,strPageName) { "

jsPopPageLink += "newWin =window.open(strPageUrl,'pageRef',""width=620,height=400,left=380,top=290"");"

jsPopPageLink += "newWin.focus(); return false;}</script>"

If (Not Me.IsStartupScriptRegistered("regJsPopPageLink")) Then

Me.RegisterStartupScript("regJsPopPageLink", jsPopPageLink)

End If

EndSub

'javeScript to pop up page by clicking on a button

Public Function popPageButton(ByVal myUrl As String) As String

Dim jsPopPageButton As String

jsPopPageButton = "newWin = window.open('" & myUrl &"','pageRef','width=620,height=400,left=380,top=290');"

jsPopPageButton += "newWin.focus();"

jsPopPageButton += "return false;"

Return jsPopPageButton

End Function


0 comments:

Post a Comment