GeoffWilson
GeoffWilson

Reputation: 433

Passing a variable into a URL

I have a function that contains a variable that I have accessed from a database. I am trying to concatenate that variable with a URL in the way stated below.

PopupWindow=window.open('Http://' + svrname +'/Quoteman/DatePicker.aspx?Ctl=' + ctl,'DatePicker',settings);

I am receiving an error when I try and compile my code.
Here is the function:

     Public Function getserverName() As String
    Dim connection As SqlConnection
    Dim command As SqlCommand
    Dim readData As SqlDataReader
    Dim path As String
    path = ""

    connection = New SqlConnection(System.Configuration.ConfigurationManager.AppSettings("getServer"))
    connection.Open()
    command = New SqlCommand("select [Email_Notification_Date] from GlobalDB where [Email_Notification_Date]='Batman'", connection)
    readData = command.ExecuteReader
    While readData.Read()
        path = readData.Item("Email_Notification_Date")
    End While
    connection.Close()
    Return path
End Function

And here is where I am trying to call the function:

    function PopupPicker(ctl,w,h)
{
var PopupWindow = null;
var serverName = new getServername;
svrname = serverName.getServername;
    settings='width='+ w + ',height='+ h + ',location=no,directories=no, menubar=no,toolbar=no,status=no,scrollbars=no,resizable=no,dependent=no';
    PopupWindow=window.open('Http://' + svrname +'/Quoteman/DatePicker.aspx?Ctl=' + ctl,'DatePicker',settings);
    PopupWindow.focus();
}

P.S. the function does return a value.

EDIT: sorry, forgot to say I am trying to call a VB function from javascript This is the window I get from the error.

Unhandled exception at line 200, column 5 in http://localhost:50209/Admin/EmployeeAssets.aspx || 0x800a1391 - JavaScript runtime error: 'getServername' is undefined

EDIT: I added an argument to the function and now it is giving me 'Access of shared member, constant member, enum member or nested type through an instance; qualifying expression will not be evaluated.'

here is my revised code

    <System.Web.Services.WebMethod()>
Public Shared Function getServerName(suffix As String) As String
    Dim connection As SqlConnection
    Dim command As SqlCommand
    Dim readData As SqlDataReader
    Dim path As String
    path = ""

    connection = New SqlConnection(System.Configuration.ConfigurationManager.AppSettings("getServer"))
    connection.Open()
    command = New SqlCommand("select [Email_Notification_Date] from GlobalDB where [Email_Notification_Date]='Batman'", connection)
    readData = command.ExecuteReader
    While readData.Read()
        path = readData.Item("Email_Notification_Date")
    End While
    connection.Close()
    Return ("http://") + path + suffix
End Function

I have edited the main file to include a string as the parameter.

PopupWindow=window.open(<%= (New getServerName).getserverName("/Quoteman/DatePicker.aspx?Ctl=") %> + ctl,'DatePicker',settings);

Upvotes: 1

Views: 266

Answers (3)

Paul Chen
Paul Chen

Reputation: 1881

You are calling server side (you're connecting to a database) code from client side, the esiest way is, write the server name to your javascript on server.

PopupWindow = window.open('Http://' + '<%= GetServerName.getserverName() %>' +'/Quoteman/...');

Another possible way is to use AJAX call. You can check this blog post: http://encosia.com/using-jquery-to-directly-call-aspnet-ajax-page-methods/

It's using C# on server side, but that's OK, you can mark your function like this:

<WebMethod> _
Public Shared Function getserverName() As String

Upvotes: 2

Mako Steel
Mako Steel

Reputation: 29

Looks like returning a string is from a server method is what you want in which case you can use PageMethod object:

You can enable it by declaring script manager and Set the EnablePageMethods attribute to true

<asp:ScriptManager ID="ScriptManager1" 
 EnablePageMethods="true" 
 EnablePartialRendering="true" runat="server" />

the declare the function as WebMethod like so It must be SHARED:

<System.Web.Services.WebMethod> _
Public Shared Function getserverName() As String

End Function

Then script:

<script>
    function test(){
        alert(PageMethods.getserverName());
    }
</script>

There is also a way to do it with with jQuery but I haven't done it that way but Check it out

Soruce of this example can be found here but in C# syntax.

Hope that helps.

Upvotes: 1

David Sdot
David Sdot

Reputation: 2333

JavaScript runtime error: 'getServername' is undefined

The function is called getserverName, JavaScript is case sensitive, maybe thats all.

Upvotes: 1

Related Questions