<tr><td class&#61;"htd" valign&#61;"top" align&#61;"right"> 销售品牌/厂家&#xff1a; td> <td align&#61;"left"> <asp:HiddenField ID&#61;"hidCarsBrand" runat&#61;"server" /> <table width&#61;"100%"> <asp:Repeater ID&#61;"rptbrand" runat&#61;"server"> <ItemTemplate> <tr tid&#61;&#39;series&#39;> <td style&#61;"border-bottom: 1px solid #888888;"> <input type&#61;"checkbox" id&#61;"s<%#Eval("InputID") %>" tag&#61;"brands" /> <label for&#61;"s<%#Eval("InputID") %>"> <%#Eval("BrandName")%>label> td> tr> <tr tid&#61;&#39;list&#39;> <td style&#61;"border-bottom: 1px solid #000000;"> <uc1:CarByCompany ID&#61;"CarByCompany1" runat&#61;"server" InputID&#61;&#39;<%#Eval("InputID") %>&#39; /> td> tr> ItemTemplate> asp:Repeater> table> <%--
<script src&#61;"../../Scripts/jquery-1.4.1.min.js" type&#61;"text/Javascript">script> <script type&#61;"text/Javascript"> jQuery(document).ready(function () { jQuery("#ctl00_ContentPlaceHolder1_btnSave").click(function () { var result &#61; ""; jQuery("input[type&#61;&#39;checkbox&#39;]").filter("[tag&#61;&#39;brands&#39;]").each(function () { if (jQuery(this).attr("checked") &#61;&#61; true) { result &#61; result &#43; jQuery(this).attr("id") &#43; ","; } }); jQuery("#ctl00_ContentPlaceHolder1_hidCarsBrand").val(result); return true; }); //关联品牌全选(取消全选)操作 jQuery("tr[tid&#61;&#39;series&#39;]").each(function () { var tmp &#61; jQuery(this); tmp.find("input[type&#61;&#39;checkbox&#39;]").filter("[tag&#61;&#39;brands&#39;]").click(function () { if (jQuery(this).attr("checked")) { tmp.next("tr[tid&#61;&#39;list&#39;]").find("table input[type&#61;&#39;checkbox&#39;]").filter("[tag&#61;&#39;brands&#39;]").each(function () { jQuery(this).attr("checked", true); }); } else { tmp.next("tr[tid&#61;&#39;list&#39;]").find("table input[type&#61;&#39;checkbox&#39;]").filter("[tag&#61;&#39;brands&#39;]").each(function () { jQuery(this).attr("checked", false); }); } }); }); //关联厂家操作(若一个选中则品牌必须选中,如所有都不选。则厂家也取消) jQuery("tr[tid&#61;&#39;list&#39;]").each(function () { var tmp &#61; jQuery(this); var flag &#61; false; tmp.find("table input[type&#61;&#39;checkbox&#39;]").filter("[tag&#61;&#39;brands&#39;]").click(function () { //判断厂家是否有选中的项 if (jQuery(this).attr("checked")) { tmp.prev("tr[tid&#61;&#39;series&#39;]").find("input[type&#61;&#39;checkbox&#39;]").filter("[tag&#61;&#39;brands&#39;]").attr("checked", true); } else { flag &#61; false; } tmp.find("table input[type&#61;&#39;checkbox&#39;]").filter("[tag&#61;&#39;brands&#39;]").each(function () { if (jQuery(this).attr("checked")) { flag &#61; true; } }); if (!flag) { tmp.prev("tr[tid&#61;&#39;series&#39;]").find("input[type&#61;&#39;checkbox&#39;]").filter("[tag&#61;&#39;brands&#39;]").attr("checked", false); } }); }); }); script>
CarByCompany.ascx
<table><tr> <asp:Repeater ID&#61;"rptCompany" runat&#61;"server"> <ItemTemplate> <td> <input type&#61;"checkbox" id&#61;"c<%#Eval("InputID") %>" tag&#61;"brands" /> <label for&#61;"c<%#Eval("InputID") %>"> <%#Eval("CompanyName")%> label> td> ItemTemplate> asp:Repeater> tr> table>
CarByCompany.ascx.cs文件代码&#xff1a;
public object InputID{set;get;}protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { int id &#61; Convert.ToInt32(InputID); using (dcauto2012Entities db &#61; new dcauto2012Entities()) { IList<Company> list &#61; db.Company.OrderByDescending(o &#61;> o.CompanyID) .Where(o &#61;> o.BrandID &#61;&#61; id) .Where(o &#61;> o.ISDongChang &#61;&#61; 0) .Where(o &#61;> o.InputID !&#61; 31) .ToList(); rptCompany.DataSource &#61; list; rptCompany.DataBind(); } } }
add.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System;
using System.Collections.Generic;
using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using dcauto.Utils; using EFDAL; using System.Data; using System.Data.Objects; using Webdiyer.WebControls.Mvc; using System.IO; public partial class manage_car_fourS_add : System.Web.UI.Page { int currentid; protected void Page_Load(object sender, EventArgs e) { ((manage_car_carfoursinfomaster)this.Master).txtTitle &#61; "4S店添加"; if (!this.IsPostBack) { using (dcauto2012Entities db &#61; new dcauto2012Entities()) { WebUtil.CtrlToList(rptbrand, db.Brand.OrderBy(o &#61;> o.ISDongChang).OrderBy(o &#61;> o.Orders).OrderBy(o &#61;> o.BrandID).Where(o &#61;> o.ISDongChang &#61;&#61; 0).Where(s &#61;> s.InputID !&#61; 5).ToList()); } } } protected void btnSave_Click(object sender, ImageClickEventArgs e) { string[] borf &#61; hidCarsBrand.Value.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries); int flag &#61; 0; if (borf.Length > 0) { flag &#61; 1; } if (flag &#61;&#61; 0) { jsHint.Alert("请选择至少一个品牌.若没有则先添加品牌后进行添加4S店"); } else { FourS model &#61; new FourS(); model.Name &#61; txtpname.Text; model.Adress &#61; txtadress.Text; model.Content &#61; txtcontent.Text; string picstr &#61; ""; if (f1.HasFile) { string _imgpath &#61; globalVariables.NewFileName &#43; Path.GetExtension(f1.FileName).ToLower(); f1.PostedFile.SaveAs(globalVariables.CarsPic &#43; _imgpath); model.Pic &#61; _imgpath; } string picstr1 &#61; ""; if (f2.HasFile) { picstr1 &#61; globalVariables.NewFileName &#43; Path.GetExtension(f2.FileName).ToLower(); f2.PostedFile.SaveAs(globalVariables.CarsPic &#43; picstr1); model.Map &#61; picstr1; } string picstr3 &#61; ""; if (f3.HasFile) { picstr3 &#61; globalVariables.NewFileName &#43; Path.GetExtension(f3.FileName).ToLower(); f3.PostedFile.SaveAs(globalVariables.CarsPic &#43; picstr3); model.Pic2 &#61; picstr3; } model.Url &#61; TextBox1.Text; model.Zip &#61; TextBox2.Text; model.Selltel1 &#61; TextBox3.Text; model.Selltel2 &#61; TextBox4.Text; model.Servtel1 &#61; TextBox5.Text; model.Servtel2 &#61; TextBox6.Text; model.Fax &#61; TextBox7.Text; model.Email &#61; TextBox8.Text; model.Companyurl &#61; txtcompanyintro.Text; model.Mapurl &#61; txtmapurl.Text; int userid &#61; 1; if (Session["UserId"] &#43; "" !&#61; "") userid &#61; int.Parse(Session["UserId"] &#43; ""); model.Datetime &#61; DateTime.Now; model.AddUser &#61; Session["loginname"] &#43; ""; model.AddUserid &#61; userid; model.ModUser &#61; Session["loginname"] &#43; ""; model.ModUserid &#61; userid; try { using (dcauto2012Entities db &#61; new dcauto2012Entities()) { db.FourS.AddObject(model); db.SaveChanges(); var brandid &#61; 0; foreach (string item in borf) { string witch &#61; item.Substring(0, 1); string id &#61; item.Replace("c", "").Replace("s", ""); EFDAL.FourSunion fs &#61; new FourSunion(); fs.FourSid &#61; model.FourSid; if (witch &#61;&#61; "s") { brandid &#61; int.Parse(id); fs.Brandid &#61; brandid; } if (witch &#61;&#61; "c") { fs.Brandid &#61; brandid; fs.Companyid &#61; int.Parse(id); db.FourSunion.AddObject(fs); db.SaveChanges(); } } } jsHint.Confirm("添加成功&#xff0c;是否继续添加&#xff1f;", "fourS.asp", "fourS_add.aspx"); } catch (Exception) { jsHint.toUrl("添加失败!", "fourS.aspx"); } } } }
编辑修改&#xff1a;
<script src&#61;"../../Scripts/jquery-1.4.1.min.js" type&#61;"text/Javascript">script> <script type&#61;"text/Javascript"> $(document).ready(function () { var ids &#61; $("#ctl00_ContentPlaceHolder1_hidCarsBrand").val(); try { if (ids !&#61; null) { var items &#61; ids.split(","); } for (var i &#61; 0; i < items.length; i&#43;&#43;) { $("#" &#43; items[i]).attr("checked", true) } } catch (E) { } $("#ctl00_ContentPlaceHolder1_hidCarsBrand").val(""); $("#ctl00_ContentPlaceHolder1_btnSave").click(function () { var result &#61; ""; $("input[type&#61;&#39;checkbox&#39;]").filter("[tag&#61;&#39;brands&#39;]").each(function () { if ($(this).attr("checked") &#61;&#61; true) { result &#61; result &#43; $(this).attr("id") &#43; ","; } }); $("#ctl00_ContentPlaceHolder1_hidCarsBrand").val(result); return true; }); //关联车型全选(取消全选)操作 $("tr[tid&#61;&#39;series&#39;]").each(function () { var tmp &#61; $(this); tmp.find("input[type&#61;&#39;checkbox&#39;]").filter("[tag&#61;&#39;brands&#39;]").click(function () { if ($(this).attr("checked")) { tmp.next("tr[tid&#61;&#39;list&#39;]").find("table input[type&#61;&#39;checkbox&#39;]").filter("[tag&#61;&#39;brands&#39;]").each(function () { $(this).attr("checked", true); }); } else { tmp.next("tr[tid&#61;&#39;list&#39;]").find("table input[type&#61;&#39;checkbox&#39;]").filter("[tag&#61;&#39;brands&#39;]").each(function () { $(this).attr("checked", false); }); } }); }); //关联车型二级操作(若一个选中则一级必须选中,如所有都不选。则一级也取消) $("tr[tid&#61;&#39;list&#39;]").each(function () { var tmp &#61; $(this); var flag &#61; false; tmp.find("table input[type&#61;&#39;checkbox&#39;]").filter("[tag&#61;&#39;brands&#39;]").click(function () { //判断二级是否有选中的项 if ($(this).attr("checked")) { tmp.prev("tr[tid&#61;&#39;series&#39;]").find("input[type&#61;&#39;checkbox&#39;]").filter("[tag&#61;&#39;brands&#39;]").attr("checked", true); } else { flag &#61; false; } tmp.find("table input[type&#61;&#39;checkbox&#39;]").filter("[tag&#61;&#39;brands&#39;]").each(function () { if ($(this).attr("checked")) { flag &#61; true; } }); if (!flag) { tmp.prev("tr[tid&#61;&#39;series&#39;]").find("input[type&#61;&#39;checkbox&#39;]").filter("[tag&#61;&#39;brands&#39;]").attr("checked", false); } }); }); }); script>
后台保存代码&#xff1a;
using System;
using System.Data;
using System.Configuration;
using System;
using System.Collections.Generic;
using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using dcauto.Utils; using EFDAL; using System.Data; using System.Data.Objects; using Webdiyer.WebControls.Mvc; using System.IO; public partial class manage_car_fourS_edit : BasePage { int currentid; protected void Page_Load(object sender, EventArgs e) { ((manage_car_carfoursinfomaster)this.Master).txtTitle &#61; "4S店修改"; if (!this.IsPostBack) { using (dcauto2012Entities db &#61; new dcauto2012Entities()) { WebUtil.CtrlToList(rptbrand, db.Brand.OrderBy(o &#61;> o.ISDongChang).OrderBy(o &#61;> o.Orders).OrderBy(o &#61;> o.BrandID).Where(o &#61;> o.ISDongChang &#61;&#61; 0).Where(s &#61;> s.InputID !&#61; 5).ToList()); } if (!string.IsNullOrEmpty(Request["Id"])) InitCtrl(Convert.ToInt32(int.Parse(Request["Id"]))); } } private void InitCtrl(int id) { using (dcauto2012Entities db &#61; new dcauto2012Entities()) { currentid &#61; id; FourS model &#61; db.FourS.Where(s &#61;> s.FourSid &#61;&#61; id).FirstOrDefault(); this.txtpname.Text &#61; model.Name; this.txtadress.Text &#61; model.Adress; this.txtcontent.Text &#61; model.Content; TextBox1.Text &#61; model.Url; TextBox2.Text &#61; model.Zip; TextBox3.Text &#61; model.Selltel1; TextBox4.Text &#61; model.Selltel2; TextBox5.Text &#61; model.Servtel1; TextBox6.Text &#61; model.Servtel2; TextBox7.Text &#61; model.Fax; TextBox8.Text &#61; model.Email; txtcompanyintro.Text &#61; model.Companyurl; txtmapurl.Text &#61; model.Mapurl; if (!string.IsNullOrEmpty(model.Pic)) { this.f1error.Text &#61; "globalVariables.CarsPicUrl &#43; model.Pic &#43; " width&#61;100 height&#61;100>"; } if (!string.IsNullOrEmpty(model.Map)) { this.f2error.Text &#61; "globalVariables.CarsPicUrl &#43; model.Map &#43; " width&#61;100 height&#61;100>"; } if (!string.IsNullOrEmpty(model.Pic2)) { this.f3error.Text &#61; "globalVariables.CarsPicUrl &#43; model.Pic2 &#43; " width&#61;100 height&#61;100>"; } IList<FourSunion> list &#61; db.FourSunion.Where(s &#61;> s.FourSid &#61;&#61; id).OrderByDescending(s &#61;> s.FourSunionid).ToList(); System.Text.StringBuilder result &#61; new System.Text.StringBuilder(); foreach (FourSunion item in list) { if (item.Companyid !&#61; null && item.Brandid !&#61; null) { result.Append("s"); result.Append(item.Brandid); result.Append(","); } if (item.Companyid !&#61; null && item.Brandid !&#61; null) { result.Append("c"); result.Append(item.Companyid); result.Append(","); } } hidCarsBrand.Value &#61; result.ToString(); } } public void db1_Click(object sender, EventArgs e) { using (dcauto2012Entities db &#61; new dcauto2012Entities()) { int foursid &#61; Convert.ToInt32(Request.Params["Id"]); FourS model &#61; db.FourS.Where(s &#61;> s.FourSid &#61;&#61; foursid).FirstOrDefault(); model.Pic &#61; ""; db.SaveChanges(); f1error.Text &#61; ""; } } public void db2_Click(object sender, EventArgs e) { using (dcauto2012Entities db &#61; new dcauto2012Entities()) { int foursid &#61; Convert.ToInt32(Request.Params["Id"]); FourS model &#61; db.FourS.Where(s &#61;> s.FourSid &#61;&#61; foursid).FirstOrDefault(); model.Map &#61; ""; db.SaveChanges(); f2error.Text &#61; ""; } } public void db3_Click(object sender, EventArgs e) { using (dcauto2012Entities db &#61; new dcauto2012Entities()) { int foursid &#61; Convert.ToInt32(Request.Params["Id"]); FourS model &#61; db.FourS.Where(s &#61;> s.FourSid &#61;&#61; foursid).FirstOrDefault(); model.Pic2 &#61; ""; db.SaveChanges(); f3error.Text &#61; ""; } } protected void btnSave_Click(object sender, ImageClickEventArgs e) { using (dcauto2012Entities db &#61; new dcauto2012Entities()) { int foursid &#61; Convert.ToInt32(Request.Params["Id"]); FourS model &#61; db.FourS.Where(s &#61;> s.FourSid &#61;&#61; foursid).FirstOrDefault(); model.Name &#61; txtpname.Text; model.Adress &#61; txtadress.Text; model.Content &#61; txtcontent.Text; string picstr &#61; ""; if (f1.HasFile) { string _imgpath &#61; globalVariables.NewFileName &#43; Path.GetExtension(f1.FileName).ToLower(); f1.PostedFile.SaveAs(globalVariables.CarsPic &#43; _imgpath); model.Pic &#61; _imgpath; } string picstr1 &#61; ""; if (f2.HasFile) { picstr1 &#61; globalVariables.NewFileName &#43; Path.GetExtension(f2.FileName).ToLower(); f2.PostedFile.SaveAs(globalVariables.CarsPic &#43; picstr1); model.Map &#61; picstr1; } string picstr3 &#61; ""; if (f3.HasFile) { picstr3 &#61; globalVariables.NewFileName &#43; Path.GetExtension(f3.FileName).ToLower(); f3.PostedFile.SaveAs(globalVariables.CarsPic &#43; picstr3); model.Pic2 &#61; picstr3; } model.Url &#61; TextBox1.Text; model.Zip &#61; TextBox2.Text; model.Selltel1 &#61; TextBox3.Text; model.Selltel2 &#61; TextBox4.Text; model.Servtel1 &#61; TextBox5.Text; model.Servtel2 &#61; TextBox6.Text; model.Fax &#61; TextBox7.Text; model.Email &#61; TextBox8.Text; model.Companyurl &#61; txtcompanyintro.Text; model.Mapurl &#61; txtmapurl.Text; int userid &#61; 1; if (Session["UserId"] &#43; "" !&#61; "") userid &#61; int.Parse(Session["UserId"] &#43; ""); model.Datetime &#61; DateTime.Now; model.AddUser &#61; Session["loginname"] &#43; ""; model.AddUserid &#61; userid; model.ModUser &#61; Session["loginname"] &#43; ""; model.ModUserid &#61; userid; try { db.SaveChanges(); string[] borf &#61; hidCarsBrand.Value.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries); IList<FourSunion> list &#61; db.FourSunion.Where(s &#61;> s.FourSid &#61;&#61; model.FourSid).OrderByDescending(s &#61;> s.FourSunionid).ToList(); for (int i &#61; 0; i < list.Count; i&#43;&#43;) { db.FourSunion.DeleteObject(list[i]); } db.SaveChanges(); var brandid &#61; 0; foreach (string item in borf) { string witch &#61; item.Substring(0, 1); string id &#61; item.Replace("c", "").Replace("s", ""); EFDAL.FourSunion fs &#61; new FourSunion(); fs.FourSid &#61; model.FourSid; if (witch &#61;&#61; "s") { brandid &#61; int.Parse(id); fs.Brandid &#61; brandid; } if (witch &#61;&#61; "c") { fs.Brandid &#61; brandid; fs.Companyid &#61; int.Parse(id); db.FourSunion.AddObject(fs); db.SaveChanges(); } } jsHint.toUrl("修改成功!", "fourS.aspx?page&#61;" &#43; Request.QueryString["page"]); } catch (Exception) { jsHint.toUrl("修改失败!", "fourS.aspx?page&#61;" &#43; Request.QueryString["page"]); } } } }