www.cnblogs.com/ustbwuyi
用ajax解决的一个完美例子

解决方案 »

  1.   

    function CallBackObject()
    {
    this.XmlHttp = this.GetHttpObject();
    }
    CallBackObject.prototype.GetHttpObject = function()
    {
    var xmlhttp; if (window.ActiveXObject)
    {
    try 
    {
    xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    }  
    catch (e) 
    {
    try 
    {
    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");

    catch (E) 
    {
    xmlhttp = false;
    }
    }
    }
    else
    {
    alert("不能创建XmlHttpRequest对象!浏览器不支持");
    return xmlhttp;
    } if (!xmlhttp && typeof XMLHttpRequest != 'undefined') 
    {
    try 
    {
    xmlhttp = new XMLHttpRequest();

    catch (e) 
    {
    xmlhttp = false;
    }
    }
    return xmlhttp;
    }
    CallBackObject.prototype.DoCallBack = function(URL)

    if( this.XmlHttp )
    {
    if( this.XmlHttp.readyState == 4 || this.XmlHttp.readyState == 0 )
    {
    var oThis = this;
    this.XmlHttp.open('POST', URL);
    this.XmlHttp.onreadystatechange = function() { oThis.ReadyStateChange(); };
    this.XmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    this.XmlHttp.send(null);
    }
    }
    }
    CallBackObject.prototype.AbortCallBack = function()
    {
    if( this.XmlHttp )
    {
    this.XmlHttp.abort();
    }
    }
    var   div; 
    CallBackObject.prototype.OnLoading = function()
    {
    div  =   document.createElement("DIV");   
    div.id   =   "div_TiShi";   
    div.innerHTML   =   '正在加载,请稍后.....';   
    div.onmousedown=function() {moveit(this)};
    div.style.backgroundColor="yellow";  
    document.body.appendChild(div);
    }
    CallBackObject.prototype.OnLoaded = function()
    {
    div.style.display="none";
    //window.alert('XmlHttpRequest对象已经加载...');
    }
    CallBackObject.prototype.OnInteractive = function()
    {
    //window.alert('交互中...');
    }
    CallBackObject.prototype.OnComplete = function(responseText, responseXml)
    {
        
    }
    CallBackObject.prototype.OnError = function(status, statusText)
    {
    // Error
    window.alert('错误的XmlHttpRequest对象状态!');
    }
    CallBackObject.prototype.ReadyStateChange = function()
    {
    if( this.XmlHttp.readyState == 1 )
    {
    this.OnLoading();
    }
    else if( this.XmlHttp.readyState == 2 )
    {
    this.OnLoaded();
    }
    else if( this.XmlHttp.readyState == 3 )
    {
    this.OnInteractive();
    }
    else if( this.XmlHttp.readyState == 4 )
    {
            
            
    if( this.XmlHttp.status == 0 )
    {
    this.OnAbort();
    alert('XmlHttpRequest对象尚未初始化!');
    }
    else if( this.XmlHttp.status == 200 && this.XmlHttp.statusText == "OK" )
    {
    this.OnComplete(this.XmlHttp.responseText, this.XmlHttp.responseXML);
    }
    else
    {
    this.OnError(this.XmlHttp.status, this.XmlHttp.statusText, this.XmlHttp.responseText);   
    }
    }
    }
                function ChooseCity()
                {
                    var f = document.Form1;
                    var selecteditem = f.drpProvince.value;
                    
                    var cbo = new CallBackObject();    
                    cbo.OnComplete = Cbo_Complete;
                    cbo.onError = Cbo_Error;
                    //将数据传到Ajax.aspx服务器端去处理(Ajax.aspx.cs的代码在下一代码块)
                    cbo.DoCallBack("Ajax.aspx?classid="+selecteditem);    
                }
                function Cbo_Complete(responseText, responseXML)
                {    
                    //信息已经成功返回,开始处理信息
                    var returnStr = responseText;
                
                    var xmlDom=new ActiveXObject("Microsoft.XmlDom");
                    xmlDom.loadXML(returnStr);
                    
                    //Table :返回的XML根节点(如果不知道,可以alert(responseText)看看...)
                    var newsItems=xmlDom.getElementsByTagName("Table");
                    /*
                    将城市的下拉列表清空
                    */
                    document.getElementById("drpCity").length=0;
                    //循环获取多少条记录
                    for(var i=0;i<newsItems.length;i++)
                    {
                        var newsItem = newsItems[i];                                                                                   
                        var strID = newsItem.getElementsByTagName("id")[0].text;
                        var strCity = newsItem.getElementsByTagName("boardname")[0].text;                    
                        //debugger;
                        //将获取的记录填充到城市的下拉列表中
                        document.all("drpCity").options.add(new Option(strCity,strID));
                    }
                }            function Cbo_Error()
                {
                    alert(responseText);
                }
    ajax.cs
    private void Page_Load(object sender, System.EventArgs e)
    {
    if(!IsPostBack)
      {
      string classid = Request.QueryString["classid"].ToString();
      if (classid.Length > 0) 
      {
      Response.Clear();   SqlConnection con = new SqlConnection(conStr);
                           string sqldasj="select * from SubTerritory where classboardid = 0 and classid='"+classid+"' and state=0  order by boardid";
       MyDataAdapter = new SqlDataAdapter(sqldasj,con);
      MyDataSet  = new DataSet();
      MyDataAdapter.Fill(MyDataSet,"city");   string chString = MyDataSet.GetXml();   Response.Clear();
      Response.ContentType = "text/xml";
                        
      Response.Write(chString);
      Response.End();
      }
      else
      {
      Response.Clear();
      Response.End();
      }    
      }
      else
      {
      Response.Clear();
      Response.End();
      } // 在此处放置用户代码以初始化页面
    }
      

  2.   

    我觉得为什么要那么复杂, 在第一个下拉控件里添加一个比如SelectedIndexChanged事件
    然后在里面类似
    getCitysByProvienceTableAdapter.FillBy(this.dataSetCity.GetCitysByProvience, ((int)(System.Convert.ChangeType(comboBoxProvience.SelectedValue, typeof(int)))));
      

  3.   

    楼主有什么问题吗?
    http://blog.csdn.net/zhulei2008/archive/2007/06/05/1638653.aspx
      

  4.   

    直接使用微软的ajax框架拉个updatepanel控件就OK