城市列表ajax 三级联动,无刷新

public class AjaxMethod
{
    public AjaxMethod()
    {
        //
        // TODO: Add constructor logic here
        //
    }

    #region GetProvinceList
    public static DataSet GetProvinceList()
    {
        string sql = "select distinct ProvinceName from Area order by ProvinceName Asc";
        return GetDataSet(sql);
    }
    #endregion

    #region GetCityList
    [Ajax.AjaxMethod(Ajax.HttpSessionStateRequirement.Read)]
    public static DataSet GetCityList(string provinceitem)
    {
        string sql = "select distinct CityName from Area where ProvinceName='" + provinceitem + "'  order by CityName Asc";
        return GetDataSet(sql);
    }
    #endregion

    #region GetDistrictList
    [Ajax.AjaxMethod(Ajax.HttpSessionStateRequirement.Read)]
    public static DataSet GetDistrictList(string cityitem)
    {
        string sql = "select distinct DistrictName from Area where CityName='" + cityitem + "'  order by DistrictName Asc";
        return GetDataSet(sql);
    }
    #endregion

    #region GetAroundList
    [Ajax.AjaxMethod(Ajax.HttpSessionStateRequirement.Read)]
    public static DataSet GetAroundList(string districtitem)
    {
        string sql = "select distinct AroundName from Area where DistrictName='" + districtitem + "'  order by AroundName Asc";
        return GetDataSet(sql);
    }
    #endregion


    #region GetDataSet
    public static DataSet GetDataSet(string sql)
    {
        ChangHope_DB chdb = new ChangHope_DB();
        DataSet ds = chdb.Db_ds(sql, "ttt");
        DataRow newRow = ds.Tables["ttt"].NewRow();
        newRow[0] = "所有";

        ds.Tables["ttt"].Rows.InsertAt(newRow,0);
        return ds;
    }
    #endregion

    #region GetNewsList
    [Ajax.AjaxMethod(Ajax.HttpSessionStateRequirement.Read)]
    public static DataSet GetNewsList()
    {
        string sql = "select top 10 Yx_NewsTitle from YX_News order by YX_ID desc";
        return GetDataSet(sql);
    }
    #endregion


 protected void Page_Load(object sender, EventArgs e)
    {       
        Function.setCitySession();
        Ajax.Utility.RegisterTypeForAjax(typeof(AjaxMethod));
        DataBind();
      
        if (!IsPostBack)
        {
            initValue();
        }
        Showforum();
    }

    //本周明星美容院
    public override void DataBind()
    {
        string sql = "select top 3 * from [Parlor] where (Status=2 or Status=3) and Locked=1 and Province='"+ Convert.ToString(Session["City"]) +"' and Good=1 and [Top]=1 order by ID desc";
        ChangHope_DB chdb = new ChangHope_DB();
        this.DataList1.DataSource = chdb.Db_ds(sql, "AAA");
        this.DataList1.DataBind();
    }

    public string accountservice(int pid)
    {
        string sql = "select count(ParlorID) from service where ParlorID=" + pid.ToString() + "";
        return Convert.ToString(ChangHope_DB.Retint(sql));
    }

