我正在尝试使用Vb.Net中的Json对象从SQL服务器检索数据。我将检索到的值添加到datatable但是添加的值没有显示在aspx页面中。为什么?
我的代码:
客户端:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>View</title>
</head>
<body>
<form id="form1" runat="server">
<table border="0" >
<tr>
<td>
<asp:Label ID= "lblName" runat="server" Text="Name" ></asp:Label>
</td>
<td>
<asp:TextBox ID="txtName" runat="server" Text="" /><br />
</td>
</tr>
<tr>
<td>   </td>
</tr>
<tr>
<td colspan ="2" >
<asp:Button ID="btnShow" Text="Show" runat="server" />
</td>
</tr>
</table>
<hr />
<asp:GridView ID="gvUsers" runat="server" AutoGenerateColumns="false" HeaderStyle-BackColor="#3AC0F2"
HeaderStyle-ForeColor="White" RowStyle-BackColor="#A1DCF2">
</asp:GridView>
</form>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<script type="text/javascript" src="https://cdn.jsdelivr.net/json2/0.2/json2.js"></script>
<script type="text/javascript">
$(document).ready(function () {
BindGridView();
});
function BindGridView() {
$("[id*=btnShow]").bind("click", function () {
var user = {};
user.Name = $("[id*=txtName]").val();
$.ajax({
type: "POST",
url: "View.aspx/ViewUser",
data: '{user: ' + JSON.stringify(user) + '}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
response(data.t);
$("#gvUsers").empty();
if (data.d.length > 0) {
$("#gvUsers").append("<tr><th>Username</th><th>Password</th></tr>");
for (var i = 0; i < response.d.length; i++) {
$("#gvUsers").append("<tr><td>" +
response.d[i].Username + "</td> <td>" +
response.d[i].Password + "</td></tr>");
}
}
}
});
return;
});
};
</script>
</body>
</html>
代码背后:
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Imports System.Web.Services
Imports System.Web.Script.Services
Imports System.Collections.Generic
Partial Class View
Inherits System.Web.UI.Page
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not IsPostBack Then
BindDummyItem()
End If
End Sub
Public Sub BindDummyItem()
Dim dtGetData As New DataTable()
dtGetData.Columns.Add("Username")
dtGetData.Columns.Add("Password")
dtGetData.Rows.Add()
gvUsers.DataSource = dtGetData
gvUsers.DataBind()
End Sub
<WebMethod()> _
<ScriptMethod()> _
Public Shared Sub ViewUser(user As Users)
Dim Detail As New List(Of Users)()
Dim dt As New DataTable()
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As New SqlConnection(constr)
Using cmd As New SqlCommand("SELECT * FROM Users WHERE Username = @Name")
Using sda As New SqlDataAdapter()
cmd.CommandType = CommandType.Text
cmd.Parameters.AddWithValue("@Name", user.Name)
cmd.Connection = con
sda.SelectCommand = cmd
sda.Fill(dt)
For Each dtRow As DataRow In dt.Rows
Dim DataObj As New Users()
DataObj.Name = dtRow("Username").ToString()
DataObj.Password = dtRow("Password").ToString()
Detail.Add(DataObj)
Next
End Using
End Using
End Using
Return
End Sub
End Class
Public Class Users
Public Property Name() As String
Get
Return _Name
End Get
Set(value As String)
_Name = value
End Set
End Property
Private _Name As String
Public Property Password As String
Get
Return _Password
End Get
Set(Value As String)
_Password = Value
End Set
End Property
Private _Password As String
End Class
执行此代码时,检索到的值会添加到详细信息列表中,但不会显示在网页中。告诉我这是检索数据的正确程序?提前谢谢。
答案 0 :(得分:0)
这个怎么样:
cmd.CommandType = CommandType.Text
cmd.Parameters.AddWithValue("@Name", user.Name)
cmd.Connection = con
sda.SelectCommand = cmd
sda.Fill(dt)
gvUsers.DataSource = dt
gvUsers.DataBind()