一个Ajax实例(成本项目)

前端代码:
<script type="text/javascript" language="javascript">
function  getHttpRequestObject()//创建xmlhttprequest对象
{
    
var xmlhttp;//定义变量,用来创建xmlhttprequest对象
    if (!xmlhttp && typeof XMLHttpRequest != 'undefined')// 创建xmlhttprequest,ajax开始
     {
        try
         {
          xmlhttp = new XMLHttpRequest();//非IE浏览器,用xmlhttprequest对象创建
         }
         catch (e)
         {
          xmlhttp = false;
         }
    }
    else
    {
         try
         {
              xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");//IE浏览器用activexobject对象创建
          } 
          catch (e) 
          {
              try {
                      xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");//IE浏览器用activexobject对象创建
                   } 
              catch(e)
               {
                  xmlhttp = false;
                }
          }
    }
    return xmlhttp;      
}
    
function  setMenuVisible(isVisible)//菜单是否显示
    {
        if(isVisible == true)
        {
           document.getElementById("divmenu").style.display = "inline";//显示菜单
           var obj=document.getElementById("txtGroupName");//获取菜单要显示的地方(HTML元素)
           var left=obj.offsetLeft;//获得obj控件距离左边或上层控件的位置
           var top=obj.offsetTop;//获得obj控件距离上边或上层控件的位置
           while(obj!=document.body)//循环获得obj控件在页面的绝对位置
             {    
             left +=obj.offsetLeft;
             top +=obj.offsetTop;
             obj=obj.offsetParent ;
             }
             document.getElementById("divmenu").style.top=document.getElementById("txtGroupName").offsetHeight +top+document.body.offsetTop;
             document.getElementById("divmenu").style.left=left+document.body.offsetLeft;
        }
        else
        {
            document.getElementById("divmenu").style.display = "none";
        }
    }


    var req;
 function  onComplete()
    {   
        var strGroupName=document.getElementById("txtGroupName").value.replace(/^\s+|\s+$/g,"");
        if(strGroupName!="")
        {
        var url;
        url ="CreaditUserAdd.aspx"+"?action=query&groupName="+escape(strGroupName);
        req = getHttpRequestObject();//获得XmlHttpRequset对象
        req.onreadystatechange = processRequest;//请求状态发生变化,回调函数
        req.open("GET",url,true);
        req.send(null);//发送请求
        }
          
    }

 function  fillSuggest(name)
    {
        document.getElementById("txtGroupName").value = name;
        setMenuVisible(false);
        GroupChange(name);
    }

function  processRequest()//回调函数
    {
        if(req.readyState == 4)//接收成功
        {
            setMenuVisible(false);         
            if(req.status == 200)
            {
                var names = req.responseXML.getElementsByTagName("Names")[0].selectNodes("Name");
                clearMenu();
                if(names.length > 0)
                {
                    var newDivHtml = "";
                    for(var i=0;i<names.length;i++)
                    {
                        newDivHtml += "<div style=\"width:150px; align:center; background-color:White; border-right: #cccccc 1px solid;z-index:2; border-left: #cccccc 1px solid;display:\"inline\";\" onMouseOver=\"this.style.background ='#ddefff'\" onMouseOut=\"this.style.background ='#FFFFFF'\" onclick=\"fillSuggest('"+names[i].text+"')\">"+names[i].text+"</div>";
                    }                             
                    document.getElementById("divmenu").innerHTML = newDivHtml;
                    setMenuVisible(true);
                }
            }
            else
            {
                clearMenu();
            }
        } 
    }

 function  clearMenu()
    {
         document.getElementById("divmenu").innerHTML = "&nbsp;";   
         //position:absolute;
    }
    
 function  GroupChange(name)
   {
    var strGroupName;
  
    if(name==null)
       strGroupName=document.getElementById("txtGroupName").value.replace(/^\s+|\s+$/g,"");
    else
       strGroupName=name;
     
    if(strGroupName!="")
    {
        var url;
        url ="CreaditUserAdd.aspx"+"?action=getSize&groupName="+escape(strGroupName);
        req = getHttpRequestObject();
        req.onreadystatechange = setGroupSize;
        req.open("GET",url,true);
        req.send(null);
    }
   }
  
   ///设置集团规模
   function setGroupSize()
   {
    if(req.readyState == 4&&req.status == 200&&req.statusText == "OK")
    {        
        if(req.responseText!="")
        {
            document.getElementById("txtGroupSize").value=req.responseText;
        }
     }
   }

    //信用类型改变
    function dplType_change()
    {
      if(document.getElementById("dplType").value=="1006000003")
      {
        document.getElementById("trGroup").style.display="inline";
        document.getElementById("rfvGroupName").enabled=true;
      }
      else
      {
        document.getElementById("trGroup").style.display="none";
        document.getElementById("rfvGroupName").enabled=false;
      }
    }
    </script>

