二级联动
今天给一个学弟的二级联动学习,发现代码很老了!但很能说明问题!
废话不说!贴上代码:
先做一个ASP.NET控件,已方便使用 CityList.ascx:
1 <%@ Control Language="C#" AutoEventWireup="true" CodeFile="CityList.ascx.cs" Inherits="CityList" %>
2 <select name="shen" onChange="redirectff(this.options.selectedIndex)">
3 <option value="陕西省" selected>陕西省</option>
4 <option value="山东省">山东省</option>
5 <option value="河南省">河南省</option>
6 </select>
7
8 <select name="city" onChange="redirectfx(this.options.selectedIndex)">
9 <option value="西安">西安</option>
10 <option value="宝鸡">宝鸡</option>
11 <option value="济南">济南</option>
12 <option value="青岛">青岛</option>
13 <option value="郑州">郑州</option>
14 <option value="洛阳">洛阳</option>
15 <option value="南阳">南阳</option>
16
17 </select>
18
19 <font color="red">*</font>
20
21 <script language="JavaScript">
22 <!--
23 var groups=document.form1.shen.options.length;
24 var group=new Array(groups);
25 for (i=0; i<groups; i++)
26 group[i]=new Array();
27
28 group[0][0]=new Option("西安","西安");
29 group[0][1]=new Option("宝鸡","宝鸡");
30 group[1][0]=new Option("济南","济南");
31 group[1][1]=new Option("青岛","青岛");
32 group[2][0]=new Option("郑州","郑州");
33 group[2][1]=new Option("洛阳","洛阳");
34 group[2][2]=new Option("南阳","南阳");
35
36 //city
37 var tempff=document.form1.city;
38 function redirectff(x){
39 for (m=tempff.options.length-1;m>0;m--){
40 tempff.options[m]=null;
41 }
42 for (i=0;i<group[x].length;i++){
43 tempff.options[i]=new Option(group[x][i].text,group[x][i].value);
44 }
45 tempff.options[0].selected=true;
46 }
2 <select name="shen" onChange="redirectff(this.options.selectedIndex)">
3 <option value="陕西省" selected>陕西省</option>
4 <option value="山东省">山东省</option>
5 <option value="河南省">河南省</option>
6 </select>
7
8 <select name="city" onChange="redirectfx(this.options.selectedIndex)">
9 <option value="西安">西安</option>
10 <option value="宝鸡">宝鸡</option>
11 <option value="济南">济南</option>
12 <option value="青岛">青岛</option>
13 <option value="郑州">郑州</option>
14 <option value="洛阳">洛阳</option>
15 <option value="南阳">南阳</option>
16
17 </select>
18
19 <font color="red">*</font>
20
21 <script language="JavaScript">
22 <!--
23 var groups=document.form1.shen.options.length;
24 var group=new Array(groups);
25 for (i=0; i<groups; i++)
26 group[i]=new Array();
27
28 group[0][0]=new Option("西安","西安");
29 group[0][1]=new Option("宝鸡","宝鸡");
30 group[1][0]=new Option("济南","济南");
31 group[1][1]=new Option("青岛","青岛");
32 group[2][0]=new Option("郑州","郑州");
33 group[2][1]=new Option("洛阳","洛阳");
34 group[2][2]=new Option("南阳","南阳");
35
36 //city
37 var tempff=document.form1.city;
38 function redirectff(x){
39 for (m=tempff.options.length-1;m>0;m--){
40 tempff.options[m]=null;
41 }
42 for (i=0;i<group[x].length;i++){
43 tempff.options[i]=new Option(group[x][i].text,group[x][i].value);
44 }
45 tempff.options[0].selected=true;
46 }
47 </script>
做好控件,应用如页面:Default.aspx
1 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
2 <%@ Register TagPrefix="uc1" TagName="CityList" Src="CityList.ascx" %>
3
4 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
5
6 <html xmlns="http://www.w3.org/1999/xhtml" >
7 <head runat="server">
8 <title>無題のページ</title>
9 <script language=javascript type="text/javascript">
10
11 function btnOK2_Click()
12 {
13 //form1.<%# txtShow %>.value="你是"+document.form1.shen.value+document.form1.city.value;
14 document.form1.Text1.value="你是"+document.form1.shen.value+document.form1.city.value;
15 }
16
17 </script>
18 </head>
19 <body>
20 <form id="form1" runat="server">
21 <div>
22 <uc1:CityList id="CityList1" runat="server"></uc1:CityList>
23 <br />
24 <br />
25 <asp:TextBox ID="txtShow" runat="server"></asp:TextBox><br />
26 <input id="Text1" type="text" value="ddd"/>
27 <br />
28 <input id="Button1" type="Button" value="btnOKHTML" onclick="btnOK2_Click()" />
29 <asp:Button ID="btnOK" runat="server" Text="Click" Width="81px" OnClick="btnOK_Click" /></div>
30 </form>
31 </body>
2 <%@ Register TagPrefix="uc1" TagName="CityList" Src="CityList.ascx" %>
3
4 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
5
6 <html xmlns="http://www.w3.org/1999/xhtml" >
7 <head runat="server">
8 <title>無題のページ</title>
9 <script language=javascript type="text/javascript">
10
11 function btnOK2_Click()
12 {
13 //form1.<%# txtShow %>.value="你是"+document.form1.shen.value+document.form1.city.value;
14 document.form1.Text1.value="你是"+document.form1.shen.value+document.form1.city.value;
15 }
16
17 </script>
18 </head>
19 <body>
20 <form id="form1" runat="server">
21 <div>
22 <uc1:CityList id="CityList1" runat="server"></uc1:CityList>
23 <br />
24 <br />
25 <asp:TextBox ID="txtShow" runat="server"></asp:TextBox><br />
26 <input id="Text1" type="text" value="ddd"/>
27 <br />
28 <input id="Button1" type="Button" value="btnOKHTML" onclick="btnOK2_Click()" />
29 <asp:Button ID="btnOK" runat="server" Text="Click" Width="81px" OnClick="btnOK_Click" /></div>
30 </form>
31 </body>
32 </html>
Default.aspx.cs
1 using System;
2 using System.Data;
3 using System.Configuration;
4 using System.Web;
5 using System.Web.Security;
6 using System.Web.UI;
7 using System.Web.UI.WebControls;
8 using System.Web.UI.WebControls.WebParts;
9 using System.Web.UI.HtmlControls;
10
11 public partial class _Default : System.Web.UI.Page
12 {
13 protected void Page_Load(object sender, EventArgs e)
14 {
15 //if (!IsPostBack)
16 //{
17 // return;
18 //}
19 }
20 protected void btnOK_Click(object sender, EventArgs e)
21 {
22 string strSheng = Request.Form["shen"];
23 string strCity = Request.Form["city"];
24
25 txtShow.Text = "你是" + strSheng.ToString() + strCity.ToString() + "人";
26
27 }
28 }
29
2 using System.Data;
3 using System.Configuration;
4 using System.Web;
5 using System.Web.Security;
6 using System.Web.UI;
7 using System.Web.UI.WebControls;
8 using System.Web.UI.WebControls.WebParts;
9 using System.Web.UI.HtmlControls;
10
11 public partial class _Default : System.Web.UI.Page
12 {
13 protected void Page_Load(object sender, EventArgs e)
14 {
15 //if (!IsPostBack)
16 //{
17 // return;
18 //}
19 }
20 protected void btnOK_Click(object sender, EventArgs e)
21 {
22 string strSheng = Request.Form["shen"];
23 string strCity = Request.Form["city"];
24
25 txtShow.Text = "你是" + strSheng.ToString() + strCity.ToString() + "人";
26
27 }
28 }
29