Using javascript command

D

Dave

I have an app with several web forms. Code for each of the forms contains a
subroutine I wrote that performs the Javascript function "alert". Thus:

Private Sub DisplayAlert(ByVal msg As String)
Dim sKey As String = "s"
Dim alertScript As String = "<script language=JavaScript runat=server> "
alertScript &= "alert('" & msg & "');"
alertScript &= "</script>"
RegisterClientScriptBlock(sKey, alertScript)
End Sub

The subroutine works fine as long as I have a version in each of the web
form modules, but I would like to put a single instance of this subroutine
into a module I have for public routines. When I try that, the compiler
objects to the "RegisterClientScriptBlock" statement. ("Name
'RegisterClientScriptBlock' is not declared.")

Would someone please help me with this?

Thanks in advance,

Dave
 
C

Cor Ligthert

Dave,

I never found a real good reason to use a shared class in a webpage, I think
that this is one.

I made this sample/try,

Have a look if it works for you.

Cor

\\\the begin of a page
Sub Page_Load(ByVal sender As Object, _
ByVal e As EventArgs) Handles MyBase.Load
javascript.alert(Me)
End Sub
End Class
Public Class javascript
Public Shared Sub alert(ByVal this As Page)
Dim str As String
str = "<script language=javascript>" & _
"alert('I hope this helps?'); </script>"
this.RegisterStartupScript("Startup", str)
End Sub
End Class
///

"Dave"
 
G

Greg Burns

Cor,

That is weird. I thought "this" was a C# thing.

I do know that RegisterStartupScript is one of those hidden methods of the
Page class (you need to uncheck "Hide hidden members" in Options->Text
Editor->All Languages->General)

But obviously you cannot use Page in a shared class. How did you know do you
"this" in a shared class?

I would have guessed it would have been something like
HttpContext.Current... or something.

Greg
 
C

Cor Ligthert

Greg,

I could have used any word, it is just the page that is distributed to this
routine using "This", I thought let me make it nice javascript looking, it
has nothing to do with the C# word, I am sure you see it when you look to it
again, and message back something as LOL..

:)

Cor
 
D

Dave

Cor,

That did the trick! Thank you very much.

Dave




Dave,

I never found a real good reason to use a shared class in a webpage, I think
that this is one.

I made this sample/try,

Have a look if it works for you.

Cor

\\\the begin of a page
Sub Page_Load(ByVal sender As Object, _
ByVal e As EventArgs) Handles MyBase.Load
javascript.alert(Me)

[snip]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top