    public void initValue()
    {
        ChangHope_DB chdb = new ChangHope_DB();
        Ajax.Utility.RegisterTypeForAjax(typeof(AjaxMethod));
        this.Province.DataTextField = "ProvinceName";
        this.Province.DataValueField = "ProvinceName";
        this.Province.DataSource = AjaxMethod.GetProvinceList();
        this.Province.DataBind();
        this.City.DataTextField = "CityName";
        this.City.DataValueField = "CityName";
        this.City.DataSource = AjaxMethod.GetCityList(this.Province.Items[0].Text.ToString());
        this.City.DataBind();
        this.District.DataTextField = "DistrictName";
        this.District.DataValueField = "DistrictName";
        this.District.DataSource = AjaxMethod.GetDistrictList(this.City.Items[0].Text.ToString());
        this.District.DataBind();
        this.Around.DataTextField = "AroundName";
        this.Around.DataValueField = "AroundName";
        this.Around.DataSource = AjaxMethod.GetAroundList(this.District.Items[0].Text.ToString());
        this.Around.DataBind();
        this.Province.Attributes.Add("onchange", "CityResult();");
        this.City.Attributes.Add("onchange", "DistrictResult();");
        this.District.Attributes.Add("onchange", "AroundResult();");


        this.FunctionMain.DataTextField = "FunctionName";
        this.FunctionMain.DataValueField = "FunctionName";
        this.FunctionMain.DataSource = chdb.Db_ds("select distinct FunctionName from Product_Function order by FunctionName Asc", "pro");
        this.FunctionMain.DataBind();
        this.FunctionMain.Items.Add(new ListItem("所有", "0"));
        this.FunctionMain.Items.FindByText("所有").Selected = true;

        this.FunctionSub.DataTextField = "FunctionSubName";
        this.FunctionSub.DataValueField = "FunctionSubName";
        this.FunctionSub.DataSource = chdb.Db_ds("select FunctionSubName from Product_Function where FunctionName='" + this.FunctionMain.SelectedValue.ToString() + "' order by FunctionSubName Asc ", "pro");
        this.FunctionSub.DataBind();
        this.FunctionSub.Items.Add(new ListItem("所有", "0"));
        this.FunctionSub.Items.FindByText("所有").Selected = true;

        this.BigClass.DataTextField = "YX_ClassName";
        this.BigClass.DataValueField = "YX_ID";
        this.BigClass.DataSource = chdb.Db_ds("select * from [YX_NewsClass] where YX_ParentID=0 and YX_Child<>0 order by YX_OrderID,YX_RootID", "WWW");
        this.BigClass.DataBind();
        this.BigClass.Items.Add(new ListItem("所有", "0"));
        this.BigClass.Items.FindByText("所有").Selected = true;

        //this.SmallClass.DataTextField = "YX_ClassName";
        //this.SmallClass.DataValueField = "YX_ID";
        //this.SmallClass.DataSource = chdb.Db_ds("select * from [YX_NewsClass] where YX_ParentID="+this.BigClass.SelectedValue.ToString(), "VVV");
        //this.SmallClass.DataBind();
        this.SmallClass.Items.Add(new ListItem("所有","0"));
        this.SmallClass.Items.FindByText("所有").Selected = true;

        this.Service_Main.DataTextField = "FunctionName";
        this.Service_Main.DataValueField = "FunctionName";
        this.Service_Main.DataSource = chdb.Db_ds("select distinct FunctionName  from Service_Function order by FunctionName Asc", "SF");
        this.Service_Main.DataBind();
        this.Service_Main.Items.Add(new ListItem("所有", "0"));
        this.Service_Main.Items.FindByText("所有").Selected = true;

        this.Service_sub.DataTextField = "FunctionSubName";
        this.Service_sub.DataValueField = "FunctionSubName";
        this.Service_sub.DataSource = chdb.Db_ds("select FunctionSubName from Service_Function where FunctionName='" + this.Service_Main.SelectedValue.ToString() + "' order by FunctionSubName Asc ", "pro");
        this.Service_sub.DataBind();
        this.Service_sub.Items.Add(new ListItem("所有", "0"));
        this.Service_sub.Items.FindByText("所有").Selected = true;
    }

    protected void FunctionMain_SelectedIndexChanged(object sender, EventArgs e)
    {
        ChangHope_DB chdb = new ChangHope_DB();
        this.FunctionSub.DataSource = chdb.Db_ds("select FunctionSubName from Product_Function where FunctionName='" + FunctionMain.SelectedItem.Text + "' order by FunctionSubName Asc", "pro2");
        this.FunctionSub.DataTextField = "FunctionSubName";
        this.FunctionSub.DataValueField = "FunctionSubName";
        this.FunctionSub.DataBind();
        this.FunctionSub.Items.Add(new ListItem("所有", "0"));
        this.FunctionSub.Items.FindByText("所有").Selected = true;
    }


    protected void BigClass_SelectedIndexChanged(object sender, EventArgs e)
    {
        ChangHope_DB chdb = new ChangHope_DB();
        this.SmallClass.DataSource = chdb.Db_ds("select YX_ClassName,YX_ID from [YX_NewsClass] where YX_ParentID=" + this.BigClass.SelectedValue + "order by YX_OrderID,YX_RootID", "pr773");
        this.SmallClass.DataTextField = "YX_ClassName";
        this.SmallClass.DataValueField = "YX_ID";
        this.SmallClass.DataBind();
        this.SmallClass.Items.Add(new ListItem("所有", "0"));
        this.SmallClass.Items.FindByText("所有").Selected = true;
    }


