please help.

I'm trying to run a script from a inside shared function:

Dim strScript As String
            strScript = "<script language=JavaScript>"
            strScript += "alert(""" & string1 & """);"
            strScript += "</script>"
            HttpContext.Current.Response.Write("alert(""" & string1 & """)")

            If (Not Page.ClientScript.IsStartupScriptRegistered("clientScript")) Then
                Page.ClientScript.RegisterClientScriptBlock(MyPage.GetType(), "clientScript", strScript)
            End If

I'm getting an error in this line: Page.ClientScript. The message is: "Reference to a non-shared member requires an object reference"

Here is my Java Script:

function HoverSub(myString)
     {
     
      PageMethods.GetContactName(myString);
    
     }

And here is my Webmethod which is in the codebehind of my Content Page.

<WebMethod()> _
    Public Shared Function GetContactName(ByVal companyID As String) As String
        If companyID Is Nothing OrElse companyID.Length = 0 Then
            Return String.Empty
        End If
        Dim conn As SqlConnection = Nothing
        Try
            Dim string1 As String = ""
            Dim connection As String = ConfigurationManager.ConnectionStrings("conn1").ConnectionString
            Dim myReader As SqlDataReader
            conn = New SqlConnection(connection)
            Dim cmd As New SqlCommand
            cmd.Connection = conn
            cmd.CommandType = Data.CommandType.StoredProcedure
            cmd.CommandText = "GetRatingsCount"
            cmd.Parameters.AddWithValue("@Company_ID", companyID)
            conn.Open()
            myReader = cmd.ExecuteReader
            Do While myReader.Read
                If string1 = "" Then
                    string1 = myReader("myCount") & " people rated " & myReader("MyRating")
                Else
                    string1 = string1 & ", " & myReader("myCount") & " people rated " & myReader("MyRating")
                End If
            Loop

            Dim strScript As String
            strScript = "<script language=JavaScript>"
            strScript += "alert(""" & string1 & """);"
            strScript += "</script>"
           

            If (Not Page.ClientScript.IsStartupScriptRegistered("clientScript")) Then
                Page.ClientScript.RegisterClientScriptBlock(MyPage.GetType(), "clientScript", strScript)
            End If
            Return string1
        Catch ex As SqlException
            Return "error"
        Finally
            conn.Close()
        End Try
    End Function

http://msdn.microsoft.com/en-us/library/zwwhc0d0(VS.80).aspx

check this links

i have tried the suggestion in the link that you gave. I've added this:

Public Shared MyPage As Page

and on Page_load event i added this:

MyPage = Me.Page

I have changed the line where it was giving the error to this:

If (Not MyPage.ClientScript.IsStartupScriptRegistered("clientScript")) Then
                MyPage.ClientScript.RegisterClientScriptBlock(MyPage.GetType(), "clientScript", strScript)
            End If

now the error is gone but the script is not firing. I don't get any error messages but the alert window is not popping up.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.