ASP.NET----级联菜单

实例1:

省市区县(含全国完整数据库)Ajax无刷新三级联动,利用微软官方ASP.NET AJAX Control Toolkit和WebService结合实现的完整无刷新省市区县三级,该数据库包含大陆32个省市自治区及部分港澳台地区信息,Bin下自带Ajax及AJAX Control Toolkit的控件程序,可以直接使用!

HTML:

View Code
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>省市区县无刷新联动</title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<div>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:DropDownList ID="ddlProvince" runat="server">
</asp:DropDownList>
<asp:DropDownList ID="ddlCity" runat="server">
</asp:DropDownList>
<asp:DropDownList ID="ddlVilliage" runat="server">
</asp:DropDownList><ajax:CascadingDropDown ID="CascadingDropDown2" runat="server" TargetControlID="ddlProvince" ServicePath="SNWebService.asmx" ServiceMethod="GetProvinceContents" Category="Province" PromptText="请选择省份" LoadingText="省份加载中...">
</ajax:CascadingDropDown>
<ajax:CascadingDropDown ID="CascadingDropDown1" runat="server" ParentControlID="ddlProvince" ServicePath="SNWebService.asmx" ServiceMethod="GetCityContents" Category="City" TargetControlID="ddlCity" PromptText="请选择城市" LoadingText="城市加载中...">
</ajax:CascadingDropDown>
<ajax:CascadingDropDown ID="CascadingDropDown3" runat="server" Category="Villiage" LoadingText="区县加载中..."
ParentControlID
="ddlCity" PromptText="请选择旅游线路" ServiceMethod="GetViliageContents"
ServicePath
="SNWebService.asmx" TargetControlID="ddlVilliage">
</ajax:CascadingDropDown>
</ContentTemplate>
</asp:UpdatePanel>
<br />
</div>
</form>
</body>
</html>

源代码下载:  http://u.115.com/file/bhzd6hwt

实例2:

普通数据库连接BindList:

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
选择你所在的省区:
<asp:DropDownList runat="server" ID="ddlPrivouce"></asp:DropDownList>
<br />
选择你所在的市区:
<asp:DropDownList runat="server" ID="ddlPrivouce0"></asp:DropDownList>
<br />
<asp:Button ID="Button1" runat="server" Text="提交" onclick="Button1_Click" />
</div>
</form>
</body>
</html>

cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.BindList();//绑定DropDownList控件
}
}

protected void BindList()
{
//数据连接对象
SqlConnection con = new SqlConnection(@"Data Source=.;Initial Catalog=Address_data;Integrated Security=True;password=sa");
SqlCommand com
= con.CreateCommand();//SQL命令对象
com.CommandText = "SELECT * FROM Area WHERE ParentID=0";//查询时执行的SQL语句

SqlDataAdapter adpt
= new SqlDataAdapter();//数据适配器对象
adpt.SelectCommand = com;//设定适配器的查询命令对象

DataSet ds
= new DataSet();//实例化数据集对象
try
{
adpt.Fill(ds);
//数据适配器填充数据集对象
}
finally
{
con.Close();
//关闭连接
}

this.ddlPrivouce.DataSource = ds;//设定DropDownList控件绑定的数据源 该数据源可以是任何实现IContainer接口的对象
this.ddlPrivouce.DataTextField = "Name";//设定DropDownList绑定的数据项中文本显示的数据列
this.ddlPrivouce.DataValueField = "AreaID";//设定DropDownList绑定的数据项中的值的数据列
this.ddlPrivouce.DataBind();//调用DataBind方法 进行数据绑定
}


protected void Button1_Click(object sender, EventArgs e)
{
if (this.ddlPrivouce.SelectedIndex.Equals("'北京市'"))
{
//显示用户选择省区 和省区ID


}
}
}

实例3:

javascript&jquery版本参考代码,收集于网上:

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title></title>



    <script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>



    <script>

        $(function() {

            $('#btn').click(function() {

                $.post('Handler.ashx', { val: $("#<%=ddlMain.ClientID %>").val() }, function(data) {

                    var json = eval(data);

                    var html = '';

                    for (var i in json) {

                        html += '<option value="' + json[i].Value + '">' + json[i].Name + '</option>';

                    }

                    $("#<%=ddlSub.ClientID %>")[0].options.length = 0;

                    $(html).appendTo($("#<%=ddlSub.ClientID %>"));



                });



            });

        });

    </script>



</head>

