2015-10-23 100 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