Ajax WebService 省級聯動

來源:互聯網
上載者:User

AjaxControlToolkit  控制項

建立服務 WebService

WebService.cs

using System;using System.Web;using System.Collections;using System.Web.Services;using System.Web.Services.Protocols;using System.Web.Script.Services;using System.Collections.Generic;using System.Collections.Specialized;using AjaxControlToolkit;using System.Data.SqlClient;using System.Data;/// <summary>/// SNWebService 的摘要說明/// </summary>[WebService(Namespace = "http://tempuri.org/")][WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)][ScriptService]    public class SNWebService : System.Web.Services.WebService{    public SNWebService()    {        //如果使用設計的組件,請取消注釋以下行         //InitializeComponent();     }    /// <summary>    /// 擷取省份資料    /// </summary>    /// <param name="knownCategoryValues"></param>    /// <param name="category"></param>    /// <returns></returns>    [WebMethod]    public CascadingDropDownNameValue[] GetProvinceContents(string knownCategoryValues, string category)    {        List<CascadingDropDownNameValue> provinceList = new List<CascadingDropDownNameValue>();        string connectionString = System.Configuration.ConfigurationManager.AppSettings["ConnectionString"];         SqlConnection sqlConn = new SqlConnection(connectionString);        sqlConn.Open();        string strSql = "Select * From Province";        SqlCommand sqlCmd = new SqlCommand(strSql, sqlConn);        SqlDataReader dtrProvince = sqlCmd.ExecuteReader();        while (dtrProvince.Read())        {            provinceList.Add(new CascadingDropDownNameValue(dtrProvince["Name"].ToString(),dtrProvince["Code"].ToString()));        }        dtrProvince.Close();        sqlConn.Close();        return provinceList.ToArray();    }    /// <summary>    /// 擷取市資料    /// </summary>    /// <param name="knownCategoryValues"></param>    /// <param name="category"></param>    /// <returns></returns>    [WebMethod]    public CascadingDropDownNameValue[] GetCityContents(string knownCategoryValues, string category)    {        StringDictionary provinceList = CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues);        string connectionString = System.Configuration.ConfigurationManager.AppSettings["ConnectionString"];        SqlConnection sqlConn = new SqlConnection(connectionString);        sqlConn.Open();        string strSql = "Select * From city Where ProvinceId='" + provinceList["Province"] + "'";        SqlCommand sqlCmd = new SqlCommand(strSql, sqlConn);        SqlDataReader dtrCity = sqlCmd.ExecuteReader();        List<CascadingDropDownNameValue> cityList = new List<CascadingDropDownNameValue>();        while (dtrCity.Read())        {            cityList.Add(new CascadingDropDownNameValue(dtrCity["Name"].ToString(), dtrCity["code"].ToString()));        }        dtrCity.Close();        return cityList.ToArray();    }    /// <summary>    /// 擷取鄉鎮資料    /// </summary>    /// <param name="knownCategoryValues"></param>    /// <param name="category"></param>    /// <returns></returns>    [WebMethod]    public CascadingDropDownNameValue[] GetViliageContents(string knownCategoryValues, string category)    {        StringDictionary cityList = CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues);        string connectionString = System.Configuration.ConfigurationManager.AppSettings["ConnectionString"];        SqlConnection sqlConn = new SqlConnection(connectionString);        sqlConn.Open();        string strSql = "Select * From area Where CityId='" + cityList["City"] + "'";        SqlCommand sqlCmd = new SqlCommand(strSql, sqlConn);        SqlDataReader dtrViliage = sqlCmd.ExecuteReader();        List<CascadingDropDownNameValue> viliageList = new List<CascadingDropDownNameValue>();        while (dtrViliage.Read())        {            viliageList.Add(new CascadingDropDownNameValue(dtrViliage["Name"].ToString(), dtrViliage["id"].ToString()));        }        dtrViliage.Close();        return viliageList.ToArray();    }    }

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.