如何在Asp中使用Jquery向数据库添加数据 [英] How Do I Add Data To Database Using Jquery In Asp
本文介绍了如何在Asp中使用Jquery向数据库添加数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经写了这些代码,但这些数据的问题是什么,不能保存在数据库中。
请帮帮我...
Tahnk you
这是我的Asp代码
I have write these codes but what is the problem with these data not save in database.
Please help me...
Tahnk you
Here is my Asp code
<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="ContactInfo.aspx.cs" Inherits="AspjQuerySaveData.ContactInfo1" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
<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>
$(document).ready(function () {
$('#btnsave').click(function () {
var contactName = $('#<%=txtContactName.ClientID%>').val();
var contactNo = $('#<%=txtContactNo.ClientID%>').val();
$('#LoadingPanel').show();
});
$.ajax({
url: "ContactInfo.aspx/SaveData",
type: "POST",
contentType: "application/json;charset=utf-8",
dataType: "json",
async: true,
cache: false,
data: JSON.stringify({
"contactName":contactName,
"contactNo": contactNo
}),
success: function (data) {
if (data.d == "success") {
alert("Data Saved Successfully");
$('#<%=txtContactName.ClientID%>').val('');
$('#<%=txtContactNo.ClientID%>').val('');
}
},
error: function (xhr, ajaxOptions, thrownError) {
alert(thrownError);
}
}).done(function () {
$('#LoadingPanel').hide()
})
});
</script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="FeaturedContent" runat="server">
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="MainContent" runat="server">
<div>
<table>
<tr>
<td>Contact Name:</td>
<td><asp:TextBox runat="server" Width="200px" ID="txtContactName" /></td>
</tr>
<tr>
<td>Contact Number:</td>
<td><asp:TextBox runat="server" ID="txtContactNo" Width="200px" /></td>
</tr>
<tr>
<td><input type="button" id="btnsave" style="width:200px;" value="Submit" /></td>
<td><div id="LoadingPanel" style="display:none; font-weight:bold;" >Data Saving ...</div></td>
</tr>
</table>
</div>
</asp:Content>
和C#
And C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
namespace AspjQuerySaveData
{
public partial class ContactInfo1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
[WebMethod]
public static string SaveData(string contactName, string contactNo)
{
string status = "";
ContactInfo c = new ContactInfo {Id=0,ContactName=contactName,ContactNo=contactNo};
using (DatabaseEntities dc = new DatabaseEntities())
{
dc.ContactInfoes.Add(c);
dc.SaveChanges();
return status;
}
}
}
}
推荐答案
(document).ready( function(){
(document).ready(function () {
(' #btnsave')。 click(function(){
var contactName =
('#btnsave').click(function () { var contactName =
(' #<%= txtContactName.ClientID%>')。val();
var contactNo =
('#<%=txtContactName.ClientID%>').val(); var contactNo =
这篇关于如何在Asp中使用Jquery向数据库添加数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文