后台全选功能以及数据的提交方法

  <tr>
                <td class="htd" valign="top" align="right">
                    销售品牌/厂家:
                </td>
                <td align="left">
                <asp:HiddenField ID="hidCarsBrand" runat="server" />
                    <table width="100%">   
                        <asp:Repeater ID="rptbrand" runat="server">
                            <ItemTemplate>
                                <tr tid='series'>
                                    <td style="border-bottom1px solid #888888;">
                                        <input type="checkbox" id="s<%#Eval("InputID") %>tag="brands" />
                                        <label for="s<%#Eval("InputID") %>">
                                            <%#Eval("BrandName")%></label>
                                    </td>
                                </tr>
                                <tr tid='list'>
                                    <td style="border-bottom1px solid #000000;">
                                        <uc1:CarByCompany ID="CarByCompany1" runat="server" InputID='<%#Eval("InputID") %>/>
                                    </td>
                                </tr>
                            </ItemTemplate>
                        </asp:Repeater>
                    </table>
                    <%--<asp:DropDownList ID="drpbrand" runat="server" AutoPostBack="true" OnSelectedIndexChanged="b1_SelectedIndexChanged">
                    </asp:DropDownList> &nbsp
                   
                    <asp:DropDownList ID="drpcompany" runat="server">
                    </asp:DropDownList>
                    <asp:CheckBoxList ID="clist" RepeatDirection="Horizontal" RepeatColumns="5" runat="server">
                    </asp:CheckBoxList>--%>
                </td>
            </tr>

 <script src="../../Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
    <script type="text/javascript">
        jQuery(document).ready(function () {
            jQuery("#ctl00_ContentPlaceHolder1_btnSave").click(function () {
                var result = "";
                jQuery("input[type='checkbox']").filter("[tag='brands']").each(function () {
                    if (jQuery(this).attr("checked") == true) {
                        result = result + jQuery(this).attr("id") + ",";
                    }
                });
                jQuery("#ctl00_ContentPlaceHolder1_hidCarsBrand").val(result);
                return true;
            });
            //关联品牌全选(取消全选)操作
            jQuery("tr[tid='series']").each(function () {
                var tmp = jQuery(this);
                tmp.find("input[type='checkbox']").filter("[tag='brands']").click(function () {
                    if (jQuery(this).attr("checked")) {
                        tmp.next("tr[tid='list']").find("table input[type='checkbox']").filter("[tag='brands']").each(function () {
                            jQuery(this).attr("checked", true);
                        });
                    } else {
                        tmp.next("tr[tid='list']").find("table input[type='checkbox']").filter("[tag='brands']").each(function () {
                            jQuery(this).attr("checked", false);
                        });
                    }
                });
            });
            //关联厂家操作(若一个选中则品牌必须选中,如所有都不选。则厂家也取消)
            jQuery("tr[tid='list']").each(function () {
                var tmp = jQuery(this);
                var flag = false;
                tmp.find("table input[type='checkbox']").filter("[tag='brands']").click(function () {
                    //判断厂家是否有选中的项
                    if (jQuery(this).attr("checked")) {
                        tmp.prev("tr[tid='series']").find("input[type='checkbox']").filter("[tag='brands']").attr("checked", true);
 
                    } else {
                        flag = false;
                    }
                    tmp.find("table input[type='checkbox']").filter("[tag='brands']").each(function () {
                        if (jQuery(this).attr("checked")) {
                            flag = true;
                        }
                    });
                    if (!flag) {
                        tmp.prev("tr[tid='series']").find("input[type='checkbox']").filter("[tag='brands']").attr("checked", false);
                    }
                });
            });
        });
    </script>

CarByCompany.ascx

<table>
    <tr>
        <asp:Repeater ID="rptCompany" runat="server">
            <ItemTemplate>
                <td>
                    <input type="checkbox" id="c<%#Eval("InputID") %>"  tag="brands" />
                    <label for="c<%#Eval("InputID") %>">
                        <%#Eval("CompanyName")%>
                    </label>
                </td>
            </ItemTemplate>
        </asp:Repeater>
    </tr>
</table>

