当前位置:编程学习 > C#/ASP.NET >>

Dropdownlist 三级联动 无刷新的问题


脚本中
<script language="javascript">            
            //城市------------------------------
            function cityResult() 
            { 
                var city=document.getElementById("DropDownList1");
              
                Ajax_Method.GetCityList(city.value,CallbackCll);

            }
            
            function CallbackCll(response)
            {
                alert(response.value);
                                   
                    debugger;
                    document.all("DropDownList2").length=0;                
                var count = response.value.Rows.length;
                alert(count);
                    if(count !=0)
                    {                    
                        for(var i=0; i<count; i++)
                    {
                        var name=response.value.Rows[i]["city_name"];
                      var id=response.value.Rows[i]["city_id"];
                      document.all("DropDownList2").options.add(new Option(name,id));
                    }
                    }
              
                
                areaResult();            
                return
            }
            //市区----------------------------------------
            function areaResult() 
            { 
                var area=document.getElementById("DropDownList2");
                Ajax_Method.GetAreaList(area.value,CallbackCll);
            }
            function CallbackCll(response)
            {
                if (response.value != null)
                {                    
                    document.all("DropDownList3").length=0;                
                var count = response.value.Rows.length;
                    if(count !=0)
                    {                    
                        for(var i=0; i<count; i++)
                    {
                      var name=response.value.Rows[i]["qx_name"];;
                      var id=response.value.Rows[i]["qx_id"];;
                      document.all("DropDownList3").options.add(new Option(name,id));
                    }                
                    }
                }
                return
            }
</script>


后台 代码中



public partial class Ajax_Method: System.Web.UI.Page
    {
           protected void Page_Load(object sender, EventArgs e)
           {
             AjaxPro.Utility.RegisterTypeForAjax(typeof(Ajax_Method));
          
          if (!IsPostBack)
            {
                this.DropDownList1.DataSource = GetProvinceList();
                this.DropDownList1.DataTextField = "sf_name";
                this.DropDownList1.DataValueField = "sf_id";
                this.DropDownList1.DataBind();

                this.DropDownList1.Attributes.Add("onchange", "cityResult();");
                this.DropDownList2.Attributes.Add("onchange", "areaResult();");
            }
           }
       
          public  DataTable GetProvinceList()
        {
            string sql = "select * from ShengFen";
            return Common.getsql(connectionstring_static, sql);
        }

       
        [AjaxPro.AjaxMethod]
        public static DataTable GetCityList(int provinceid)
        {
            string sql = "select * from City where sf_id = " + provinceid;
             DataTable dt=Common.getsql(connectionstring_static,sql);
            return dt;
        }

        [AjaxPro.AjaxMethod]
        public static DataTable GetAreaList(int cityid)
        {
            string sql = "select * from Qx where City_id = " + cityid;
            DataTable dt=Common.getsql(connectionstring_static,sql);
            return dt;
        }

            
}

当加载页面的时候只显示  省份的名字    城市 和 区 没有显示  选择某个省份的时候  城市 和 区也不显示




--------------------编程问答-------------------- 绑定 --------------------编程问答-------------------- 数据库 

   省份  ShengFen           城市  City                    区 Qx
         sf_id  sf_name     city_id  city_name  sf_id    qx_id  qx_name  city_id --------------------编程问答-------------------- http://www.cnblogs.com/oec2003/archive/2007/11/30/978069.html --------------------编程问答-------------------- 上述问题 如何解决 --------------------编程问答-------------------- 偶有个js的。。。挺好用

要的话 加 792713666
--------------------编程问答-------------------- 上述问题 如何解决 .... --------------------编程问答-------------------- 上述问题 如何解决 .... --------------------编程问答-------------------- 上述问题 如何解决 .... --------------------编程问答--------------------  function CallbackCll(response)
{
if (response.value != null)
{

document.all("DropDownList_province1").length=0; 
document.all("DropDownList_area1").length=0;    
     var ds = response.value;
if(ds != null && typeof(ds) == "object")
{
document.all("DropDownList_province1").options.add(new Option("",""));
for(var i=0; i<ds.Tables[0].Rows.length; i++)
     {
     var name=ds.Tables[0].Rows[i].Province;
       var id=ds.Tables[0].Rows[i].Province;
       document.all("DropDownList_province1").options.add(new Option(name,id));
     }
}
}
return
}
function areaResult() 

var area=document.getElementById("DropDownList_province1");

var  YR=document.getElementById("Hd_province1");
YR.value=area.value;
AjaxMethod.GetCityList(area.value,get_city_Result);
}
function get_city_Result(response)
{
if (response.value != null)
{
document.all("DropDownList_area1").length=0;    
     var ds = response.value;

if(ds != null && typeof(ds) == "object")
{
for(var i=0; i<ds.Tables[0].Rows.length; i++)
     {
       var name=ds.Tables[0].Rows[i].City;
       var id=ds.Tables[0].Rows[i].City;
       document.all("DropDownList_area1").options.add(new Option(name,id));
     }
}
}
return
}
--------------------编程问答-------------------- 回帖是一种美德!每天回帖即可获得 10 分可用分! --------------------编程问答--------------------
引用 9 楼 wuyq11 的回复:
function CallbackCll(response) 

if (response.value != null) 


document.all("DropDownList_province1").length=0;  
document.all("DropDownList_area1").length=0;     
     var ds = response.value; 
if(ds != null && typeof(ds) == "object") 

document.all("DropDownList_province1").options.add(new Option("","")); 
for(var i=0; i <ds.Tables[0].Rows.length; i++) 
     { 



DropDownList_province1(DropDownList1)已经在后台代码中 绑定数据了为何还要在脚本中添加 --------------------编程问答-------------------- 如果想无刷新,好像只有js(+ajax)来做了。用asp.net必须控制局部在刷新,用自带的Ajax控件,否则三级联动一般的绑定,选省了,整个页面都会刷新的。 --------------------编程问答-------------------- 还是js或ajax实现比较好 --------------------编程问答-------------------- 他这个本身就是ajax,没看到[ajax.ajaxmethod]属性吗

1.你用什么浏览器?我看到你的代码里有document.all,比如这个火狐就不支持
2.看看你的浏览器状态烂上有没有js错误?
3.看你的写法跟我以前的写法一样,没什么错误,你可以调试一下,看看返回的table
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,