jQuery+ajax从数据库取值和和绑定前台html控件上,然后再通过ajax把值传到后台,让后台写入到数据库
<script type="text/javascript">
var Json=<%=Json %>;//等于取后台变量
var Bind;
var DishID;//全局变量,用于编辑传入主键
var arry_dishID=new Array();
var arry_imgUrl="";
function BindData(){//绑定数据
if(Json!=0&&!!_heditor){
$("#OrderID").val(Json.OrderID);
$("#DishName").val(Json.DishName);
$("#memory").val(Json.Mnemonics);
$("#Img_url").val(Json.ImgSrc);
arry_imgUrl=FileDomain+"canyin/"+Json.ImgSrc;
$("#img_id").attr("src",arry_imgUrl);
_heditor.html(Json.DishDesc);
$("#<%=DishSortList.ClientID %>").val(Json.SortID);
$("#<%=DishtasteList.ClientID %>").val(Json.TasteID);
$("#dishStar").val(Json.DishStar);
$("input[type][id='Istakeout'][value='"+Json.IsTakeOut+"']").attr("checked","checked");
DishID=Json.DishID;
$("input[type][id='dish_tag1'][value='"+Json.tjc+"']").attr("checked","checked");
$("input[type][id='dish_tag2'][value='"+Json.hpc+"']").attr("checked","checked");
$("input[type][id='dish_tag3'][value='"+Json.xpc+"']").attr("checked","checked");
$("#price").val(Json.Price);
$("#<%=unitList.ClientID %>").val(Json.UnitID);
}
}
$(document).ready(function () {//jQuery写在这里面
SetBtn({
AddBtn: false, SeachBtn: false
});
Bind = setInterval("BindData()", 50);//设置调用间隔毫秒级
$("input[type='checkbox'][name='dish_tag']").each(function () {//each() jQuery遍历 方法规定为每个匹配元素规定运行的函数。提示:返回 false 可用于及早停止循环。
var Tag_value_arry = Tag_value.split(',');
for (var i = 0; i < Tag_value_arry.length; i++) {
if ($(this).val() == Tag_value_arry[i]) {
$(this).attr("checked", "checked");
}
}
});
$("#Save").click(function () {
if (Json != 0) {//如果修改,则因为开始是Json是有值的,否则新增Json是空的
$.ajax({
url: "edit_dishes.aspx",//待载入页面的URL地址,比如本页面的修改就是在这个页面,然后修改的代码得写到这个后台里面
type: "post",
data: { "cmd": "Update",//待发送 Key/value 参数,即键值对,data里面的所有东西,在后台都是可以使用的,用request["DinnerID"]来得到值
"DishID":DishID,
"OrderID": $("#OrderID").val(),
"DishName": $("#DishName").val(),
"Mnemonics": $("#memory").val(),
"Img_url":$("#Img_url").val(),
"DishDesc": _heditor.html(), //简介
"SortID":$("#<%=DishSortList.ClientID %>").val(),
"TasteID":$("#<%=DishtasteList.ClientID %>").val(),
"DishStar":$("#dishStar").val(),//.options[index].val(), //星级
"IsTakeOut": $("input[name='IstakeOut']:checked").val(),
"Price":$("#price").val(),
"UnitID":$("#<%=unitList.ClientID %>").val(),
"dish_tag":$("input[name='dish_tag']:checked").val(),//传入推荐、好评、新品菜
"rad": Math.random()
},
beforeSend: function (XMLHttpRequest) { },
error: function () { },//这个应该是失败出现错误的时候调用的吧?
success: function (data) {//载入成功时回调函数。上面的data传入数据后,后台写方法,执行后输出一个值,然后下面根据输出值,选择操作
if (data == "success") {
VMsg.AlertSuccess("修改成功!");
window.location="dishes_list.aspx";
}
else {
VMsg.AlertError("修改失败!");
}
}
});
}
});
});
public partial class edit_dishes : BasePage
{
Dish dishModel = new Dish();
public string Json = "";
public string Tag_value = "0";
protected string DishID = string.Empty;
protected string action = string.Empty;
protected string imgPath = string.Empty;
protected string imgPath_0 = "";
protected void Page_Load(object sender, EventArgs e)
{
DishID = Request.QueryString["dishid"];
action = Request.QueryString["action"];
string cmd = Request["cmd"];
if (!IsPostBack)
{
bindDishSortList();
bindDishtasteList();
bindunitList();
}
if (!string.IsNullOrEmpty(action))
{
if (action == "edit") edit(); //editshow(DishID);
}
if (!string.IsNullOrEmpty(cmd))
{
if (cmd == "Update") operate();
}
}
/// <summary>
/// 获取菜品信息,既然是编辑,肯定是传主键,得到主键后,根据主键得到一个Model,把其所有信息全部取出放入Model中,然后对其Json序列化,
/// 即model里的是一个字段一个值,散装的,序列化后变成一个字符串的形式,
/// Json序列值 {"DishID":28,"ShopID":4,"AppID":4,"SortID":3,"CuisineID":1,"TasteID":9,"Price":12.00,"UnitID":12,"DishName":"鱼儿",
/// "Mnemonics":"yu","DishDesc":"a","DishStar":2,"IsShow":true,"IsTakeOut":true,"CreateTime":"\/Date(1364780994373)\/","OrderID":1,
/// "ImgSrc":null,"SP_Price":null,"Other":null}
/// </summary>
private void edit()
{
int dishid = 0;
int ShopID = CurrentUser.ShopID;
int AppID = CurrentUser.AppID;
string tagss = "";
if (int.TryParse(Request.QueryString["dishid"], out dishid))
{
dishModel = new Dish();//实例化一个model对象
dishModel = DishesDAL.getModelByID(ShopID,AppID,dishid.ToString());
tagss = DishesDAL.getDish_DishTagID(dishid, ShopID, AppID);
}
if (dishModel != null)
{
Json = KnetApps.Toolkit.Web.JsonSerializer.ToJson(dishModel);
}
}
/// <summary>
/// 添加/修改操作
/// </summary>
void operate()
{
string ss = Request.Form["dish_tag"];
int orderid = Convert.ToInt32(Request["OrderID"]);
string dishname = Request["DishName"].ToString();
string memory = Request["Mnemonics"].ToString();
string img_url = Request["Img_url"].ToString();
string dishdesc = Request["DishDesc"].ToString();
int sortid = Convert.ToInt32(Request["SortID"]);
int tasteid = Convert.ToInt32(Request["TasteID"]);
int dishstar = dishStar.SelectedIndex - 1;
bool istakeout = Convert.ToBoolean(Request["IsTakeOut"]);
bool isShow = true;
decimal price = Convert.ToDecimal(Request["Price"]);
int unitid = Convert.ToInt32(Request["UnitID"]);
string s = Request["dish_tag"];
int DishID = 0;
Dish dishmodel = new Dish()
{
OrderID=orderid,
DishName = dishname,
Mnemonics = memory,
DishDesc=dishdesc,
SortID=sortid,
TasteID=tasteid,
DishStar=dishstar,
IsTakeOut=istakeout,
IsShow=isShow,
Price=price,
UnitID=unitid,
AppID = CurrentUser.AppID,
ShopID = CurrentUser.ShopID,
DishID = Int32.TryParse(Request["DishID"], out DishID) ? DishID : 0
};
if (string.IsNullOrEmpty(dishname)) Output(true, "", Context);
else
{
Output(DishesDAL.SaveDishModel(dishmodel).ToString());
}
}