<body>

    <form id="form1" runat="server">

    <asp:DropDownList ID="ddlMain" runat="server">

        <asp:ListItem Text="一级城市" Value="1"></asp:ListItem>

        <asp:ListItem Text="旅游城市" Value="2"></asp:ListItem>

    </asp:DropDownList>

    <asp:DropDownList ID="ddlSub" runat="server">

    </asp:DropDownList>

    <input type="button" id='btn' value='click me' />

    </form>

</body>

</html>













public void ProcessRequest (HttpContext context) {

        

        context.Response.ContentType = "text/plain";        



        string json = string.Empty;

        

        if (context.Request.Form["val"] == "1")

        {

            json = "[{\"Name\":\"北京\",\"Value\":1},{\"Name\":\"深圳\",\"Value\":2},{\"Name\":\"上海\",\"Value\":3}]";

        }

        else if (context.Request.Form["val"] == "2")

        {

            json = "[{\"Name\":\"杭州\",\"Value\":4},{\"Name\":\"苏州\",\"Value\":5},{\"Name\":\"桂林\",\"Value\":6}]";

        }



        context.Response.Write(json);

        context.Response.End();

    }

































*******************************************************



/***



 * 插件名称: 中国省市信息联动插件



 */







$._cityInfo = [{"n":"北京市","c":["北京市"]},



{"n":"天津市","c":["天津市"]},



{"n":"上海市","c":["上海市"]},



{"n":"重庆市","c":["重庆市"]},



{"n":"河北省","c":["石家庄市","唐山市","秦皇岛市","邯郸市","邢台市","保定市","张家口市","承德市","沧州市","廊坊市","衡水市"]},



{"n":"山西省","c":["太原市","大同市","阳泉市","长治市","晋城市","朔州市","晋中市","运城市","忻州市","临汾市","吕梁市"]},



{"n":"台湾省","c":["台北市","高雄市","基隆市","台中市","台南市","新竹市","嘉义市","台北县","宜兰县","桃园县","新竹县","苗栗县","台中县","彰化县","南投县","云林县","嘉义县","台南县","高雄县","屏东县","澎湖县","台东县","花莲县"]},



{"n":"辽宁省","c":["沈阳市","大连市","鞍山市","抚顺市","本溪市","丹东市","锦州市","营口市","阜新市","辽阳市","盘锦市","铁岭市","朝阳市","葫芦岛市"]},



{"n":"吉林省","c":["长春市","吉林市","四平市","辽源市","通化市","白山市","松原市","白城市","延边朝鲜族自治州"]},



{"n":"黑龙江省","c":["哈尔滨市","齐齐哈尔市","鹤岗市","双鸭山市","鸡西市","大庆市","伊春市","牡丹江市","佳木斯市","七台河市","黑河市","绥化市","大兴安岭地区"]},



{"n":"江苏省","c":["南京市","无锡市","徐州市","常州市","苏州市","南通市","连云港市","淮安市","盐城市","扬州市","镇江市","泰州市","宿迁市"]},



{"n":"浙江省","c":["杭州市","宁波市","温州市","嘉兴市","湖州市","绍兴市","金华市","衢州市","舟山市","台州市","丽水市"]},



{"n":"安徽省","c":["合肥市","芜湖市","蚌埠市","淮南市","马鞍山市","淮北市","铜陵市","安庆市","黄山市","滁州市","阜阳市","宿州市","巢湖市","六安市","亳州市","池州市","宣城市"]},



{"n":"福建省","c":["福州市","厦门市","莆田市","三明市","泉州市","漳州市","南平市","龙岩市","宁德市"]},



{"n":"江西省","c":["南昌市","景德镇市","萍乡市","九江市","新余市","鹰潭市","赣州市","吉安市","宜春市","抚州市","上饶市"]},



{"n":"山东省","c":["济南市","青岛市","淄博市","枣庄市","东营市","烟台市","潍坊市","济宁市","泰安市","威海市","日照市","莱芜市","临沂市","德州市","聊城市","滨州市","荷泽市"]},



{"n":"河南省","c":["郑州市","开封市","洛阳市","平顶山市","安阳市","鹤壁市","新乡市","焦作市","濮阳市","许昌市","漯河市","三门峡市","南阳市","商丘市","信阳市","周口市","驻马店市"]},



{"n":"湖北省","c":["武汉市","黄石市","十堰市","宜昌市","襄樊市","鄂州市","荆门市","孝感市","荆州市","黄冈市","咸宁市","随州市","恩施土家族苗族自治州","仙桃市","潜江市","天门市","神农架林区"]},



{"n":"湖南省","c":["长沙市","株洲市","湘潭市","衡阳市","邵阳市","岳阳市","常德市","张家界市","益阳市","郴州市","永州市","怀化市","娄底市","湘西土家族苗族自治州"]},



{"n":"广东省","c":["广州市","深圳市","珠海市","汕头市","韶关市","佛山市","江门市","湛江市","茂名市","肇庆市","惠州市","梅州市","汕尾市","河源市","阳江市","清远市","东莞市","中山市","潮州市","揭阳市","云浮市"]},



{"n":"甘肃省","c":["兰州市","金昌市","白银市","天水市","嘉峪关市","武威市","张掖市","平凉市","酒泉市","庆阳市","定西市","陇南市","临夏回族自治州","甘南藏族自治州"]},



{"n":"四川省","c":["成都市","自贡市","攀枝花市","泸州市","德阳市","绵阳市","广元市","遂宁市","内江市","乐山市","南充市","眉山市","宜宾市","广安市","达州市","雅安市","巴中市","资阳市","阿坝藏族羌族自治州","甘孜藏族自治州","凉山彝族自治州"]},



{"n":"贵州省","c":["贵阳市","六盘水市","遵义市","安顺市","铜仁地区","毕节地区","黔西南布依族苗族自治州","黔东南苗族侗族自治州","黔南布依族苗族自治州"]},



{"n":"海南省","c":["海口市","三亚市","五指山市","琼海市","儋州市","文昌市","万宁市","东方市","澄迈县","定安县","屯昌县","临高县","白沙黎族自治县","昌江黎族自治县","乐东黎族自治县","陵水黎族自治县","保亭黎族苗族自治县","琼中黎族苗族自治县"]},



{"n":"云南省","c":["昆明市","曲靖市","玉溪市","保山市","昭通市","丽江市","思茅市","临沧市","楚雄彝族自治州","红河哈尼族彝族自治州","文山壮族苗族自治州","西双版纳傣族自治州","大理白族自治州","德宏傣族景颇族自治州","怒江傈僳族自治州","迪庆藏族自治州"]},



{"n":"青海省","c":["西宁市","海东地区","海北藏族自治州","黄南藏族自治州","海南藏族自治州","果洛藏族自治州","玉树藏族自治州","海西蒙古族藏族自治州"]},



{"n":"陕西省","c":["西安市","铜川市","宝鸡市","咸阳市","渭南市","延安市","汉中市","榆林市","安康市","商洛市"]},



{"n":"广西壮族自治区","c":["南宁市","柳州市","桂林市","梧州市","北海市","防城港市","钦州市","贵港市","玉林市","百色市","贺州市","河池市","来宾市","崇左市"]},



{"n":"西藏自治区","c":["拉萨市","昌都地区","山南地区","日喀则地区","那曲地区","阿里地区","林芝地区"]},



{"n":"宁夏回族自治区","c":["银川市","石嘴山市","吴忠市","固原市","中卫市"]}];



 