    protected void Service_Main_SelectedIndexChanged(object sender, EventArgs e)
    {
        ChangHope_DB chdb = new ChangHope_DB();
        this.Service_sub.DataSource = chdb.Db_ds("select FunctionSubName from [Service_Function] where FunctionName='" + this.Service_Main.SelectedItem.Text+ "' order by ID Asc", "pr333");
        this.Service_sub.DataTextField = "FunctionSubName";
        this.Service_sub.DataValueField = "FunctionSubName";
        this.Service_sub.DataBind();
        this.Service_sub.Items.Add(new ListItem("所有", "0"));
        this.Service_sub.Items.FindByText("所有").Selected = true;
    }

    protected void Showforum()
    {
        string sql = "select top 10 [id],[title] from bbs_topic order by addtime";
        ChangHope_DB chdb = new ChangHope_DB();
        DataView dv = chdb.Db_ds(sql, "topic").Tables[0].DefaultView;
        if (dv.Count > 1)
            DataList2.Width = new System.Web.UI.WebControls.Unit(360);
        else
            DataList2.Width = new System.Web.UI.WebControls.Unit(180);
        DataList2.DataSource = dv;
        DataList2.DataBind();
    }
}


<script language="javascript">           
            //城市------------------------------
            function CityResult()
            {
                var Province=document.getElementById("ctl00_MainContent_Province");
                AjaxMethod.GetCityList(Province.value,get_City_Result_CallBack);
            }
           
            function get_City_Result_CallBack(response)
            {
                if (response.value != null)
                {                   
                    //debugger;
                    document.all("ctl00_MainContent_City").length=0;               
                var ds = response.value;
                    if(ds != null && typeof(ds) == "object" && ds.Tables != null)
                    {                   
                        for(var i=0; i<ds.Tables[0].Rows.length; i++)
                    {
                        var name=ds.Tables[0].Rows[i].CityName;
                      var id=ds.Tables[0].Rows[i].CityName;
                      document.all("ctl00_MainContent_City").options.add(new Option(name,id));
                    }
                    }
                    DistrictResult();
                }               
                return
            }
           
           
           
            //区县----------------------------------------
            function DistrictResult()
            {
                var City=document.getElementById("ctl00_MainContent_City");
                AjaxMethod.GetDistrictList(City.value,get_District_Result_CallBack);
            }
            function get_District_Result_CallBack(response)
            {
                if (response.value != null)
                {                   
                    document.all("ctl00_MainContent_District").length=0;               
                var ds = response.value;
                    if(ds != null && typeof(ds) == "object" && ds.Tables != null)
                    {                   
                        for(var i=0; i<ds.Tables[0].Rows.length; i++)
                    {
                      var name=ds.Tables[0].Rows[i].DistrictName;
                      var id=ds.Tables[0].Rows[i].DistrictName;
                      document.all("ctl00_MainContent_District").options.add(new Option(name,id));
                    }               
                    }
                    AroundResult();
                }
                return
            }
           
            function AroundResult(){
               var District=document.getElementById("ctl00_MainContent_District");
               AjaxMethod.GetAroundList(District.value,get_Around_Result_CallBack);
            }
           
            function get_Around_Result_CallBack(response){
               if (response.value != null)
                {                   
                    document.all("ctl00_MainContent_Around").length=0;               
                var ds = response.value;
                    if(ds != null && typeof(ds) == "object" && ds.Tables != null)
                    {                   
                        for(var i=0; i<ds.Tables[0].Rows.length; i++)
                    {
                      var name=ds.Tables[0].Rows[i].AroundName;
                      var id=ds.Tables[0].Rows[i].AroundName;
                      document.all("ctl00_MainContent_Around").options.add(new Option(name,id));
                    }               
                    }
                }
                return
            }
          
    </script>

<httpHandlers>
   <add verb="POST,GET" path="ajax/*.ashx" type="Ajax.PageHandlerFactory, Ajax"/>     
    </httpHandlers>

 

你可能感兴趣的:(Ajax)