gpt4 book ai didi

c# - jquery触发c#函数

转载 作者:行者123 更新时间:2023-11-30 16:00:30 24 4
gpt4 key购买 nike

下面是我的代码,代码看起来不错,但在 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# 中放置一个断点但它没有响应。

最佳答案

Web 方法应该是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>

关于c# - jquery触发c#函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40012288/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com