CarByCompany.ascx.cs文件代码:
 public object InputID
    {
        set;
        get;
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            int id = Convert.ToInt32(InputID);
            using (dcauto2012Entities db = new dcauto2012Entities())
            {
                IList<Company> list = db.Company.OrderByDescending(o => o.CompanyID)
                .Where(o => o.BrandID == id)
                .Where(o => o.ISDongChang == 0)
                .Where(o => o.InputID != 31)
                .ToList();
                rptCompany.DataSource = 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 = "4S店添加";
        if (!this.IsPostBack)   
        {
            using (dcauto2012Entities db = new dcauto2012Entities())
            {
                WebUtil.CtrlToList(rptbrand, db.Brand.OrderBy(o => o.ISDongChang).OrderBy(o => o.Orders).OrderBy(o => o.BrandID).Where(o => o.ISDongChang == 0).Where(s => s.InputID != 5).ToList());
            }
        }
    }
 
    protected void btnSave_Click(object sender, ImageClickEventArgs e)
    {
 
        string[] borf = hidCarsBrand.Value.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
        int flag = 0;
        if (borf.Length > 0)
        {
            flag = 1;
        }
        if (flag == 0)
        {
            jsHint.Alert("请选择至少一个品牌.若没有则先添加品牌后进行添加4S店");
        }
        else
        {
            FourS model = new FourS();
            model.Name = txtpname.Text;
            model.Adress = txtadress.Text;
            model.Content = txtcontent.Text;
            string picstr = "";
            if (f1.HasFile)
            {
                string _imgpath = globalVariables.NewFileName + Path.GetExtension(f1.FileName).ToLower();
                f1.PostedFile.SaveAs(globalVariables.CarsPic + _imgpath);
                model.Pic = _imgpath;
            }
            string picstr1 = "";
            if (f2.HasFile)
            {
                picstr1 = globalVariables.NewFileName + Path.GetExtension(f2.FileName).ToLower();
                f2.PostedFile.SaveAs(globalVariables.CarsPic + picstr1);
                model.Map = picstr1;
            }
 
            string picstr3 = "";
            if (f3.HasFile)
            {
                picstr3 = globalVariables.NewFileName + Path.GetExtension(f3.FileName).ToLower();
                f3.PostedFile.SaveAs(globalVariables.CarsPic + picstr3);
                model.Pic2 = picstr3;
            }
            model.Url = TextBox1.Text;
            model.Zip = TextBox2.Text;
            model.Selltel1 = TextBox3.Text;
            model.Selltel2 = TextBox4.Text;
            model.Servtel1 = TextBox5.Text;
            model.Servtel2 = TextBox6.Text;
            model.Fax = TextBox7.Text;
            model.Email = TextBox8.Text;
            model.Companyurl = txtcompanyintro.Text;
            model.Mapurl = txtmapurl.Text;
            int userid = 1;
            if (Session["UserId"] + "" != "")
                userid = int.Parse(Session["UserId"] + "");
            model.Datetime = DateTime.Now;
            model.AddUser = Session["loginname"] + "";
            model.AddUserid = userid;
            model.ModUser = Session["loginname"] + "";
            model.ModUserid = userid;
            try
            {
                using (dcauto2012Entities db = new dcauto2012Entities())
                {
                    db.FourS.AddObject(model);
                    db.SaveChanges();
                    var brandid = 0;
                    foreach (string item in borf)
                    {
                        string witch = item.Substring(0, 1);
                        string id = item.Replace("c", "").Replace("s", "");
                        EFDAL.FourSunion fs = new FourSunion();
                        fs.FourSid = model.FourSid;
                        if (witch == "s")
                        {
                            brandid = int.Parse(id);
                            fs.Brandid = brandid;
                           
                        }
                        if (witch == "c")
                        {
                            fs.Brandid = brandid;
                            fs.Companyid = int.Parse(id);
                            db.FourSunion.AddObject(fs);
                            db.SaveChanges();
                        }
                       
                       
                    }
                }
                jsHint.Confirm("添加成功,是否继续添加?", "fourS.asp", "fourS_add.aspx");
            }
            catch (Exception)
            {
                jsHint.toUrl("添加失败!", "fourS.aspx");
            }
 
        }
 
    }
}
编辑修改:

