简体   繁体   English

使用Json对象(WebMethod)检索数据未显示在ASPX页面中

[英]Retriving datas using Json object(webmethod) is not displaying in aspx page

I am trying to retrieve data's from SQL server using Json object in Vb.Net.My retrieved values are added to datatable But the added values are not displaying in the aspx page. 我正在尝试使用Vb.Net中的Json对象从SQL Server检索数据。我检索到的值被添加到数据表中,但是添加的值未显示在aspx页中。 Why?? 为什么??

My code : 我的代码:

Client-Side : 客户端 :

<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> &nbsp </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>

In Code Behind : 在后面的代码中:

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

When I execute this code the retrieved values are added in Detail list but it does not displaying in the web page. 当我执行此代码时,检索到的值将添加到“详细信息”列表中,但不会显示在网页中。 Tell me it is the correct procedure to retrieve the data?? 告诉我这是检索数据的正确程序吗? Thanks in advance. 提前致谢。

How about this: 这个怎么样:

 cmd.CommandType = CommandType.Text
 cmd.Parameters.AddWithValue("@Name", user.Name)
 cmd.Connection = con
 sda.SelectCommand = cmd
 sda.Fill(dt)


 gvUsers.DataSource = dt
 gvUsers.DataBind()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM