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());
}
}