<script src="../../Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(document).ready(function () {
 
            var ids = $("#ctl00_ContentPlaceHolder1_hidCarsBrand").val();
            try {
                if (ids != null) {
                    var items = ids.split(",");
                }
 
                for (var i = 0; i < items.length; i++) {
                    $("#" + items[i]).attr("checked", true)
                }
            }
            catch (E) { }
            $("#ctl00_ContentPlaceHolder1_hidCarsBrand").val("");
            $("#ctl00_ContentPlaceHolder1_btnSave").click(function () {
                var result = "";
                $("input[type='checkbox']").filter("[tag='brands']").each(function () {
                    if ($(this).attr("checked") == true) {
                        result = result + $(this).attr("id") + ",";
 
                    }
                });
 
                $("#ctl00_ContentPlaceHolder1_hidCarsBrand").val(result);
                return true;
            });
            //关联车型全选(取消全选)操作
            $("tr[tid='series']").each(function () {
                var tmp = $(this);
                tmp.find("input[type='checkbox']").filter("[tag='brands']").click(function () {
                    if ($(this).attr("checked")) {
 
                        tmp.next("tr[tid='list']").find("table input[type='checkbox']").filter("[tag='brands']").each(function () {
                            $(this).attr("checked", true);
                        });
                    } else {
                        tmp.next("tr[tid='list']").find("table input[type='checkbox']").filter("[tag='brands']").each(function () {
                            $(this).attr("checked", false);
                        });
                    }
                });
            });
            //关联车型二级操作(若一个选中则一级必须选中,如所有都不选。则一级也取消)
            $("tr[tid='list']").each(function () {
                var tmp = $(this);
                var flag = false;
                tmp.find("table input[type='checkbox']").filter("[tag='brands']").click(function () {
                    //判断二级是否有选中的项
                    if ($(this).attr("checked")) {
                        tmp.prev("tr[tid='series']").find("input[type='checkbox']").filter("[tag='brands']").attr("checked", true);
 
                    } else {
                        flag = false;
                    }
                    tmp.find("table input[type='checkbox']").filter("[tag='brands']").each(function () {
                        if ($(this).attr("checked")) {
                            flag = true;
                        }
                    });
                    if (!flag) {
                        tmp.prev("tr[tid='series']").find("input[type='checkbox']").filter("[tag='brands']").attr("checked", false);
                    }
                });
            });
        });
    </script>


