主頁 > 知識庫 > asp.net DropDownList實現(xiàn)二級聯(lián)動效果

asp.net DropDownList實現(xiàn)二級聯(lián)動效果

熱門標(biāo)簽:代理接電話機器人如何取消 地圖標(biāo)注的公司有哪些 遂寧市地圖標(biāo)注app 400電話辦理哪家性價比高 濮陽外呼電銷系統(tǒng)怎么樣 塔城代理外呼系統(tǒng) 天心智能電銷機器人 地圖定位圖標(biāo)標(biāo)注 地圖標(biāo)注專業(yè)團隊

最近在做新聞發(fā)布系統(tǒng)的時候,用到了二級聯(lián)動,我把使用方法記錄下來,以便日后查閱以及幫助新手朋友們。下面是效果圖:

下面來講解一下實現(xiàn)的方法:

1.在.aspx頁面中,拖入兩個DroDownList控件。代碼如下:

tr>
  td>新聞風(fēng)格:/td>
  td>asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"onselectedindexchanged="DropDownList1_SelectedIndexChanged" >/asp:DropDownList>
  /td>
/tr>
tr>
  td>新聞類型:/td>
  td>asp:DropDownList ID="DropDownList2" runat="server">/asp:DropDownList>
  /td>
/tr>

2.aspx.cs頁面中的代碼如下:

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

public partial class release_News : System.Web.UI.Page
{
  protected void DropdownList1()
  {
    DropDownList1.Items.Add(new ListItem("新聞動態(tài)"));
    DropDownList1.Items.Add(new ListItem("政務(wù)公開"));
    DropDownList1.Items.Add(new ListItem("網(wǎng)上辦事"));
  }
  protected void DropdownList2()
  {
    switch (DropDownList1.SelectedValue)
    {
      case "新聞動態(tài)":
        DropDownList2.Items.Clear();
        DropDownList2.Items.Add(new ListItem("工作動態(tài)"));
        DropDownList2.Items.Add(new ListItem("公示公告"));
        DropDownList2.Items.Add(new ListItem("經(jīng)濟新聞"));
        DropDownList2.Items.Add(new ListItem("省內(nèi)新聞"));
        DropDownList2.Items.Add(new ListItem("熱點新聞"));
        break;
      case "政務(wù)公開":
        DropDownList2.Items.Clear();
        DropDownList2.Items.Add(new ListItem("領(lǐng)導(dǎo)班子"));
        DropDownList2.Items.Add(new ListItem("機構(gòu)設(shè)置"));
        DropDownList2.Items.Add(new ListItem("計劃規(guī)劃"));
        break;
      case "網(wǎng)上辦事":
        DropDownList2.Items.Clear();
        DropDownList2.Items.Add(new ListItem("崗位職責(zé)"));
        DropDownList2.Items.Add(new ListItem("辦事流程"));
        break;
    }
  }
  protected void Page_Load(object sender, EventArgs e)
  {
     if (!IsPostBack)
    {
      DropdownList1();
      DropdownList2();
    }
  }
  protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
  {
    DropdownList2();
  }
}


使用DroDownList和js實現(xiàn)地區(qū)二級聯(lián)動,效果圖如下:

1.在aspx頁面中,拖入兩個DroDownList控件,代碼如下:

div>
  asp:DropDownList ID="s1" runat="server">/asp:DropDownList>
  asp:DropDownList ID="s2" runat="server">/asp:DropDownList>
  script language="javascript" type="text/javascript" src="er.js>/script>
  script language="javascript" type="text/javascript">setup()/script>
/div>

2.er.js代碼如下:

function Dsy() 
{ 
this.Items = {}; 
} 
Dsy.prototype.add = function(id,iArray) 
{ 
this.Items[id] = iArray; 
} 
Dsy.prototype.Exists = function(id) 
{ 
if(typeof(this.Items[id]) == "undefined") return false; 
return true; 
}

