Jquery触发c#函数

本文关键字:函数 触发 Jquery | 更新日期: 2023-09-27 18:12:47

下面是我的代码,代码看起来很好,但无法在input = '8'之后调用我的c#函数。

我不知道我做错了什么。请指导和帮助我,非常感谢。
<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication10._Default" %>
<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
    <br />
    <div style="text-align: center">
        <br />
        <input autofocus="autofocus"  id="myinput1" />
        <br />
        <hr />
    </div>
<script type="text/javascript">
    $(document).ready(function () {
        $("#myinput1").on('input', function () {
            if ($(this).val().length >= 8) {
                $.ajax({
                    type: "POST",
                    url: "Default.aspx/fillfields",
                    contentType: "application/json; charset=utf-8",
                    data: '{input: "' + $("#myinput1").val() + '"}',
                    dataType: "json"
                });
                $('#myinput1').val("");
            }
        });
    });
    </script>
</asp:Content>
代码:

using System;
using System.Web.UI;
using System.Data.SqlClient;
using System.Configuration;
namespace WebApplication10
{
    public partial class _Default : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }
        [System.Web.Services.WebMethod]
        public static void fillfields(string input)
        {
            string constr = ConfigurationManager.ConnectionStrings["DbConnection"].ConnectionString;
            using (SqlConnection connection = new SqlConnection(constr))
            {
                connection.Open();
                using (SqlCommand command = new SqlCommand("TMS_INSERT", connection))
                {
                    command.CommandType = System.Data.CommandType.StoredProcedure;
                    command.Parameters.AddWithValue("@EMP_ID", input);
                    command.ExecuteNonQuery();
                }
                connection.Close();
            }
        }
    }
}

我已经被困在这个几个小时,试图检查浏览器控制台,但它是空白的,试图在我的c#中设置一个断点,但它没有响应。

Jquery触发c#函数

Web Method应为static。此外,使用ExecuteReader也没有意义,因为你没有从数据库中读取任何东西。而是使用ExecuteNonQuery来执行这个过程。

        [System.Web.Services.WebMethod]
        public static void fillfields(string input)
        {
            string constr = ConfigurationManager.ConnectionStrings["DbConnection"].ConnectionString;
            using (SqlConnection connection = new SqlConnection(constr))
            {
                connection.Open();
                using (SqlCommand command = new SqlCommand("TMS_INSERT", connection))
                {
                    command.CommandType = System.Data.CommandType.StoredProcedure;
                    command.Parameters.AddWithValue("@EMP_ID", input);
                    command.ExecuteNonQuery();
                }
                //myinput1.Value = string.Empty;
                connection.Close();
            }
        }

修改ajax函数代码,如下所示:

     <%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication10._Default" %>
    <asp:Content ID="headcontent" ContentPlaceHolderID="head" runat="server">
    <script type="text/javascript">
        $(document).ready(function () {
            $("#myinput1").on('input', function () {
                if ($(this).val().length >= 8) {
                    $.ajax({
                        type: "POST",
                        url: "Default.aspx/fillfields",
                        contentType: "application/json; charset=utf-8",
                        data: '{input: "' + $("#myinput1").val() + '"}',
                        dataType: "json"
                    });
                    $('#myinput1').val("");
                }
            });
        });
    </script>
</asp:Content>
    <asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
        <br />
        <div style="text-align: center">
            <br />
            <input autofocus="autofocus"  id="myinput1" />
            <br />
            <hr />
        </div>
    </asp:Content>

您的事件绑定input不是一个有效的事件名称:https://api.jquery.com/category/events/

用于测试目的:

$("#myinput1").on('blur', function() .... );

和tab退出字段。最好还是对你的用户好一点,给他们一个按钮让他们点击,这样他们就知道是什么触发了这个动作。