[NET][Ajax]Ajax編程備忘之——Combobox無重新整理填充

來源:互聯網
上載者:User
Ajax編程備忘之——Combobox無重新整理填充

  編程思路如下:

  1:後台根據前台傳來的上層業務ID,擷取對應的下級部門,然後組成格式為

   部門代碼1-部門名稱1|部門代碼2-部門名稱2|部門代碼3-部門名稱3.....

   前台Client端根據傳來的字串進行拆分,然後用javascript重新組織下級部門的Combobox

  2:後台取Combobox值的時候用Request['ComboboxID']方式擷取,不用Combobox.Value

  [第2條適用於Asp.net]

  javascript部分的代碼:

  //上層業務修改後,下級部門也要隨著改動
   function DepartmentChange()
   {
    var objDept=document.all['cmbDeptID'];
    var objSubDept=document.all['cmbSubDeptID'];
        
    if(objDept && objSubDept)
    { 
     //Ajax調用        
     var response = Register.RefillDept(objDept.value,'-');
      
     if(response.value!="")
     {
         var length=1;
        
         while(length!=0)
         { 
       var length=objSubDept.options.length;
       for(var i=0;i<length;i++)
        objSubDept.options.remove(i);
       length=length/2;
      }
      
      var strItem=response.value.split('|');
      
      if(strItem.length>0)
      {
       for(var n=0;n<strItem.length;n++)
       {
        var option=document.createElement("option");
        option.value=strItem[n];
        option.text=strItem[n];
        //alert(option.value);
        objSubDept.options.add(option);        
       }
      }     
     }
    }   
      
   }

 

   Asp.net後台Server端代碼:

   /// <summary>
  /// 擷取部門改變後對應下級部門的資訊
  /// </summary>
  /// <param name="strDept">部門編碼-部門名稱</param>
  /// <param name="strSeparetor">拆分符號</param>
  /// <returns>對應下級部門,格式:部門1|部門2|部門3</returns>
  [Ajax.AjaxMethod()]
  public string RefillDept(string strDept,string strSeparetor)
  {
   string strDeptString="";

   try
   {
    string []strItem=strDept.Split(strSeparetor.ToCharArray());
               
    if(strItem.Length>1)
    {
     OLExamDepartment objOLExamDepartment=new OLExamDepartment();

     DataSet dstDept=objOLExamDepartment.GetDepartInfo(strItem[0]);
     
     if(dstDept!=null)
     {
      if(dstDept.Tables[0].Rows.Count>0)
      {
       //組成返回字串
       for(int i=0;i<dstDept.Tables[0].Rows.Count;i++)
       {
        strDeptString=strDeptString+dstDept.Tables[0].Rows[i]["DeptCode"].ToString()+"-"+dstDept.Tables[0].Rows[i]["DeptName"].ToString()+"|" ;
       }
      }
     }

     if(strDeptString.Length>0)
     {
      strDeptString=strItem[0]+"-本部|"+strDeptString.Substring(0,strDeptString.Length-1);
     }
     else
     {
      strDeptString=strItem[0]+"-本部";
     }
    }
        
    return strDeptString;
   }
   catch(Exception ex)
   {
    return "";
   }   
  }

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.