后台保存代码:

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 = "4S店修改";
        if (!this.IsPostBack)
        {
            using (dcauto2012Entities db = new dcauto2012Entities())
            {
                WebUtil.CtrlToList(rptbrand, db.Brand.OrderBy(o => o.ISDongChang).OrderBy(o => o.Orders).OrderBy(o => o.BrandID).Where(o => o.ISDongChang == 0).Where(s => s.InputID != 5).ToList());
            }
            if (!string.IsNullOrEmpty(Request["Id"]))
                InitCtrl(Convert.ToInt32(int.Parse(Request["Id"])));
        }
    }
    private void InitCtrl(int id)
    {
        using (dcauto2012Entities db = new dcauto2012Entities())
        {
            currentid = id;
            FourS model = db.FourS.Where(s => s.FourSid == id).FirstOrDefault();
            this.txtpname.Text = model.Name;
            this.txtadress.Text = model.Adress;
            this.txtcontent.Text = model.Content;
            TextBox1.Text = model.Url;
            TextBox2.Text = model.Zip;
            TextBox3.Text = model.Selltel1;
            TextBox4.Text = model.Selltel2;
            TextBox5.Text = model.Servtel1;
            TextBox6.Text = model.Servtel2;
            TextBox7.Text = model.Fax;
            TextBox8.Text = model.Email;
            txtcompanyintro.Text = model.Companyurl;
            txtmapurl.Text = model.Mapurl;
 
            if (!string.IsNullOrEmpty(model.Pic))
            {
                this.f1error.Text = "<img src=" + globalVariables.CarsPicUrl + model.Pic + " width=100 height=100>";
            }
            if (!string.IsNullOrEmpty(model.Map))
            {
                this.f2error.Text = "<img src=" + globalVariables.CarsPicUrl + model.Map + " width=100 height=100>";
            }
            if (!string.IsNullOrEmpty(model.Pic2))
            {
                this.f3error.Text = "<img src=" + globalVariables.CarsPicUrl + model.Pic2 + " width=100 height=100>";
            }
            IList<FourSunion> list = db.FourSunion.Where(s => s.FourSid == id).OrderByDescending(s => s.FourSunionid).ToList();
            System.Text.StringBuilder result = new System.Text.StringBuilder();
            foreach (FourSunion item in list)
            {
                if (item.Companyid != null && item.Brandid != null)
                {
                    result.Append("s");
                    result.Append(item.Brandid);
                    result.Append(",");
                }
                if (item.Companyid != null && item.Brandid != null)
                {
                    result.Append("c");
                    result.Append(item.Companyid);
                    result.Append(",");
                }
            }
            hidCarsBrand.Value = result.ToString();
        }
    }
 
    public void db1_Click(object sender, EventArgs e)
    {
        using (dcauto2012Entities db = new dcauto2012Entities())
        {
            int foursid = Convert.ToInt32(Request.Params["Id"]);
            FourS model = db.FourS.Where(s => s.FourSid == foursid).FirstOrDefault();
            model.Pic = "";
            db.SaveChanges();
            f1error.Text = "";
        }
    }
 
    public void db2_Click(object sender, EventArgs e)
    {
        using (dcauto2012Entities db = new dcauto2012Entities())
        {
            int foursid = Convert.ToInt32(Request.Params["Id"]);
            FourS model = db.FourS.Where(s => s.FourSid == foursid).FirstOrDefault();
            model.Map = "";
            db.SaveChanges();
            f2error.Text = "";
        }
    }
 
    public void db3_Click(object sender, EventArgs e)
    {
        using (dcauto2012Entities db = new dcauto2012Entities())
        {
            int foursid = Convert.ToInt32(Request.Params["Id"]);
            FourS model = db.FourS.Where(s => s.FourSid == foursid).FirstOrDefault();
            model.Pic2 = "";
            db.SaveChanges();
            f3error.Text = "";
        }
    }
  
  
    protected void btnSave_Click(object sender, ImageClickEventArgs e)
    {
        using (dcauto2012Entities db = new dcauto2012Entities())
        {
            int foursid = Convert.ToInt32(Request.Params["Id"]);
            FourS model = db.FourS.Where(s => s.FourSid == foursid).FirstOrDefault();
            model.Name = txtpname.Text;
            model.Adress = txtadress.Text;
            model.Content = txtcontent.Text;
            string picstr = "";
            if (f1.HasFile)
            {
                string _imgpath = globalVariables.NewFileName + Path.GetExtension(f1.FileName).ToLower();
                f1.PostedFile.SaveAs(globalVariables.CarsPic + _imgpath);
                model.Pic = _imgpath;
            }
            string picstr1 = "";
            if (f2.HasFile)
            {
                picstr1 = globalVariables.NewFileName + Path.GetExtension(f2.FileName).ToLower();
                f2.PostedFile.SaveAs(globalVariables.CarsPic + picstr1);
                model.Map = picstr1;
            }
 
            string picstr3 = "";
            if (f3.HasFile)
            {
                picstr3 = globalVariables.NewFileName + Path.GetExtension(f3.FileName).ToLower();
                f3.PostedFile.SaveAs(globalVariables.CarsPic + picstr3);
                model.Pic2 = picstr3;
            }
            model.Url = TextBox1.Text;
            model.Zip = TextBox2.Text;
            model.Selltel1 = TextBox3.Text;
            model.Selltel2 = TextBox4.Text;
            model.Servtel1 = TextBox5.Text;
            model.Servtel2 = TextBox6.Text;
            model.Fax = TextBox7.Text;
            model.Email = TextBox8.Text;
            model.Companyurl = txtcompanyintro.Text;
            model.Mapurl = txtmapurl.Text;
            int userid = 1;
            if (Session["UserId"] + "" != "")
                userid = int.Parse(Session["UserId"] + "");
            model.Datetime = DateTime.Now;
            model.AddUser = Session["loginname"] + "";
            model.AddUserid = userid;
            model.ModUser = Session["loginname"] + "";
            model.ModUserid = userid;
            try
            {
 
                db.SaveChanges();
                string[] borf = hidCarsBrand.Value.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
                IList<FourSunion> list = db.FourSunion.Where(s => s.FourSid == model.FourSid).OrderByDescending(s => s.FourSunionid).ToList();
                for (int i = 0; i < list.Count; i++)
                {
                    db.FourSunion.DeleteObject(list[i]);
                }
               
                db.SaveChanges();
                var brandid = 0;
                foreach (string item in borf)
                {
                    string witch = item.Substring(0, 1);
                    string id = item.Replace("c", "").Replace("s", "");
                    EFDAL.FourSunion fs = new FourSunion();
                    fs.FourSid = model.FourSid;
                    if (witch == "s")
                    {
                        brandid = int.Parse(id);
                        fs.Brandid = brandid;
 
                    }
                    if (witch == "c")
                    {
                        fs.Brandid = brandid;
                        fs.Companyid = int.Parse(id);
                        db.FourSunion.AddObject(fs);
                        db.SaveChanges();
                    }
 
 
                }
 
              
 
                jsHint.toUrl("修改成功!", "fourS.aspx?page=" + Request.QueryString["page"]);
            }
 
            catch (Exception)
            {
                jsHint.toUrl("修改失败!", "fourS.aspx?page=" + Request.QueryString["page"]);
            }
        }
 
    }
 
 
}
 





posted @ 2015-07-10 10:13  Pavilion  阅读(341)  评论(0编辑  收藏  举报