2015-10-23 133 views
0

我想了解如何在VB.NET中使用PageMethods,但我遇到了一些問題。出於某種原因,我無法設法讓PageMethods調用的方法運行。PageMethods not working

這裏是JavaScript函數:

<script type="text/javascript"> 
    function AddHouse() { 
     var address = document.getElementById("addrTxt").valueOf; 
     var city = document.getElementById("cityTxt").valueOf; 
     var state = document.getElementById("stateTxt").valueOf; 
     var zip = parseInt(document.getElementById("zipTxt").valueOf); 
     var firstName = document.getElementById("rFirstName").valueOf; 
     var lastName = document.getElementById("rLastName").valueOf; 
     var rent = parseInt(document.getElementById("rentAmt").valueOf); 
     PageMethods.InsertHouse(address, city, state, zip, firstName, lastName, rent); 
    } 
</script> 

而這裏的VB.NET代碼:

Public Class _Default 
Inherits Page 

Private Shared dbConnection As String 
Private file As String = "C:\\Projects\\HousingInfo\\HousingInfo\\bin\\housingInfo.db3" 

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load 
    dbConnection = String.Format("Data Source={0}", file) 
    CreateTables() 
End Sub 

Private Sub CreateTables() 
    Dim connection = New SQLiteConnection(dbConnection) 
    If Not My.Computer.FileSystem.FileExists("C:\\Projects\\HousingInfo\\HousingInfo\\bin\\housingInfo.db3") Then 
     SQLiteConnection.CreateFile("C:\\Projects\\HousingInfo\\HousingInfo\\bin\\housingInfo.db3") 
    End If 
    Using Query As New SQLiteCommand() 
     connection.ConnectionString = dbConnection 
     connection.Open() 
     With Query 
      .Connection = connection 
      .CommandText = "CREATE TABLE IF NOT EXISTS houses(id INTEGER PRIMARY KEY AUTOINCREMENT, address TEXT, city TEXT, state TEXT, 
       zipCode INTEGER, rent INTEGER, rFirstName TEXT, rLastName TEXT)" 
     End With 
     Query.ExecuteNonQuery() 
     connection.Close() 
    End Using 
End Sub 

<Services.WebMethod()> 
Public Shared Sub InsertHouse(ByVal addr As String, ByVal city As String, ByVal state As String, ByVal zip As Integer, ByVal firstName As String, 
           ByVal lastName As String, ByVal rent As Integer) 
    Dim connection = New SQLiteConnection(dbConnection) 
    Using Query As New SQLiteCommand() 
     connection.ConnectionString = dbConnection 
     connection.Open() 
     With Query 
      .Connection = connection 
      .CommandText = String.Format("INSERT INTO houses(address, city, state, zipCode, rent, rFirstName, rLastName) VALUES ('{0}', '{1}', '{2}', {3}, {4}, '{5}', '{6}'", 
             addr, city, state, zip, firstName, lastName, rent) 
     End With 
     Query.ExecuteNonQuery() 
     connection.Close() 
    End Using 
End Sub 
End Class 
+0

你確定你要進入PageMethods.InsertHouse行嗎?你得到了什麼JS錯誤? – Caveman

回答

0

您使用了錯誤的屬性來獲取控件的值,重命名 '的valueOf' '價值',如下所示:

對'valueOf'的所有其他情況都這樣做

var address = document.getElementById("addrTxt").value; 
+0

感謝您的幫助,但它仍然沒有調用該方法。 – Michael