/// <summary> /// 组合成对象实体 /// </summary> private Model.menu DataRowToModel(DataRow row) { Model.menu model = new Model.menu(); if (row != null) { if (row["id"].ToString() != null && row["id"].ToString() != "") { model.id = int.Parse(row["id"].ToString()); } if (row["title"].ToString() != "") { model.title = row["title"].ToString(); } if (row["link_url"].ToString() != "") { model.link_url = row["link_url"].ToString(); } if (row["parent_id"].ToString() != null && row["parent_id"].ToString() != "") { model.parent_id = int.Parse(row["parent_id"].ToString()); } if (row["class_list"].ToString() != "") { model.class_list = row["class_list"].ToString(); } if (row["class_layer"].ToString() != null && row["class_layer"].ToString() != "") { model.class_layer = int.Parse(row["class_layer"].ToString()); } if (row["open_mode"].ToString() != "") { model.open_mode = row["open_mode"].ToString(); } if (row["sort_id"].ToString() != null && row["sort_id"].ToString() != "") { model.sort_id = int.Parse(row["sort_id"].ToString()); } if (row["css_txt"].ToString() != "") { model.css_txt = row["css_txt"].ToString(); } if (row["img_url"].ToString() != "") { model.img_url = row["img_url"].ToString(); } if (row["is_lock"].ToString() != null && row["is_lock"].ToString() != "") { model.is_lock = int.Parse(row["is_lock"].ToString()); } if (row["add_time"].ToString() != null && row["add_time"].ToString() != "") { model.add_time = DateTime.Parse(row["add_time"].ToString()); } } return(model); }
private bool DoEdit(int _id) { try { BLL.menu bll = new BLL.menu(); Model.menu model = bll.GetModel(_id); model.title = txtName.Text; model.link_url = txtUrl.Text; //如果选择的父ID不是自己,则更改 int parentId = int.Parse(ddlParentId.SelectedValue); if (parentId != model.id) { model.parent_id = parentId; } model.open_mode = rblMode.SelectedValue; model.is_lock = int.Parse(rblHide.SelectedValue); model.sort_id = Utils.StrToInt(txtSort.Text, 99); model.css_txt = txtCssTxt.Text; //判断上传图片 if (this.imgUpload.HasFile) { //上传前先删除原图片 if (!string.IsNullOrEmpty(model.img_url)) { Utils.DeleteFile(model.img_url); } Model.upLoad upfile = new Web.UI.UpLoad().fileSaveAs(this.imgUpload.PostedFile, 0, false, false); if (upfile.status > 0) { model.img_url = upfile.path; } } else { //判断是否需要删除原图 if (txtIconUrl.Text.Trim() == "" && !string.IsNullOrEmpty(model.img_url)) { Utils.DeleteFile(model.img_url); } model.img_url = txtIconUrl.Text.Trim(); } if (bll.Update(model)) { AddAdminLog(DTEnums.ActionEnum.Edit.ToString(), "修改菜单内容:" + model.title); //记录日志 return(true); } } catch { return(false); } return(false); }
protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e) { TreeNode t = this.TreeView1.SelectedNode; int deptId = int.Parse(t.Value); Model.menu model = bll.GetModel(deptId); bindParentMenu(); lblpId.Text = model.menuId.ToString(); ddrParent.SelectedValue = model.parentId.ToString(); txtMenuName.Text = model.menuName; txtOrder.Text = model.orders.ToString(); txtUrl.Text = model.url; }
private void SiteMapBind() { if (this.parent_id > 0) { BLL.menu bll = new BLL.menu(); Model.menu model = bll.GetModel(this.parent_id); LabelStatus.Text += "<a href=\"index.aspx\">全部</a><i></i>"; DataTable dt = bll.GetList(0, "id in(" + model.class_list.Substring(1, (model.class_list).LastIndexOf(",") - 1) + ")", "charindex(','+ltrim(id)+',','" + model.class_list + "')", 0); foreach (DataRow dr in dt.Rows) { LabelStatus.Text += "<a href=\"index.aspx?parent_id=" + dr["id"].ToString() + "\">" + dr["title"].ToString() + "</a><i></i>"; } } }
private void ShowInfo(int _id) { BLL.menu bll = new BLL.menu(); Model.menu model = bll.GetModel(_id); txtName.Text = model.title; txtUrl.Text = model.link_url; ddlParentId.SelectedValue = model.parent_id.ToString(); txtSort.Text = model.sort_id.ToString(); rblHide.SelectedValue = model.is_lock.ToString(); rblMode.SelectedValue = model.open_mode; txtName.Focus(); //设置焦点,防止JS无法提交 txtCssTxt.Text = model.css_txt; txtIconUrl.Text = model.img_url; if (!string.IsNullOrEmpty(model.img_url)) { ImgDiv.Visible = true; IconUrl.ImageUrl = model.img_url; } }
/// <summary> /// 修改子节点的ID列表及深度(自身迭代) /// </summary> /// <param name="parent_id"></param> private void UpdateChilds(SqlConnection conn, SqlTransaction trans, int parent_id) { //查找父节点信息 Model.menu model = GetModel(conn, trans, parent_id); if (model != null) { //查找子节点 string strSql = "select id from [" + databaseprefix + "menu] where parent_id=" + parent_id; DataSet ds = DbHelperSQL.Query(conn, trans, strSql); //带事务 foreach (DataRow dr in ds.Tables[0].Rows) { //修改子节点的ID列表及深度 int id = int.Parse(dr["id"].ToString()); string class_list = model.class_list + id + ","; int class_layer = model.class_layer + 1; DbHelperSQL.ExecuteSql(conn, trans, "update [" + databaseprefix + "menu] set class_list='" + class_list + "', class_layer=" + class_layer + " where id=" + id); //带事务 //调用自身迭代 this.UpdateChilds(conn, trans, id); //带事务 } } }
private bool DoAdd() { try { BLL.menu bll = new BLL.menu(); Model.menu model = new Model.menu(); model.title = txtName.Text; model.link_url = txtUrl.Text; model.parent_id = int.Parse(ddlParentId.SelectedValue); model.open_mode = rblMode.SelectedValue; model.is_lock = int.Parse(rblHide.SelectedValue); model.sort_id = Utils.StrToInt(txtSort.Text, 99); model.css_txt = txtCssTxt.Text; //判断上传图片 if (this.imgUpload.HasFile) { Model.upLoad upfile = new Web.UI.UpLoad().fileSaveAs(this.imgUpload.PostedFile, 0, false, false); if (upfile.status > 0) { model.img_url = upfile.path; } } else { model.img_url = txtIconUrl.Text.Trim(); } model.add_time = DateTime.Now; if (bll.Add(model) > 0) { AddAdminLog(DTEnums.ActionEnum.Edit.ToString(), "添加菜单内容:" + model.title); //记录日志 return(true); } } catch { return(false); } return(false); }
/// <summary> /// 得到一个对象实体(重载,带事务) /// </summary> public Model.menu GetModel(SqlConnection conn, SqlTransaction trans, int id) { StringBuilder strSql = new StringBuilder(); strSql.Append("select " + this.column + " from " + databaseprefix + "menu"); strSql.Append(" where id=@id"); SqlParameter[] parameters = { new SqlParameter("@id", SqlDbType.Int, 4) }; parameters[0].Value = id; Model.menu model = new Model.menu(); DataSet ds = DbHelperSQL.Query(conn, trans, strSql.ToString(), parameters); if (ds.Tables[0].Rows.Count > 0) { return(DataRowToModel(ds.Tables[0].Rows[0])); } else { return(null); } }
/// <summary> /// 删除一条数据 /// </summary> public bool Delete(int id) { Model.menu model = GetModel(id); StringBuilder strSql = new StringBuilder(); strSql.Append("delete from " + databaseprefix + "menu where id=@id"); SqlParameter[] parameters = { new SqlParameter("@id", SqlDbType.Int, 4) }; parameters[0].Value = id; int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters); if (rows > 0) { Utils.DeleteFile(model.img_url); return(true); } else { return(false); } }
/// <summary> /// 更新一条数据 /// </summary> public bool Update(Model.menu model) { return(dal.Update(model)); }
/// <summary> /// 增加一条数据 /// </summary> public int Add(Model.menu model) { return(dal.Add(model)); }
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { bindMenuTree(); } if (RequsetAjax("edit")) { int menuId = int.Parse(Request.Form["menuid"].ToString()); Model.menu model = bll.GetModel(menuId); Response.Write("{\"status\":1,\"msg\":\"修改成功!\",\"menuId\":\"" + model.menuId + "\",\"menuName\":\"" + model.menuName + "\",\"parentId\":\"" + model.parentId + "\",\"orders\":\"" + model.orders + "\",\"url\":\"" + model.url + "\"}"); Response.End(); } if (RequsetAjax("editsubmit")) { //编辑 int menuId = int.Parse(Request.Form["menuid"].ToString()); string menuName = Request.Form["menuname"].ToString(); string menuOrder = Request.Form["menuorder"].ToString(); string menuUrl = Request.Form["menuurl"].ToString(); string parentId = Request.Form["parentId"].ToString(); Model.menu model = bll.GetModel(menuId); model.parentId = int.Parse(parentId); model.menuName = menuName; model.orders = int.Parse(menuOrder); model.url = menuUrl; bll.Update(model); Response.Write("{\"status\":1,\"msg\":\"修改成功!\"}"); Response.End(); } if (RequsetAjax("add")) { //添加 string menuName = Request.Form["menuname"].ToString(); string menuOrder = Request.Form["menuorder"].ToString(); string menuUrl = Request.Form["menuurl"].ToString(); string parentId = Request.Form["parentId"].ToString(); Model.menu model = new Model.menu(); model.parentId = int.Parse(parentId); model.menuName = menuName; model.orders = int.Parse(menuOrder); model.url = menuUrl; bll.Add(model); Response.Write("{\"status\":1,\"msg\":\"添加成功!\"}"); Response.End(); } if (RequsetAjax("del")) { //添加 int menuId = int.Parse(Request.Form["menuid"].ToString()); bll.Delete(menuId); Response.Write("{\"status\":1,\"msg\":\"添加成功!\"}"); Response.End(); } }
/// <summary> /// 更新一条数据 /// </summary> public bool Update(Model.menu model) { using (SqlConnection conn = new SqlConnection(DbHelperSQL.connectionString)) { conn.Open(); using (SqlTransaction trans = conn.BeginTransaction()) { try { //先判断选中的父节点是否被包含 if (IsContainNode(model.id, model.parent_id)) { //查找旧数据 Model.menu oldModel = GetModel(model.id); //查找旧父节点数据 string class_list = "," + model.parent_id + ","; int class_layer = 1; if (oldModel.parent_id > 0) { Model.menu oldParentModel = GetModel(conn, trans, oldModel.parent_id); //带事务 class_list = oldParentModel.class_list + model.parent_id + ","; class_layer = oldParentModel.class_layer + 1; } //先提升选中的父节点 DbHelperSQL.ExecuteSql(conn, trans, "update " + databaseprefix + "menu set parent_id=" + oldModel.parent_id + ",class_list='" + class_list + "', class_layer=" + class_layer + " where id=" + model.parent_id); //带事务 UpdateChilds(conn, trans, model.parent_id); //带事务 } //更新子节点 if (model.parent_id > 0) { Model.menu model2 = GetModel(conn, trans, model.parent_id); //带事务 model.class_list = model2.class_list + model.id + ","; model.class_layer = model2.class_layer + 1; } else { model.class_list = "," + model.id + ","; model.class_layer = 1; } StringBuilder strSql = new StringBuilder(); strSql.Append("update [" + databaseprefix + "menu] set "); strSql.Append("title=@title,"); strSql.Append("link_url=@link_url,"); strSql.Append("parent_id=@parent_id,"); strSql.Append("class_list=@class_list,"); strSql.Append("class_layer=@class_layer,"); strSql.Append("open_mode=@open_mode,"); strSql.Append("sort_id=@sort_id,"); strSql.Append("css_txt=@css_txt,"); strSql.Append("img_url=@img_url,"); strSql.Append("is_lock=@is_lock,"); strSql.Append("add_time=@add_time"); strSql.Append(" where id=@id"); SqlParameter[] parameters = { new SqlParameter("@title", SqlDbType.NVarChar, 255), new SqlParameter("@link_url", SqlDbType.NVarChar, 255), new SqlParameter("@parent_id", SqlDbType.Int, 6), new SqlParameter("@class_list", SqlDbType.NVarChar, 500), new SqlParameter("@class_layer", SqlDbType.Int, 6), new SqlParameter("@open_mode", SqlDbType.NVarChar, 20), new SqlParameter("@sort_id", SqlDbType.Int, 6), new SqlParameter("@css_txt", SqlDbType.NVarChar, 50), new SqlParameter("@img_url", SqlDbType.NVarChar, 200), new SqlParameter("@is_lock", SqlDbType.TinyInt, 1), new SqlParameter("@add_time", SqlDbType.DateTime), new SqlParameter("@id", SqlDbType.Int, 4) }; parameters[0].Value = model.title; parameters[1].Value = model.link_url; parameters[2].Value = model.parent_id; parameters[3].Value = model.class_list; parameters[4].Value = model.class_layer; parameters[5].Value = model.open_mode; parameters[6].Value = model.sort_id; parameters[7].Value = model.css_txt; parameters[8].Value = model.img_url; parameters[9].Value = model.is_lock; parameters[10].Value = model.add_time; parameters[11].Value = model.id; DbHelperSQL.ExecuteSql(conn, trans, strSql.ToString(), parameters); //更新子节点 UpdateChilds(conn, trans, model.id); trans.Commit(); } catch (Exception ex) { trans.Rollback(); return(false); } } } return(true); }
/// <summary> /// 增加一条数据 /// </summary> public int Add(Model.menu model) { using (SqlConnection conn = new SqlConnection(DbHelperSQL.connectionString)) { conn.Open(); using (SqlTransaction trans = conn.BeginTransaction()) { try { StringBuilder strSql = new StringBuilder(); strSql.Append("insert into [" + databaseprefix + "menu]("); strSql.Append("title,link_url,parent_id,class_list,class_layer,open_mode,sort_id,css_txt,img_url,is_lock,add_time"); strSql.Append(") values("); strSql.Append("@title,@link_url,@parent_id,@class_list,@class_layer,@open_mode,@sort_id,@css_txt,@img_url,@is_lock,@add_time)"); strSql.Append(";select @@IDENTITY"); SqlParameter[] parameters = { new SqlParameter("@title", SqlDbType.NVarChar, 255), new SqlParameter("@link_url", SqlDbType.NVarChar, 255), new SqlParameter("@parent_id", SqlDbType.Int, 6), new SqlParameter("@class_list", SqlDbType.NVarChar, 500), new SqlParameter("@class_layer", SqlDbType.Int, 6), new SqlParameter("@open_mode", SqlDbType.NVarChar, 20), new SqlParameter("@sort_id", SqlDbType.Int, 6), new SqlParameter("@css_txt", SqlDbType.NVarChar, 50), new SqlParameter("@img_url", SqlDbType.NVarChar, 200), new SqlParameter("@is_lock", SqlDbType.TinyInt, 1), new SqlParameter("@add_time", SqlDbType.DateTime) }; parameters[0].Value = model.title; parameters[1].Value = model.link_url; parameters[2].Value = model.parent_id; parameters[3].Value = model.class_list; parameters[4].Value = model.class_layer; parameters[5].Value = model.open_mode; parameters[6].Value = model.sort_id; parameters[7].Value = model.css_txt; parameters[8].Value = model.img_url; parameters[9].Value = model.is_lock; parameters[10].Value = model.add_time; object obj = DbHelperSQL.GetSingle(conn, trans, strSql.ToString(), parameters); //带事务 model.id = Convert.ToInt32(obj); if (model.parent_id > 0) { Model.menu model2 = GetModel(conn, trans, model.parent_id); //带事务 model.class_list = model2.class_list + model.id + ","; model.class_layer = model2.class_layer + 1; } else { model.class_list = "," + model.id + ","; model.class_layer = 1; } //修改节点列表和深度 DbHelperSQL.ExecuteSql(conn, trans, "update " + databaseprefix + "menu set class_list='" + model.class_list + "', class_layer=" + model.class_layer + " where id=" + model.id); //带事务 trans.Commit(); } catch { trans.Rollback(); return(0); } } } return(model.id); }