<input type="text" style="width: 150px;" runat="server" id="txtGroupName" onchange="GroupChange()"
                        onkeyup="onComplete()" />

<asp:DropDownList ID="ddlSalePan" runat="server" Width="150px"></asp:DropDownList>

后端代码:

#region 模糊查询集团
        if (null != Request.QueryString["groupName"] && Request["action"] == "query")
        {
            Response.ContentType = "text/xml";
            Response.AppendHeader("Cache-Control", "no-cache");
            Response.Write(GetSuggestInfo(Request.QueryString["groupName"].Trim()));
            Response.End();
        }
#endregion

#region 获得集团规模
        if (null != Request.QueryString["groupName"] && Request["action"] == "getSize")
        {
            Response.Clear();
            Response.Write(CM_CREDIT_USER.getGroupSize(Request["groupName"]));
            Response.End();
        }
#endregion

#region 保持客户端控件的属性
        if (dplType.SelectedValue == "1006000003")
        {
            if (txtGroupName.Name.Trim() != "")
            {
                txtGroupSize.Attributes["value"] = CM_CREDIT_USER.getGroupSize(txtGroupName.Value.Trim());
            }
            trGroup.Style["display"] = "inline";
            rfvGroupName.Enabled = true;
        }
        else
        {
            trGroup.Style["display"] = "none";
            rfvGroupName.Enabled = false;
        }

#endregion

Business层:

#region GetSuggestInfo
    public string GetSuggestInfo(string strname)
        {

            XmlDocument doc = new XmlDocument();

            XmlNode rootnode = doc.CreateNode(XmlNodeType.Element, "Names", "");

            doc.AppendChild(rootnode);
            DataSet ds = QueryGroupFromBbossByName(strname);
            if (ds != null && ds.Tables[0].Rows.Count<1000)
            {
                foreach (DataRow objRow in ds.Tables[0].Rows)
                {
                    XmlNode subnode = doc.CreateNode(XmlNodeType.Element, "Name", "");
                    subnode.InnerText = objRow["customer_name"].ToString();
                    doc.ChildNodes[0].AppendChild(subnode);
                }
            }
            return doc.InnerXml;
        }

#endregion
#region getGroupSize
        /// <summary>
        /// 根据集团名称获得集团规模
        /// </summary>
        /// <param name="strGroupName">集团名称</param>
        /// <returns>集团规模</returns>
        public static string getGroupSize(string strGroupName)
        {
            string strReturn = "";
            DataSet ds = new CM_GROUP_COST().GetGroupInfoFromBboss(strGroupName);
            if (ds != null && ds.Tables[0].Rows.Count != 0)
            {
                if (ds.Tables[0].Rows[0]["CUSTOMER_SIZE"] == DBNull.Value)
                    return "";
                int nGroupSize = Convert.ToInt32(ds.Tables[0].Rows[0]["CUSTOMER_SIZE"]);
                switch (nGroupSize)
                {
                    case 1:
                        strReturn = "A 类";
                        break;
                    case 2:
                        strReturn = "B 类";
                        break;
                    case 3:
                    case 4:
                        strReturn = "C 类";
                        break;
                    default:
                        strReturn = "";
                        break;
                }
            }
            return strReturn;
        }
#endregion

posted @ 2008-06-26 14:43  Gerald1983  阅读(287)  评论(0编辑  收藏  举报