2013-07-25 57 views
1

我有一個函數,其中包含我從數據庫訪問的變量。 我想以下面陳述的方式將該變量與URL連接起來。將變量傳遞到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(); 
} 

附:該函數確實會返回一個值。

編輯:對不起,忘了說我想從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); 
+0

你是從JavaScript調用VB.NET代碼? – Satpal

+0

是的,對不起。忘記指定語言 – GeoffWilson

+0

請指定錯誤並可能發佈日誌。它是一個編譯或運行時錯誤? –

回答

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 
+0

當我嘗試說'getserverName是一個類型並且不能用作表達式'時。 – GeoffWilson

+0

那麼你把這個函數放在一個名爲getServername的類中? – Diryboy

+0

是的,該文件位於App_Code文件夾中。 – GeoffWilson

1

JavaScript runtime error: 'getServername' is undefined

功能稱爲getserverName,JavaScript是大小寫敏感的,也許這就是全部。

+0

我改變了大小寫,但它仍然不適合我。但是,謝謝你。我會錯過它的。 – GeoffWilson

+1

PopupPicker是一個Javascript函數嗎? 如果是,那麼可能會工作> var serverName = getservername(); –

+0

我已經這樣做了,但仍然無法正常工作。非常好的一點,但。 – GeoffWilson

1

貌似返回一個字符串,從服務器的方法是什麼你想在這種情況下,你可以使用PageMethod對象:

可以通過聲明腳本管理器啓用和EnablePageMethods屬性設置爲真正

<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但我沒有這樣做,但Check it out

本例的Soruce可以在C#語法中找到,但是可以找到here

希望有所幫助。