function change(v){ 
var str="0"; 
for(i=0;iv;i++){ str+=("_"+(document.getElementById(s[i]).selectedIndex-1));}; 
var ss=document.getElementById(s[v]); 
with(ss){ 
length = 0; 
options[0]=new Option(opt0[v],opt0[v]); 
if(v  document.getElementById(s[v-1]).selectedIndex>0 || !v) 
{ 
if(dsy.Exists(str)){ 
ar = dsy.Items[str]; 
for(i=0;iar.length;i++)options[length]=new Option(ar[i],ar[i]); 
if(v)options[1].selected = true; 
} 
} 
if(++vs.length){change(v);} 
} 
}

var dsy = new Dsy();

dsy.add("0",["北京市","天津市","河北省","山西省","內(nèi)蒙古自治區(qū)","遼寧省","吉林省","黑龍江省","上海市","江蘇省","浙江省","安徽省","福建省","江西省","山東省","河南省","湖北省","湖南省","廣東省","廣西自治區(qū)","海南省","重慶市","四川省","貴州省","云南省","西藏自治區(qū)","陜西省","甘肅省","青海省","寧夏回族自治區(qū)","新疆維吾爾自治區(qū)","香港特別行政區(qū)","澳門特別行政區(qū)","臺灣省","其它"]);

