asp.net DropDownList to achieve two-level linkage effect _ practical skills

Source: Internet
Author: User

Recently in the press release system, the use of the two-level linkage, I put the use of methods recorded, so that in the future to check and help novice friends. Here is the effect chart:

Here's how to implement it:

1. In the. aspx page, drag into two Drodownlist controls. The code is as follows:

<tr>
  <td> News style:</td>
  <td><asp:dropdownlist id= "DropDownList1" runat= "Server" autopostback= "True" onselectedindexchanged= "dropdownlist1_selectedindexchanged" ></asp:DropDownList>
  </td>
</tr>
<tr>
  <td> News type:</td>
  <td><asp:d Ropdownlist id= "DropDownList2" runat= "Server" >/asp:DropDownList>
  </td>
</tr>

The code in the 2.aspx.cs page is as follows:

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 ("News Update"));
    DROPDOWNLIST1.ITEMS.ADD (New ListItem ("Open Government"));
  DROPDOWNLIST1.ITEMS.ADD (New ListItem ("online Office")); } protected void DropdownList2 () {switch (dropdownlist1.selectedvalue) {case "news": DROPDOWNL Ist2.
        Items.clear ();
        DROPDOWNLIST2.ITEMS.ADD (New ListItem ("Work dynamic"));
        DROPDOWNLIST2.ITEMS.ADD (New ListItem ("Notice of publicity");
        DROPDOWNLIST2.ITEMS.ADD (New ListItem ("Economic News"));
        DROPDOWNLIST2.ITEMS.ADD (New ListItem ("News in the province")); DROPDOWNLIST2.ITEMS.ADD (New ListItem ("Hot News"));
        Break
        Case "Open Government": DropDownList2.Items.Clear ();
        DROPDOWNLIST2.ITEMS.ADD (New ListItem ("leading Group"));
        DROPDOWNLIST2.ITEMS.ADD (New ListItem ("institution setting"));
        DROPDOWNLIST2.ITEMS.ADD (New ListItem ("Planning Plan"));
      Break
        Case "Online": DropDownList2.Items.Clear ();
        DROPDOWNLIST2.ITEMS.ADD (New ListItem ("post responsibility"));
        DROPDOWNLIST2.ITEMS.ADD (New ListItem ("process"));
    Break } protected void Page_Load (object sender, EventArgs e) {if (!
      IsPostBack) {DropdownList1 ();
    DropdownList2 ();
  } protected void DropDownList1_SelectedIndexChanged (object sender, EventArgs e) {DropdownList2 ();
 }
}


Use Drodownlist and JS to achieve regional two-level linkage, the results are as follows:

1. In the ASPX page, drag in two Drodownlist controls, the following code:

<div>
  <asp:dropdownlist id= "S1" runat= "Server" ></asp:DropDownList>
  <asp:d Ropdownlist 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>

The 2.er.js code is as follows:

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;i<v;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;i<ar.length;i++) options[length]=new Option (Ar[i],ar[i]); 
if (v) options[1].selected = true; 
} if (++v<s.length) {change (v);}

} var dsy = new Dsy (); Dsy.add ("0", ["Beijing", "Tianjin", "Hebei province", "Shanxi province", "Inner Mongolia Autonomous Region", "Liaoning province", "Jilin Province", "Heilongjiang province", "Shanghai City", "Jiangsu province", "Zhejiang province", "Anhui province", "Fujian province", "Jiangxi province", "Shandong province", " Henan province "," Hubei province "," Hunan province "," Guangdong province "," Guangxi Autonomous Region, "Hainan Province", "Chongqing city", "Sichuan province", "Guizhou province", "Yunnan province", "Tibet Autonomous Region", "Shaanxi province", "Gansu province", "Qinghai province", "Ningxia Hui Autonomous Region", "Xinjiang Uygur Autonomous Region", "

Hong Kong Special Administrative Region "," Macao Special Administrative Region "," Taiwan Province "," other "]; Dsy.adD ("0_0", ["Beijing", "Dongcheng District", "Xicheng District," Chongwen District "," Xuan Wu qu "," Chaoyang District "," Fengtai District "," Shijingshan "," Haidian District "," Tongzhou District "," Fangshan "," Shunyi District "," Changping District "," Daxing District "," Huairou " 
, "Pinggu District", "Miyun County", "Yanqing County", "other"]); Dsy.add ("0_1", "Zone of Peace", "Hedong District", "Hexi District", "Hebei District", "Nankai District", "Hongqiao District", "Tanggu District", "Hangu District", "Grand Port District", "Xiqing District", "Jinnan District", "Wuqing District", "Jixian", "Ninghe County", "Jinghai County", "other"] 
; 
Dsy.add ("0_2", ["Shijiazhuang City", "Zhangjiakou", "Chengde", "Qinhuangdao Shi", "Tangshan", "Langfang", "Hengshui", "Cangzhou", "Xingtai", "Hanhan", "Baoding", "other"]); 
Dsy.add ("0_3", ["Taiyuan City", "Shuozhou", "Datong", "Changzhi", "Jincheng", "Xinzhou", "Jinzhong", "Linfen", "Luliang City", "Yun City", "other"]); 
Dsy.add ("0_4", ["Hohhot City", "Baotou", "Chifeng", "Holonbuyr", "Eerduosi", "Wulanchabu City", "Bayan nur City", "Hinggan League", "Alxa League", "Xilin", "other"]); 
Dsy.add ("0_5", ["Shenyang City", "] Chaoyang", "Fuxin", "Tieling", "Fushun", "Dandong", "Benxi", "Liaoyang", "Anshan", "Dalian", "Yingkou", "Panjin", "Jinzhou", "Huludao", "other"]; 
Dsy.add ("0_6", ["Changchun", "White City", "Jilin", "Siping", "Liaoyuan", "Tonghua", "Bss", "Yanbian Korean Autonomous Prefecture", "Other"]); Dsy.add ("0_7", ["Harbin City", "Qitaihe Shi", "Heihe", "Daqing", "Qiqihar", "Yichun", "Jiamusi", "Shuangyashan", "Jixi", "Daxing ' anling (Jagdaqi)", "Mudanjiang", "Hegang", "Suihua", " 
Other "]); Dsy.add ("0_8", ["Huangpu District", "] Luwan District", "Xuhui District", "Changning", "Jingan", "Putuo", "Zhabei", "Hongkou", "Yangpu", "Minhang", "Baoshan", "Jiading", "Pudong New Area", "Jinshan", "Songjiang", "Qingpu 
"," Nanhui "," Fengxian "," Chongming County "," other "]); Dsy.add ("0_9", ["Nanjing", "Xuzhou", "LianyungangCity "," Suqian "," Huaian "," Yancheng "," Yangzhou "," Taizhou "," Nantong "," Zhenjiang "," Changzhou "," Wuxi "," Suzhou "," other "]);
Dsy.add ("0_10", ["Hangzhou City", "Huzhou", "Jiaxing", "Zhoushan", "Ningbo", "Shaoxing", "Quzhou", "Jinhua", "Taizhou", "Wenzhou", "Lishui", "other"]); Dsy.add ("0_11", ["Hefei", "Suzhou", "Huaibei", "Bozhou", "Fuyang", "Bangbu", "Huainan", "Chuzhou", "Maanshan", "Wuhu", "Tongling", "Anqing", "Huangshan", "Liu'an", "Chaohu", "
Chizhou "," Xuancheng "," other "]);
Dsy.add ("0_12", ["Fuzhou", "Nanping", "Putian", "Sanming", "Quanzhou", "Xiamen", "Zhangzhou", "Longyan", "Ningde", "other"]);
Dsy.add ("0_13", ["Nanchang", "Jiujiang", "Jingdezhen", "Yingtan", "Xinyu", "Pingxiang", "Ganzhou", "Shangrao", "Fuzhou", "Yichun", "Jian", "other"]); Dsy.add ("0_14", ["Jinan", "Liaocheng", "Dezhou", "Dongying", "Zibo", "Weifang", "Yantai", "Weihai", "Qingdao", "Rizhao", "Linyi", "Zaozhuang", "Jining", "Taian", "Laiwu", "Binzhou
"," Heze "," other "]); Dsy.add ("0_15", ["Zhengzhou", "Sanmenxia Shi", "Luoyang", "Jiaozuo", "Xinxiang", "Hebi", "Anyang", "Puyang", "Kaifeng", "Shangqiu", "Xuchang", "Leihe", "Pingdingshan", "Nanyang", "Xinyang", "
Zhoukou "," Zhumadian "," other "]); Dsy.add ("0_16", ["Wuhan City", "Shiyan", "Xiangfan", "Jingmen", "Xiaogan", "Huanggang", "Ezhou", "Huangshi", "Xianning", "Jinzhou", "Yichang", "Suizhou", "Enshi", "Xiantao", "
Tianmen "," Qianjiang "," Shennongjia "," other "]); Dsy.add ("0_17", ["Changsha", "Zhangjiajie", "Changde", "Yiyang", "Yueyang", "Zhuzhou", "Xiangtan", "Hengyang", "Chenzhou", "Yongzhou", "Shaoyang", "Huaihua", "Loudi", "Xiangxi Tujia", "
Other "]); Dsy.add ("0_18", ["Guangzhou", "Qingyuan City", "Shaoguan", "RiverSOURCE City "," Meizhou "," Chaozhou "," Shantou "," Jieyang "," Shanwei "," Huizhou "," Dongguan "," Shenzhen "," Zhuhai "," Zhongshan "," Jiangmen "," Foshan "," Zhaoqing "," Yunfu "," Yangjiang "," Maoming "," Zhanjiang "," other "
]);
Dsy.add ("0_19", ["Nanning", "Guilin", "Liuzhou", "Wuzhou", "Guigang", "Yulin", "Qinzhou", "Beihai", "Fangchenggang", "Chongzuo City", "Baise", "Hechi", "Guest City", "Hezhou", "other"];
Dsy.add ("0_20", ["Haikou", "Sanya", "other"]); Dsy.add ("0_21", ["Yuzhong District", "Dadukou District", "Jiangbei District", "Shapingba District", "Jiulongpo District", "Nanan District", "Beibei", "Wancheng District", "Shuangqiao District", "Yubei District", "Banan", "Wanxian District", "Fuling District", "Qianjiang District", "Changshou District", " Hechuan "," Yongchuan "," Jiangjin "," Nanchuan "," Qijiang County "," Tongnan County "," Tongliang County "," Dazu County "," Bishan County "," Dianjiang County "," Wulong County "," Fengdou County "," Chengkou County "," Kai "," Wuxi County "," Wushan County "," Fengjie County "," Yunyang County "," Zhongxian "
, "Shizhu County", "Pengshui County", "Youyang County", "Xiushan County", "other"]); Dsy.add ("0_22", ["Chengdu City", "] Guangyuan", "Mianyang", "Deyang", "Nanchong", "Guangan", "Suining", "Neijiang", "Leshan", "Zigong", "Luzhou", "Yibin", "Panzhihua", "Bazhong", "Ziyang", "
Meishan "," Yaan "," Aba Tibetan and Qiang Autonomous Prefecture "," Ganzi Tibetan Autonomous Prefecture "," Liangshan Yi Autonomous Prefecture County "," other "]);
Dsy.add ("0_23", ["Guiyang", "Liupanshui", "Zunyi", "Anshun", "Bijie Prefecture", "Tongren Prefecture", "Southeast Guizhou Miao and Dong Autonomous Prefecture", "Qiannan Buyei and Miao Autonomous Prefecture", "Southwest Guizhou Buyei and Miao Autonomous Prefecture", "Other"]); Dsy.add ("0_24", ["Kunming", "Qujing", "Yuxi", "Baoshan", "Zhaotong", "Lijiang City", "Puer City", "Lincang City", "Ningde", "Dehong Dai and Jingpo Autonomous Prefecture", "Nujiang Lisu Autonomous Prefecture", "Chuxiongzhou", "
Honghe "," Wenshan "," Dali Bai "," Dêqên Tibetan Autonomous Prefecture "," Xishuangbanna Dai Autonomous Prefecture "," Other "]; Dsy.add ("0_25", ["Lasa", "Nagchu", "ChangAll regions, "Linzhi", "Shannan area", "Xigazê Prefecture", "Ali", "other"]);
Dsy.add ("0_26", ["Xi ' an city", "Yanan", "Tongchuan", "Weinan", "Xianyang", "Baoji", "Hanzhong", "Ankang", "Shangluo", "other"]); Dsy.add ("0_27", "Lanzhou City", "Jiayuguan", "Jinchang", "Baiyin", "Tianshui", "Wuwei", "Jiuquan", "Zhangye", "Qingyang City", "Pingliang", "Dingxi City", "Longnan", "Linxia Hui Autonomous Prefecture", "Gannan Tibetan Autonomous Prefecture", "
Other "]);
Dsy.add ("0_28", ["Xining City", "Haidong Prefecture", "Haibei Tibetan Autonomous Prefecture", "Huangnan Tibetan Autonomous Prefecture", "Yushu Tibetan Autonomous Prefecture", "Hainan Tibetan Autonomous Prefecture", "Golog Tibetan Autonomous Prefecture", "Sea Simon Haixi Tibetan Autonomous Prefecture", "Other"]);
Dsy.add ("0_29", ["Yinchuan", "Shizuishan", "Wuzhong", "Guyuan", "Zhongwei City", "other"]); Dsy.add ("0_30", ["Urumqi", "Karamay", "Kashgar", "Aksu", "Hotan Prefecture", "Turpan", "Hami", "Tacheng", "Altay", "Kizilsu Kirgiz Zizhizhou", "Bortala Mongol Autonomous Prefecture",
"Isahasak in the Changji Hui Autonomous Prefecture", "Bainguo", "River City", "Alar City", "five Canals and cities", "Tumshuk City", "other"];
Dsy.add ("0_31", ["Hong Kong", "other"]); 
Dsy.add ("0_31", ["Macao", "Other"]) Dsy.add ("0_32", ["Taiwan", "other"]) var s=["S1", "S2"]; 
var opt0 = ["Please select", "Please select"]; function Setup () {for (i=0;i<s.length-1;i++) document.getElementById (S[i]). Onchange=new function ("Change (i+1) 
+")"); 
Change (0);
 }

The above is the entire content of this article, I hope to learn aps.net program design help.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.