vb.net 如何解决此错误“不推荐使用 getPreventDefault()。改用 defaultPrevented。”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41282673/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 20:08:59  来源:igfitidea点击:

how to resolve this error "Use of getPreventDefault() is deprecated. Use defaultPrevented instead."

javascriptjqueryjsonvb.netsql-server-2005-express

提问by MDP

I tried to fetch datas for a particualr user from sql sever database using json data. But its always showing error in the console as:

我尝试使用 json 数据从 sql sever 数据库中为特定用户获取数据。但它总是在控制台中显示错误为:

"Use of getPreventDefault() is deprecated. Use defaultPrevented instead."

“不推荐使用 getPreventDefault()。改用 defaultPrevented。”

And values are not retrieving from the database.

并且值不是从数据库中检索的。

My code is :

我的代码是:

Client-side:

客户端:

<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">
    <Columns>
        <asp:BoundField DataField="Username" HeaderText="Username" />
        <asp:BoundField DataField="Password" HeaderText="Password" />
    </Columns>
</asp:GridView> 
</form>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript" src="http://cdn.jsdelivr.net/json2/0.1/json2.js"></script>
<script type="text/javascript">
    $(function () {
        $("[id*=btnShow]").bind("click", function () {
            var user = {};
            user.Name = $("[id*=txtName]").val();
            user.grd = $("[id*=gvUsers]").val();
            $.ajax({
                type: "POST",
                url: "View.aspx/ViewUser",
                data: '{user: ' + JSON.stringify(user) + '}',
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (response) {
                    //alert("User has been added successfully.");
                    window.location.reload();
                }
            });
            return false;
        });
    });
</script>
</body>

Server-Side :

服务器端 :

<WebMethod()> _
<ScriptMethod()> _
Public Shared Sub ViewUser(user As Users)
    Dim grd As GridView
    grd = user.grd
    'Dim gvUsers As GridView
    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()
                Dim dt As New DataTable()
                cmd.CommandType = CommandType.Text
                cmd.Parameters.AddWithValue("@Name", user.Name)
                cmd.Connection = con
                sda.SelectCommand = cmd
                sda.Fill(dt)
                'grd.Visible = True
                grd.DataSource = dt
                grd.DataBind()
            End Using
        End Using
    End Using
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 grd() As GridView
    Get
        Return _grd
    End Get
    Set(value As GridView)
        _grd = value
    End Set
End Property
Private _grd As GridView
End Class

But my datas are not displaying in the webpage. Thank you in advance

但是我的数据没有显示在网页中。先感谢您

回答by T.J. Crowder

This is just because you're using a version of jQuery that caters to older browsers in a slightly-sloppy fashion. The version you're using has this code in it:

这只是因为您使用的 jQuery 版本以稍微马虎的方式迎合了旧浏览器。您使用的版本中包含以下代码:

this.isDefaultPrevented=e.defaultPrevented||e.returnValue===!1||e.getPreventDefault&&e.getPreventDefault()

As you can see, it does try to use the defaultPreventedproperty, but then goes on to call getPreventDefaultif the default hasn't been prevented. So even on browsers with the property, if the default hasn't been prevented, it will call the old function if present.

如您所见,它确实尝试使用该defaultPrevented属性,但getPreventDefault如果未阻止默认设置,则继续调用。因此,即使在具有该属性的浏览器上,如果未阻止默认设置,它也会调用旧函数(如果存在)。

Newer versions of jQuery (2.x, 3.x) don't use getPreventDefaultanymore. If you want to get rid of the warning, either use a more up-to-date jQuery or hack the one you're using so that it checks for the existence of the property rather than just checking its value.

较新版本的 jQuery(2.x、3.x)不再使用getPreventDefault。如果您想摆脱警告,请使用更新的 jQuery 或破解您正在使用的 jQuery,以便它检查属性是否存在,而不仅仅是检查其值。