$.initProv = function(prov, city, defaultProv, defaultCity) {



    var provEl = $(prov);



    var cityEl = $(city);



    var hasDefaultProv = (typeof(defaultCity) != 'undefined');



     



    var provHtml = '';



     



    provHtml += '<option value="-1">请选择</option>';



    for(var i = 0; i < $._cityInfo.length; i++) {



        provHtml += '<option value="' + i + '"' + ((hasDefaultProv && $._cityInfo[i].n == defaultProv) ? ' selected="selected"' : '') + '>' + $._cityInfo[i].n + '</option>';



    }



    provEl.html(provHtml);



    $.initCities(provEl, cityEl, defaultCity);



    provEl.change(function() {



        $.initCities(provEl, cityEl);



    });



};



 



$.initCities = function(provEl, cityEl, defaultCity) {



    var hasDefaultCity = (typeof(defaultCity) != 'undefined');



    if(provEl.val() != '' && parseInt(provEl.val()) >= 0) {



        var cities = $._cityInfo[parseInt(provEl.val())].c;



        var cityHtml = '';



         



        cityHtml += '<option value="-1">请选择</option>';



        for(var i = 0; i < cities.length; i++) {



            cityHtml += '<option value="' + i + '"' + ((hasDefaultCity && cities[i] == defaultCity) ? ' selected="selected"' : '') + '>' + cities[i] + '</option>';



        }



        cityEl.html(cityHtml);



    } else {



        cityEl.html('<option value="-1">请先选择</option>');



    }



};

  

  

  

你可能感兴趣的:(asp.net)