Asp.Net商品尺码,颜色,数量入库示例

  jQuery+Asp.Net实现商品尺码,颜色,数量保存到Access数据库示例,如下图所示

Asp.Net商品尺码,颜色,数量入库示例

  数据传输使用JSON数据,对象的序列化和反序列用到了json.net,去这里下载dll:Newtonsoft.Json.Net 4.0下载

  Asp.Net商品尺码,颜色,数量入库示例源代码如下

<%@ Page Language="C#" AutoEventWireup="true" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDb" %>
<%@ Import Namespace="System.Collections.Generic" %>
<script runat="server">
    protected string s = "",driver="";
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.ContentEncoding = Request.ContentEncoding = Encoding.UTF8;
        driver = "provider=microsoft.jet.oledb.4.0;data source=" + Server.MapPath("d.mdb");
        SaveInfo();
        
        DataTable dt = new DataTable();
        OleDbConnection cn=new OleDbConnection(driver);
        cn.Open();
        OleDbDataAdapter da=new OleDbDataAdapter("select cm,ys,num from sizecolornum",cn);
        da.Fill(dt);
        da.Dispose();
        cn.Close();
        s = Newtonsoft.Json.JsonConvert.SerializeObject(dt);
        dt.Dispose();
        this.DataBind();
    }
    /// <summary>
    /// 颜色尺码数量类,反序列化用
    /// </summary>
    public class ColorSize { 
        private string _cm, _ys;
        private int _num;
        public string cm { get { return string.IsNullOrEmpty(_cm) ? "" : _cm.Replace("'", "&apos;"); } set { _cm = value; } }
        public string ys { get { return string.IsNullOrEmpty(_ys) ? "" : _ys.Replace("'", "&apos;"); } set { _ys = value; } }
        public int num { get { return _num; } set { _num = value; } }
    }
    protected void SaveInfo()
    {
        if (Request.Form["op"] == "saveinfo")
        {
            string r = "1", s = Request.Form["s"];
            if (string.IsNullOrEmpty(s)) r = "尺码颜色信息不能为空!";
            else
            {
                List<ColorSize> list = Newtonsoft.Json.JsonConvert.DeserializeObject<List<ColorSize>>(s);
                if (list == null) r = "尺码颜色信息错误";
                else if (list.Count == 0) r = "未添加尺码颜色信息";
                else
                {
                    OleDbConnection cn = new OleDbConnection(driver);
                    cn.Open();
                    OleDbCommand cmd = new OleDbCommand();
                    cmd.CommandText = "update sizecolornum set num=null";//如果是某个产品的,自己扩展数据库添加proid列什么的,过滤加上
                    cmd.Connection = cn;
                    cmd.ExecuteNonQuery(); 
                    foreach (ColorSize o in list)
                    {
                        cmd.CommandText = "update sizecolornum set num="+o.num+" where cm='" + o.cm + "' and ys='" + o.ys + "'";
                        if (cmd.ExecuteNonQuery() == 0)
                        {
                            cmd.CommandText = "insert into sizecolornum(cm,ys,num)values('" + o.cm + "','" + o.ys + "'," + o.num + ")";
                            cmd.ExecuteNonQuery(); 
                        }
                         
                    }
                    //删除num为null的,说明删除了
                    cmd.CommandText = "delete from sizecolornum where num is null";
                    cmd.ExecuteNonQuery(); 
                    cn.Close();
                }
            }
            Response.Clear();
            Response.Write(r);
            Response.End();
        }
    }
</script>
<meta charset="utf-8" />
<script type="text/javascript" src="http://cdn.bootcss.com/jquery/1.7.1/jquery.min.js"></script>
<style>td.center{text-align:center}</style>
<script>
    var data=<%#s%>,cm={},ys={},kv={};
    //过滤出不同的cm,ys
    for(var i=0;i<data.length;i++){
        if(!cm[data[i].cm])cm[data[i].cm]=true;
        if(!ys[data[i].ys])ys[data[i].ys]=true;
        kv[data[i].ys+data[i].cm]=data[i].num;//整理成键值对方便获取值
    }
    function renderTable(id){
        var s='<table border="1" id="'+id+'"><tr><td class="center">   <button type="button">新增尺码</button><br><button type="button">新增颜色</button>   </td>';
        var attr,attr1;
        for( attr in cm)s+='<td>'+attr+'<button type="button">删除尺码</button></td>';
        s+='</tr>';
        for(attr in ys){
            s+='<td>'+attr+'<button type="button">删除颜色</button></td>';
            for(attr1 in cm)s+='<td><input type="text" value="'+(kv[attr+attr1]||'')+'" cm="'+attr1+'" ys="'+attr+'"/></td>';
            s+='</tr>';
        }
        s+='</table><br/><input type="button" value="保存" onclick="saveInfo(\''+id+'\',this)"/>';
        return s;
    }
    function checkExits(id,s,isColor){//检查新增颜色/尺码是否存在
        var ex=false;
        $('#'+id+(isColor?' tr:gt(0) td:first-child':' tr:first td:gt(0)')).each(function(){if(this.innerHTML==s){ex=true;return false}});
        return ex;
    }
    function addRowOrCell(id,s,isColor){//添加尺码或者颜色
        if(isColor)$('#'+id+' tr:last').clone().find('td:first').html(s+'<button type="button">删除颜色</button>').end()
            .find('input').attr('ys',s).val('').end().appendTo('#'+id);
        else{
            $('#'+id+' tr').append(
                function(i){
                    return i==0?'<td>'+s+'<button type="button">删除尺码</button></td>'
                        :'<td><input type="text" ys="'+this.cells[0].firstChild.data+'" cm="'+s+'"/></td>'
                });
        }
    }
    function delRowOrCell(id,el,t){//删除尺码或者颜色
        if(confirm('确认删除'+t+el.previousSibling.data+'?!')){
            if(t=='尺码')$('#'+id+' tr td:nth-child('+(el.parentNode.cellIndex+1)+')').remove();
            else $('#'+id+' tr:eq('+el.parentNode.parentNode.rowIndex+')').remove()
        }
    }
    function buttonClick(){
        var id=$(this).closest('table').attr('id');
        var t=this.innerHTML.replace('新增','').replace('删除','');
        if(this.innerHTML.indexOf('删除')!=-1) delRowOrCell(id,this,t);
        else{
            var s=prompt('请输入'+t);
            if(s){
                if(t=='尺码') s=s.toUpperCase();
                if(checkExits(id,s,t=='颜色')){alert(t+s+'已经存在!');return;}
                addRowOrCell(id,s,t=='颜色')
            }
        }
    }
    function saveInfo (id,btn){
        var arr=[];
        $('#'+id+' input').each(function(){arr.push({cm:this.getAttribute('cm'),ys:this.getAttribute('ys'),num:parseInt(this.value)||0});});
        btn.disabled=true;
        $.ajax({url:location.href,data:{s:JSON.stringify(arr),op:'saveinfo'},type:'POST',complete:function(xhr){
            if(xhr.responseText=='1')alert('保存成功!');
            else alert(xhr.responseText);
            btn.disabled=false;
        }})
    }
    document.write(renderTable('mytb'));
    $('#mytb').on('click','button',buttonClick);
</script>

  点击这里下载Asp.Net商品尺码,颜色,数量入库示例,再次强调要下载Newtonsoft.Json.Net 4.0下载放到网站bin目录下,或者自己修改为其他json序列化,反序列化组件,


原创文章,转载请注明出处:Asp.Net商品尺码,颜色,数量入库示例

评论(0)Web开发网
阅读(681)喜欢(0)不喜欢(0)Asp.Net/C#