gpt4 book ai didi

c# - 如何创建向数据库提交信息的单页 ASP.NET 应用程序?

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

好吧,我知道这很尴尬,因为我已经作为 ASP.NET 的 Web 开发人员工作了几个月,但我过去常常从一个基本上预先打包的 ASP.NET MVC 站点开始,其中包含路由、 Controller 等,并且从那里延伸它。我现在需要做的就是创建一个向数据库提交表单的单页 ASP.NET 页面。

我的表已在数据库中

 CREATE TABLE stuff ( field1 VARCHAR (100), field2 VARCHAR (100) ); 

我的 HTML 中有一个表单

<form id="myform">
<input type="text" name="field1"/>
<input type="text" name="field2"/>
<input type="submit"/>
</form>

我有一个函数

  $('#myform input[type="submit"]').click(function(){
var that = this;
$.ajax({
url: '????',
method: 'POST',
data: new FormData($(that).closest('form')[0]),
success: function() { alert("Well, at least this succeeded"); }
});

我从 Visual Studio 中开始使用“ASP.NET 空 Web 应用程序 Visual C#”,但我真的不知道需要右键单击什么类型的文件 - 添加到项目来处理此问题。我想做的只是将输入 field1field2 简单插入到数据库中相应的列中。我找不到任何关于如何从头开始构建 ASP.NET 页面的资源,而且我读过的所有书籍都是从一个包含所有内容的模板开始的,所以...

有人可以告诉我如何将这些点连接起来吗?

最佳答案

这是代码

Web 方法必须是公共(public)且静态的

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script src="http://ajax.aspnetcdn.com/ajax/jquery/jquery-1.8.0.js"></script>
<script src="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.22/jquery-ui.js"></script>
<link rel="Stylesheet" href="StyleSheet.css" />

<script type="text/javascript">
$(document).ready(function () {
$('#Button1').click(function (e) {
alert('Data Saved')
var commentsCorrespondence;

var ddlST = $('#ddlStatus option:selected').val();// get dropdown selected value in ddlST variable
var chkbox = $('#ChkValue').val();// get checkbox value in chkbox
var Date = $('#txtDate').val();//get textbox value in date variable
$.ajax({
type: "POST",
url: "AutoCompleteCity.aspx/SaveData", //this is the url from which you call your web method ! in my case its /Default.aspx and method name is SaveData
data: "{'status': '" + ddlST + "','chkBoxValue': '" + chkbox + "','DueDate': '" + Date + "'}",// These are the method parameters in my case 'status' , 'chkBoxValue' and 'DueDate' are parameters
contentType: "application/json; charset=utf-8",
dataType: "json",
success: alert('Data Saved'),
failure: function (response) {
Message = response.d;
}
});

return false;
});
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="txtDate" runat="server"></asp:TextBox>
<asp:DropDownList ID="ddlStatus" runat="server"></asp:DropDownList>
<asp:CheckBox ID="ChkValue" runat="server" />
<asp:Button ID="Button1" runat="server" Text="Button" />
<br />
<br />
<br />
</div>
</form>

</body>
</html>

代码隐藏

using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class AutoCompleteCity : System.Web.UI.Page
{
[WebMethod]
public static void SaveData(string status, string chkBoxValue, string DueDate)
{
List<string> Emp = new List<string>();
string query = string.Format("Insert Into [Table] Values ({0},{1},{2})", status, chkBoxValue, DueDate);
using (SqlConnection con = new SqlConnection("your Connection string"))
{
using (SqlCommand cmd = new SqlCommand(query, con))
{
con.Open();
cmd.ExecuteNonQuery();
}
}
}
protected void Page_Load(object sender, EventArgs e)
{

}
}

关于c# - 如何创建向数据库提交信息的单页 ASP.NET 应用程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34149720/

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