dsy.add("0_0",["北京","東城區(qū)","西城區(qū)","崇文區(qū)","宣武區(qū)","朝陽區(qū)","豐臺區(qū)","石景山區(qū)"," 海淀區(qū)(中關(guān)村)","門頭溝區(qū)","房山區(qū)","通州區(qū)","順義區(qū)","昌平區(qū)","大興區(qū)","懷柔區(qū)","平谷區(qū)","密云縣","延慶縣","其他"]); 
dsy.add("0_1",["和平區(qū)","河?xùn)|區(qū)","河西區(qū)","河北區(qū)","南開區(qū)","紅橋區(qū)","塘沽區(qū)","漢沽區(qū)","大港區(qū)","西青區(qū)","津南區(qū)","武清區(qū)","薊縣","寧河縣","靜海縣","其他"]); 
dsy.add("0_2",["石家莊市","張家口市","承德市","秦皇島市","唐山市","廊坊市","衡水市","滄州市","邢臺市","邯鄲市","保定市","其他"]); 
dsy.add("0_3",["太原市","朔州市","大同市","長治市","晉城市","忻州市","晉中市","臨汾市","呂梁市","運城市","其他"]); 
dsy.add("0_4",["呼和浩特市","包頭市","赤峰市","呼倫貝爾市","鄂爾多斯市","烏蘭察布市","巴彥淖爾市","興安盟","阿拉善盟","錫林郭勒盟","其他"]); 
dsy.add("0_5",["沈陽市","朝陽市","阜新市","鐵嶺市","撫順市","丹東市","本溪市","遼陽市","鞍山市","大連市","營口市","盤錦市","錦州市","葫蘆島市","其他"]); 
dsy.add("0_6",["長春市","白城市","吉林市","四平市","遼源市","通化市","白山市","延邊朝鮮族自治州","其他"]); 
dsy.add("0_7",["哈爾濱市","七臺河市","黑河市","大慶市","齊齊哈爾市","伊春市","佳木斯市","雙鴨山市","雞西市","大興安嶺地區(qū)(加格達(dá)奇)","牡丹江","鶴崗市","綏化市 ","其他"]); 
dsy.add("0_8",["黃浦區(qū)","盧灣區(qū)","徐匯區(qū)","長寧區(qū)","靜安區(qū)","普陀區(qū)","閘北區(qū)","虹口區(qū)","楊浦區(qū)","閔行區(qū)","寶山區(qū)","嘉定區(qū)","浦東新區(qū)","金山區(qū)","松江區(qū)","青浦區(qū)","南匯區(qū)","奉賢區(qū)","崇明縣","其他"]); 
dsy.add("0_9",["南京市","徐州市","連云港市","宿遷市","淮安市","鹽城市","揚州市","泰州市","南通市","鎮(zhèn)江市","常州市","無錫市","蘇州市","其他"]);
dsy.add("0_10",["杭州市","湖州市","嘉興市","舟山市","寧波市","紹興市","衢州市","金華市","臺州市","溫州市","麗水市","其他"]);
dsy.add("0_11",["合肥市","宿州市","淮北市","亳州市","阜陽市","蚌埠市","淮南市","滁州市","馬鞍山市","蕪湖市","銅陵市","安慶市","黃山市","六安市","巢湖市","池州市","宣城市","其他"]);
dsy.add("0_12",["福州市","南平市","莆田市","三明市","泉州市","廈門市","漳州市","龍巖市","寧德市","其他"]);
dsy.add("0_13",["南昌市","九江市","景德鎮(zhèn)市","鷹潭市","新余市","萍鄉(xiāng)市","贛州市","上饒市","撫州市","宜春市","吉安市","其他"]);
dsy.add("0_14",["濟南市","聊城市","德州市","東營市","淄博市","濰坊市","煙臺市","威海市","青島市","日照市","臨沂市","棗莊市","濟寧市","泰安市","萊蕪市","濱州市","菏澤市","其他"]);
dsy.add("0_15",["鄭州市","三門峽市","洛陽市","焦作市","新鄉(xiāng)市","鶴壁市","安陽市","濮陽市","開封市","商丘市","許昌市","漯河市","平頂山市","南陽市","信陽市","周口市","駐馬店市","其他"]);
dsy.add("0_16",["武漢市","十堰市","襄樊市","荊門市","孝感市","黃岡市","鄂州市","黃石市","咸寧市","荊州市","宜昌市","隨州市","恩施土家族苗族自治州","仙桃市","天門市","潛江市","神農(nóng)架林區(qū)","其他"]);
dsy.add("0_17",["長沙市","張家界市","常德市","益陽市","岳陽市","株洲市","湘潭市","衡陽市","郴州市","永州市","邵陽市","懷化市","婁底市","湘西土家族苗族自治州","其他"]);
dsy.add("0_18",["廣州市","清遠(yuǎn)市市","韶關(guān)市","河源市","梅州市","潮州市","汕頭市","揭陽市","汕尾市","惠州市","東莞市","深圳市","珠海市","中山市","江門市","佛山市","肇慶市","云浮市","陽江市","茂名市","湛江市","其他"]);
dsy.add("0_19",["南寧市","桂林市","柳州市","梧州市","貴港市","玉林市","欽州市","北海市","防城港市","崇左市","百色市","河池市","來賓市","賀州市","其他"]);
dsy.add("0_20",["??谑?,"三亞市","其他"]);
dsy.add("0_21",["渝中區(qū)","大渡口區(qū)","江北區(qū)","沙坪壩區(qū)","九龍坡區(qū)","南岸區(qū)","北碚區(qū)","萬盛區(qū)","雙橋區(qū)","渝北區(qū)","巴南區(qū)","萬州區(qū)","涪陵區(qū)","黔江區(qū)","長壽區(qū)","合川市","永川市","江津市","南川市","綦江縣","潼南縣","銅梁縣","大足縣","璧山縣","墊江縣","武隆縣","豐都縣","城口縣","開縣","巫溪縣","巫山縣","奉節(jié)縣","云陽縣","忠縣","石柱土家族自治縣","彭水苗族土家族自治縣","酉陽土家族苗族自治縣","秀山土家族苗族自治縣","其他"]);
dsy.add("0_22",["成都市","廣元市","綿陽市","德陽市","南充市","廣安市","遂寧市","內(nèi)江市","樂山市","自貢市","瀘州市","宜賓市","攀枝花市","巴中市","資陽市","眉山市","雅安","阿壩藏族羌族自治州","甘孜藏族自治州","涼山彝族自治州縣","其他"]);
dsy.add("0_23",["貴陽市","六盤水市","遵義市","安順市","畢節(jié)地區(qū)","銅仁地區(qū)","黔東南苗族侗族自治州","黔南布依族苗族自治州","黔西南布依族苗族自治州","其他"]);
dsy.add("0_24",["昆明市","曲靖市","玉溪市","保山市","昭通市","麗江市","普洱市","臨滄市","寧德市","德宏傣族景頗族自治州","怒江傈僳族自治州","楚雄彝族自治州","紅河哈尼族彝族自治州","文山壯族苗族自治州","大理白族自治州","迪慶藏族自治州","西雙版納傣族自治州","其他"]);
dsy.add("0_25",["拉薩市","那曲地區(qū)","昌都地區(qū)","林芝地區(qū)","山南地區(qū)","日喀則地區(qū)","阿里地區(qū)","其他"]);
dsy.add("0_26",["西安市","延安市","銅川市","渭南市","咸陽市","寶雞市","漢中市","安康市","商洛市","其他"]);
dsy.add("0_27",["蘭州市 ","嘉峪關(guān)市","金昌市","白銀市","天水市","武威市","酒泉市","張掖市","慶陽市","平?jīng)鍪?,"定西市","隴南市","臨夏回族自治州","甘南藏族自治州","其他"]);
dsy.add("0_28",["西寧市","海東地區(qū)","海北藏族自治州","黃南藏族自治州","玉樹藏族自治州","海南藏族自治州","果洛藏族自治州","海西蒙古族藏族自治州","其他"]);
dsy.add("0_29",["銀川市","石嘴山市","吳忠市","固原市","中衛(wèi)市","其他"]);
dsy.add("0_30",["烏魯木齊市","克拉瑪依市","喀什地區(qū)","阿克蘇地區(qū)","和田地區(qū)","吐魯番地區(qū)","哈密地區(qū)","塔城地區(qū)","阿勒泰地區(qū)","克孜勒蘇柯爾克孜自治州","博爾塔拉蒙古自治州","昌吉回族自治州 伊犁哈薩克自治州","巴音郭楞蒙古自治州","河子市","阿拉爾市","五家渠市","圖木舒克市","其他"]);
dsy.add("0_31",["香港","其他"]);
dsy.add("0_31",["澳門","其他"])
dsy.add("0_32",["臺灣","其他"])

var s=["s1","s2"]; 
var opt0 = ["請選擇","請選擇"]; 
function setup() 
{ 
for(i=0;is.length-1;i++) 
document.getElementById(s[i]).onchange=new Function("change("+(i+1)+")"); 
change(0); 
}

以上就是本文的全部內(nèi)容,希望對大家學(xué)習(xí)APS.NET程序設(shè)計有所幫助。

您可能感興趣的文章:
  • asp.net省市三級聯(lián)動的DropDownList+Ajax的三種框架(aspnet/Jquery/ExtJs)示例
  • ASP.NET MVC下拉框聯(lián)動實例解析
  • ASP.NET中DropDownList和ListBox實現(xiàn)兩級聯(lián)動功能
  • asp.net下使用AjaxPro實現(xiàn)二級聯(lián)動代碼
  • asp.net DropDownList 三級聯(lián)動下拉菜單實現(xiàn)代碼
  • asp.net兩級聯(lián)動(包含添加和修改)
  • 適用與firefox ASP.NET無刷新二級聯(lián)動下拉列表
  • ASP.NET實現(xiàn)級聯(lián)下拉框效果實例講解
  • ASP.NET Ajax級聯(lián)DropDownList實現(xiàn)代碼
  • jQuery+Asp.Net實現(xiàn)省市二級聯(lián)動功能的方法

標(biāo)簽:宜春 河南 汕頭 婁底 吉林 本溪 重慶 麗江

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《asp.net DropDownList實現(xiàn)二級聯(lián)動效果》,本文關(guān)鍵詞  asp.net,DropDownList,實現(xiàn),二級,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《asp.net DropDownList實現(xiàn)二級聯(lián)動效果》相關(guān)的同類信息!
  • 本頁收集關(guān)于asp.net DropDownList實現(xiàn)二級聯(lián)動效果的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章