1

我有一个函数,其中包含我从数据库访问的变量。我正在尝试以下述方式将该变量与 URL 连接起来。

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

尝试编译代码时收到错误消息。
这是功能:

     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

这是我试图调用该函数的地方:

    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();
}

PS该函数确实返回一个值。

编辑:抱歉,忘了说我正在尝试从 javascript 调用 VB 函数这是我从错误中得到的窗口。

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

编辑:我向函数添加了一个参数,现在它给了我“通过实例访问共享成员、常量成员、枚举成员或嵌套类型;将不评估限定表达式。

这是我修改后的代码

    <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

我已经编辑了主文件以包含一个字符串作为参数。

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

3 回答 3

2

您正在从客户端调用服务器端(您正在连接到数据库)代码,最简单的方法是将服务器名称写入服务器上的 javascript。

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

另一种可能的方式是使用 AJAX 调用。您可以查看此博客文章:http ://encosia.com/using-jquery-to-directly-call-aspnet-ajax-page-methods/

它在服务器端使用 C#,但没关系,你可以像这样标记你的函数:

<WebMethod> _
Public Shared Function getserverName() As String
于 2013-07-25T08:49:18.037 回答
1

JavaScript 运行时错误:“ getServername ”未定义

该函数称为getserverName,JavaScript 区分大小写,也许就是这样。

于 2013-07-25T08:40:19.647 回答
1

看起来返回的字符串来自服务器方法是您想要的,在这种情况下您可以使用PageMethod对象:

您可以通过声明脚本管理器并将EnablePageMethods属性设置为true来启用它

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

像这样将函数声明为WebMethod必须是共享的:

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

End Function

然后脚本:

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

还有一种使用 jQuery 的方法,但我没有这样做,但检查一下

可以在此处找到此示例的来源,但使用 C# 语法。

希望有帮助。

于 2013-07-25T08:58:03.747 回答