C# ASP Ajax插入不起作用
我正在使用ASP,我希望在不刷新页面的情况下向数据库插入一些数据,从而使用ajax。 但是由于某种原因,我无法确定,数据没有被插入 my index.aspx:C# ASP Ajax插入不起作用,c#,asp.net,ajax,C#,Asp.net,Ajax,我正在使用ASP,我希望在不刷新页面的情况下向数据库插入一些数据,从而使用ajax。 但是由于某种原因,我无法确定,数据没有被插入 my index.aspx: <form id="form1" runat="server"> <div> <asp:TextBox ID="txtName" runat="server"></asp:TextBox> <asp:TextBox ID="txtDesc" r
<form id="form1" runat="server">
<div>
<asp:TextBox ID="txtName" runat="server"></asp:TextBox>
<asp:TextBox ID="txtDesc" runat="server"></asp:TextBox>
</div>
</form>
<a href="#" id="btnInsert" onclick="insertData()">INSERT</a>
<script>
function insertData() {
var name = document.getElementById("txtName").value;
var description = document.getElementById("txtDesc").value;
alert(name + description);
$.ajax(
{
type: "POST",
url: "insertAjax.aspx/Insert",
data: "{'name':'" + name + "','description':'" + description + "'}",
contentType: "application/json;charset=utf-8",
dataType: "json",
success: function () { alert("Inserted"); },
error: function () { alert("Not inserted"); }
});
}
</script>
Product.dbml(使用linq到SQL):
将
[WebMethod]
属性添加到您的方法仍然没有做任何事情,我怀疑我没有正确执行ajax功能,但在学习过程中无法告诉im…文档。getElementById(“txtName”)。值
也会产生无效结果-您必须设置一个静态IDClientIDMode=“Static
或获取服务器ID文档。getElementById(“”)
是否显示您的警报(“插入/未插入”)
?不,我没有收到该警报
public partial class insertAjax : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
public void Insert(string name, string description) {
using (ProductDataContext db = new ProductDataContext()) {
Product dt = new Product();
dt.Name = name;
dt.Description = description;
db.Products.InsertOnSubmit(dt);
db.SubmitChanges();
}
}
}
ID int (primary key, auto increment)
Name varchar(255)
Description varchar(255)