AJAX无刷新添加一级分类和二级分类

var http_request=false;
     function createrequest()
     {
      http_request=false;
      if(window.XmlHttpRequest)
      {
        http_request=new XmlHttpRequest();
        if(http_request.overrideMimeType)
        {
         http_request.overrideMimeType("text/xml");
        }
      }
      else
      {
        try
        {
         http_request=new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch(e)
        {
          try
          {
           http_request=new ActiveXObject("Microsoft.XMLHTTP");
          }
          catch(e){}
        }
      }
     }

function disPlay(str)
      {
         if(str==1)
         {
           document.getElementById("btn_Add").value="添加一级";
         }
         if(str==2)
         {
           document.getElementById("btn_Add").value="添加二级";
         }
        document.getElementById("txtSort").value="";
        document.getElementById("layer").style.display='block';
      }
      function OutdisPlay()
      {
        document.getElementById("layer").style.display='none';
      }


function one()
      {
       if(http_request.readyState==4)
       {
         if(http_request.status==200)
         {
          var sort=document.getElementById("txtSort").value;
           var drplist=document.getElementById('');
             var id=http_request.responseText;
           var optNew=document.createElement('option');
           optNew.text=sort;
           optNew.value=id;
           drplist.add(optNew);
           drplist.selectedValue=id;
             drplist.selectedIndex=drplist.options.length-1;
         }
       }
         
      }
      function two()
      {
        if(http_request.readyState==4)
        {
          if(http_request.status==200)
          {
           var sort=document.getElementById("txtSort").value;
            var drplist=document.getElementById('');
              var sid=http_request.responseText;
            var optNew=document.createElement('option');
            optNew.text=sort;
            optNew.value=sid;
            drplist.add(optNew);
            drplist.selectedValue=sid;
             drplist.selectedIndex=drplist.options.length-1;
          }
        }
         
      }


      function submitOnerequest(str)
   {
      
   if(document.getElementById("btn_Add").value=="添加一级")
    {
       createrequest()
     var sort=document.getElementById("txtSort").value;
    var url='UseAjax.aspx?sort='+escape(sort);
//     send_request("GET",url,null,"text",callback);
    http_request.open("GET",url,true);
    http_request.onreadystatechange=one;
    http_request.send(null);
//  var sort=document.getElementById("txtSort").value;
        
  
    }
   if(document.getElementById("btn_Add").value=="添加二级")
    {
       createrequest();
       
      var sort=document.getElementById("txtSort").value;
       var drpBsort=document.getElementById('');
       if(drpBsort.selectedValue=="")
       {
        window.alert("请选择一级分类");
        OutdisPlay()
       }
     var id=drpBsort.selectedValue;
  
    var url='UseAjax.aspx?sort='+escape(sort)+'&id='+escape(id);
    http_request.open("GET",url,true);
    http_request.onreadystatechange=two;
    http_request.send(null);
//    send_request("GET",url,null,"text",callback);
  
    }
     document.getElementById("layer").style.display='none';
}

 商家一级分类<br />
                 <asp:DropDownList ID="drpListBsort" runat="server" Width="106px" AutoPostBack="True" OnSelectedIndexChanged="drpListBsort_SelectedIndexChanged">
                 </asp:DropDownList><br />
                   <a href="javascript:" οnclick="disPlay(1)">添加分类</a>
                 <br />
                 商品二级分类<br />
<asp:DropDownList ID="drpListSsort" runat="server" Width="103px">
                 </asp:DropDownList>
<a href="javascript:" οnclick="disPlay(2)">添加分类</a>
                   <div id="layer" style=" height:50px; left:150px; top:1050px; background-color:AntiqueWhite; display:none;">
                       <input type="text" id="txtSort" /><br />
                       <input type="button" id="btn_Add" value="添加"  οnclick="submitOnerequest(this.value)"/>
                       <input type="button" id="btn_Close" value="关闭" οnclick="OutdisPlay()" />
                       </div>

 

在'UseAjax.aspx页面的代码

 

 DataBase db=new DataBase();
    string sql = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!this.IsPostBack)
        {
           if (Request.QueryString["id"] == null && Request.QueryString["sort"]!=null)
          {
              sql = "insert into City_Product_Bsort(Bname) values('"+Request.QueryString["sort"].ToString()+"');select @@identity as id";
              int id=Convert.ToInt32(db.ExecuteScalar(sql));
               if (id>0)
              {
                  Response.Write(id.ToString());
              }     
          }
          if (Request.QueryString["id"] != null && Request.QueryString["sort"] != null)
          {
              sql = "insert into City_Product_Ssort(Sname,Bid) values('"+Request.QueryString["sort"].ToString()+"',"+Request.QueryString["id"].ToString()+");select @@identity as id";
              int id = Convert.ToInt32(db.ExecuteScalar(sql));
              if(id>0)
              {
                  Response.Write(id.ToString());
              }
          }
        }
       
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值