上一篇博客写到用javascript生成多组文本,可以让数据的输入不受显示,现在我们需要把这些输入写入数据库,这里就用到json传入。
首先,我们来写一下后台如何生成要传输的数据
[html]
复制代码 代码如下:
function generateDtb() {
//写入
var txtName = document.getElementById("txtName").value;
//创建数组
var dtb = new Array();
//通过循环把数据写入到数组并返回
for (var i = 0; i < firstGroup.length; i++) {
var row = new Object();
row.Name = txtName;
row.fullMoney = firstGroup[i].value;
row.discount = secondGroup[i].value;
dtb.push(row);
}
return dtb;
}
function generateDtb() {
//写入
var txtName = document.getElementById("txtName").value;
//创建数组
var dtb = new Array();
//通过循环把数据写入到数组并返回
for (var i = 0; i < firstGroup.length; i++) {
var row = new Object();
row.Name = txtName;
row.fullMoney = firstGroup[i].value;
row.discount = secondGroup[i].value;
dtb.push(row);
}
return dtb;
}
把数组转换成json串传入到后台:
[html]
复制代码 代码如下:
$(function () {
//点击botton1
$("#lbtnOK").click(function () {
var url = "DiscountManger.aspx?ajax=1";
var dtb = generateDtb();
// var strName = document.getElementById("txtName").value;
if (dtb == null)
{ }
else {
//序列化对象
var postdata = JSON.stringify(dtb);
//异步请求
$.post(url, { json: postdata }, function (json) {
if (json) {
jBox.tip("添加成功!", "提示");
location.reload();
}
else {
jBox.tip("添加失败!", "提示");
location.reload();
}
}, "json")
}
});
});
$(function () {
//点击botton1
$("#lbtnOK").click(function () {
var url = "DiscountManger.aspx?ajax=1";
var dtb = generateDtb();
// var strName = document.getElementById("txtName").value;
if (dtb == null)
{ }
else {
//序列化对象
var postdata = JSON.stringify(dtb);
//异步请求
$.post(url, { json: postdata }, function (json) {
if (json) {
jBox.tip("添加成功!", "提示");
location.reload();
}
else {
jBox.tip("添加失败!", "提示");
location.reload();
}
}, "json")
}
});
});
在后台的操作:
首先判断是否需要传输数据
[html]
复制代码 代码如下:
if (!IsPostBack)
{
//判断是否异步请求
if (Request.QueryString["ajax"] == "1")
{
ProcessRequest();
}
if (!IsPostBack)
{
//判断是否异步请求
if (Request.QueryString["ajax"] == "1")
{
ProcessRequest();
}
在这里进行对数据的处理:
[html]
复制代码 代码如下:
/// <summary>
/// 处理异步请求
/// </summary>
private void ProcessRequest()
{
//存入要填写的策略
ArrayList arrDiscount = new ArrayList();
Response.ContentType = "text/html";
string json = Request.Form["json"];
//反序列化DataTable
if (json == null)
{
return;
}
else
{
DataTable newdtb = Json2Dtb(json);
for (int i = 0; i < newdtb.Rows.Count; i++)
{
Entity.StrategyDiscount enStrategyDiscount = new Entity.StrategyDiscount();
//打折方案名
enStrategyDiscount.name = newdtb.Rows[i]["Name"].ToString();
//商店ID
enStrategyDiscount.shopId = long.Parse(LoginInfo.ShopID);
enStrategyDiscount.fullMoney = Convert.ToDecimal(newdtb.Rows[i]["fullMoney"].ToString());
enStrategyDiscount.discount = Convert.ToDecimal(newdtb.Rows[i]["discount"].ToString());
//写入数据到数组
arrDiscount.Add(enStrategyDiscount);
}
//写入数据到数据库
IStrategyBLL strategy = new StrategyBLL();
if (strategy.AddStrategyDiscount(arrDiscount))
{
Response.Write("true");
Response.End();
}
else
{
Response.Write("false");
Response.End();
}
}
/// <summary>
/// 处理异步请求
/// </summary>
private void ProcessRequest()
{
//存入要填写的策略
ArrayList arrDiscount = new ArrayList();
Response.ContentType = "text/html";
string json = Request.Form["json"];
//反序列化DataTable
if (json == null)
{
return;
}
else
{
DataTable newdtb = Json2Dtb(json);
for (int i = 0; i < newdtb.Rows.Count; i++)
{
Entity.StrategyDiscount enStrategyDiscount = new Entity.StrategyDiscount();
//打折方案名
enStrategyDiscount.name = newdtb.Rows[i]["Name"].ToString();
//商店ID
enStrategyDiscount.shopId = long.Parse(LoginInfo.ShopID);
enStrategyDiscount.fullMoney = Convert.ToDecimal(newdtb.Rows[i]["fullMoney"].ToString());
enStrategyDiscount.discount = Convert.ToDecimal(newdtb.Rows[i]["discount"].ToString());
//写入数据到数组
arrDiscount.Add(enStrategyDiscount);
}
//写入数据到数据库
IStrategyBLL strategy = new StrategyBLL();
if (strategy.AddStrategyDiscount(arrDiscount))
{
Response.Write("true");
Response.End();
}
else
{
Response.Write("false");
Response.End();
}
}
这里,我们需要把json转换成datatable
[html]
复制代码 代码如下: