Asp.net 2.0 一个简单的联动DropDownList示例(示例代码下载) [zhuan :D]
Posted on 2006-12-22 01:47 桦林 阅读(676) 评论(0) 编辑 收藏 举报(一). 示例图片
(二). 详细代码
1. 页面前台代码
1<body>
2 <form id="form1" runat="server">
3 <div>
4 <asp:Panel ID="Panel2" runat="server" BackColor="#FFC080" Font-Size="X-Large"
5 ForeColor="#C04000" Height="50px" Width="387px">
6 DropDownList 联动示例:</asp:Panel>
7 <asp:Panel ID="Panel1" runat="server" BackColor="#C0C0FF" Height="170px" Width="387px">
8 <br />
9 请选择类别:<br />
10 <br />
11
12 <asp:DropDownList ID="ddl1" runat="server">
13 </asp:DropDownList>
14 <asp:DropDownList ID="ddl2" runat="server">
15 </asp:DropDownList>
16 <asp:DropDownList ID="ddl3" runat="server">
17 </asp:DropDownList> <br />
18 <br />
19
20 <C1:RelatedList id="rl1" runat="server"></C1:RelatedList>
21 <asp:Button ID="btOK" runat="server" OnClick="Button1_Click" Text="取值" Width="67px" />
22 </asp:Panel>
23 </div>
24 </form>
25</body>
26
2 <form id="form1" runat="server">
3 <div>
4 <asp:Panel ID="Panel2" runat="server" BackColor="#FFC080" Font-Size="X-Large"
5 ForeColor="#C04000" Height="50px" Width="387px">
6 DropDownList 联动示例:</asp:Panel>
7 <asp:Panel ID="Panel1" runat="server" BackColor="#C0C0FF" Height="170px" Width="387px">
8 <br />
9 请选择类别:<br />
10 <br />
11
12 <asp:DropDownList ID="ddl1" runat="server">
13 </asp:DropDownList>
14 <asp:DropDownList ID="ddl2" runat="server">
15 </asp:DropDownList>
16 <asp:DropDownList ID="ddl3" runat="server">
17 </asp:DropDownList> <br />
18 <br />
19
20 <C1:RelatedList id="rl1" runat="server"></C1:RelatedList>
21 <asp:Button ID="btOK" runat="server" OnClick="Button1_Click" Text="取值" Width="67px" />
22 </asp:Panel>
23 </div>
24 </form>
25</body>
26
2.页面后台代码
1protected void Page_Load(object sender, EventArgs e)
2 {
3 if (!Page.IsPostBack)
4 {
5 ArrayList Category = new ArrayList();
6 Category.Add("ddl1");
7 Category.Add("ddl2");
8 Category.Add("ddl3");
9
10 //Method 1. 指定XML文件路径
11 rl1.DataSource = @"Category.xml";
12
13 ////Method 2. 指定数据源为DataSet对象
14 //DataSet ds = new DataSet();
15 //ds = GetData();
16 //rl1.DataSource = ds;
17
18
19 rl1.ListID = Category;
20 rl1.DataBind();
21
22 }
23 }
24 private DataSet GetData()
25 {
26 DataSet ds = new DataSet();
27 DataTable dt = new DataTable();
28 dt.Columns.Add(new DataColumn("Value", typeof(int)));
29 dt.Columns.Add(new DataColumn("Context", typeof(string)));
30 dt.Columns.Add(new DataColumn("ParentValue", typeof(int)));
31 DataRow drNew = dt.NewRow();
32 drNew["Value"] = 1;
33 drNew["Context"] = "水果";
34 drNew["ParentValue"] = 0;
35 dt.Rows.Add(drNew);
36
37 drNew = dt.NewRow();
38 drNew["Value"] = 2;
39 drNew["Context"] = "动物";
40 drNew["ParentValue"] = 0;
41 dt.Rows.Add(drNew);
42
43 drNew = dt.NewRow();
44 drNew["Value"] = 3;
45 drNew["Context"] = "葡萄";
46 drNew["ParentValue"] = 1;
47 dt.Rows.Add(drNew);
48
49 drNew = dt.NewRow();
50 drNew["Value"] = 7;
51 drNew["Context"] = "红葡萄";
52 drNew["ParentValue"] = 3;
53 dt.Rows.Add(drNew);
54
55 drNew = dt.NewRow();
56 drNew["Value"] = 8;
57 drNew["Context"] = "柴葡萄";
58 drNew["ParentValue"] = 3;
59 dt.Rows.Add(drNew);
60
61 drNew = dt.NewRow();
62 drNew["Value"] = 4;
63 drNew["Context"] = "桔子";
64 drNew["ParentValue"] = 1;
65 dt.Rows.Add(drNew);
66
67 drNew = dt.NewRow();
68 drNew["Value"] = 12;
69 drNew["Context"] = "红桔子";
70 drNew["ParentValue"] = 4;
71 dt.Rows.Add(drNew);
72
73 drNew = dt.NewRow();
74 drNew["Value"] = 13;
75 drNew["Context"] = "绿桔子";
76 drNew["ParentValue"] = 4;
77 dt.Rows.Add(drNew);
78
79 drNew = dt.NewRow();
80 drNew["Value"] = 5;
81 drNew["Context"] = "香蕉";
82 drNew["ParentValue"] = 1;
83 dt.Rows.Add(drNew);
84
85 drNew = dt.NewRow();
86 drNew["Value"] = 14;
87 drNew["Context"] = "马";
88 drNew["ParentValue"] = 2;
89 dt.Rows.Add(drNew);
90
91 drNew = dt.NewRow();
92 drNew["Value"] = 15;
93 drNew["Context"] = "红马";
94 drNew["ParentValue"] = 14;
95 dt.Rows.Add(drNew);
96
97 drNew = dt.NewRow();
98 drNew["Value"] = 16;
99 drNew["Context"] = "白马";
100 drNew["ParentValue"] = 14;
101 dt.Rows.Add(drNew);
102
103 drNew = dt.NewRow();
104 drNew["Value"] = 17;
105 drNew["Context"] = "蓝马";
106 drNew["ParentValue"] = 14;
107 dt.Rows.Add(drNew);
108
109 drNew = dt.NewRow();
110 drNew["Value"] = 18;
111 drNew["Context"] = "牛";
112 drNew["ParentValue"] = 2;
113 dt.Rows.Add(drNew);
114
115 ds.Tables.Add(dt);
116 return ds;
117 }
118 protected void Button1_Click(object sender, EventArgs e)
119 {
120 Response.Write("<script>alert('" + this.ddl1.SelectedItem.Text + "," + this.ddl2.SelectedItem.Text + "," +
121 this.ddl3.SelectedItem.Text + "');</script>");
122}
123
2 {
3 if (!Page.IsPostBack)
4 {
5 ArrayList Category = new ArrayList();
6 Category.Add("ddl1");
7 Category.Add("ddl2");
8 Category.Add("ddl3");
9
10 //Method 1. 指定XML文件路径
11 rl1.DataSource = @"Category.xml";
12
13 ////Method 2. 指定数据源为DataSet对象
14 //DataSet ds = new DataSet();
15 //ds = GetData();
16 //rl1.DataSource = ds;
17
18
19 rl1.ListID = Category;
20 rl1.DataBind();
21
22 }
23 }
24 private DataSet GetData()
25 {
26 DataSet ds = new DataSet();
27 DataTable dt = new DataTable();
28 dt.Columns.Add(new DataColumn("Value", typeof(int)));
29 dt.Columns.Add(new DataColumn("Context", typeof(string)));
30 dt.Columns.Add(new DataColumn("ParentValue", typeof(int)));
31 DataRow drNew = dt.NewRow();
32 drNew["Value"] = 1;
33 drNew["Context"] = "水果";
34 drNew["ParentValue"] = 0;
35 dt.Rows.Add(drNew);
36
37 drNew = dt.NewRow();
38 drNew["Value"] = 2;
39 drNew["Context"] = "动物";
40 drNew["ParentValue"] = 0;
41 dt.Rows.Add(drNew);
42
43 drNew = dt.NewRow();
44 drNew["Value"] = 3;
45 drNew["Context"] = "葡萄";
46 drNew["ParentValue"] = 1;
47 dt.Rows.Add(drNew);
48
49 drNew = dt.NewRow();
50 drNew["Value"] = 7;
51 drNew["Context"] = "红葡萄";
52 drNew["ParentValue"] = 3;
53 dt.Rows.Add(drNew);
54
55 drNew = dt.NewRow();
56 drNew["Value"] = 8;
57 drNew["Context"] = "柴葡萄";
58 drNew["ParentValue"] = 3;
59 dt.Rows.Add(drNew);
60
61 drNew = dt.NewRow();
62 drNew["Value"] = 4;
63 drNew["Context"] = "桔子";
64 drNew["ParentValue"] = 1;
65 dt.Rows.Add(drNew);
66
67 drNew = dt.NewRow();
68 drNew["Value"] = 12;
69 drNew["Context"] = "红桔子";
70 drNew["ParentValue"] = 4;
71 dt.Rows.Add(drNew);
72
73 drNew = dt.NewRow();
74 drNew["Value"] = 13;
75 drNew["Context"] = "绿桔子";
76 drNew["ParentValue"] = 4;
77 dt.Rows.Add(drNew);
78
79 drNew = dt.NewRow();
80 drNew["Value"] = 5;
81 drNew["Context"] = "香蕉";
82 drNew["ParentValue"] = 1;
83 dt.Rows.Add(drNew);
84
85 drNew = dt.NewRow();
86 drNew["Value"] = 14;
87 drNew["Context"] = "马";
88 drNew["ParentValue"] = 2;
89 dt.Rows.Add(drNew);
90
91 drNew = dt.NewRow();
92 drNew["Value"] = 15;
93 drNew["Context"] = "红马";
94 drNew["ParentValue"] = 14;
95 dt.Rows.Add(drNew);
96
97 drNew = dt.NewRow();
98 drNew["Value"] = 16;
99 drNew["Context"] = "白马";
100 drNew["ParentValue"] = 14;
101 dt.Rows.Add(drNew);
102
103 drNew = dt.NewRow();
104 drNew["Value"] = 17;
105 drNew["Context"] = "蓝马";
106 drNew["ParentValue"] = 14;
107 dt.Rows.Add(drNew);
108
109 drNew = dt.NewRow();
110 drNew["Value"] = 18;
111 drNew["Context"] = "牛";
112 drNew["ParentValue"] = 2;
113 dt.Rows.Add(drNew);
114
115 ds.Tables.Add(dt);
116 return ds;
117 }
118 protected void Button1_Click(object sender, EventArgs e)
119 {
120 Response.Write("<script>alert('" + this.ddl1.SelectedItem.Text + "," + this.ddl2.SelectedItem.Text + "," +
121 this.ddl3.SelectedItem.Text + "');</script>");
122}
123
(三).示例代码下载