我正在学习如何使用PageMethods与VB。NET,我遇到了一些问题。由于某些原因,我无法让PageMethods调用的方法运行。
下面是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
您正在使用错误的属性来获取控件的值,将'valueOf'重命名为'value',如下所示:
对所有其他出现的'valueOf'执行此操作
var address = document.getElementById("addrTxt").value;