DropDownList四级联动

前台代码:

 

 1 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="iframe_dropdown.aspx.cs" Inherits="Admin_iframe_dropdown" %>
 2 
 3 
 4 <!DOCTYPE html>
 5 
 6 <html xmlns="http://www.w3.org/1999/xhtml">
 7 <head runat="server">
 8     <title></title>
 9 </head>
10 <body>
11     <form id="form1" runat="server">
12      <asp:ScriptManager ID="ScriptManager1" runat="server" />
13         <div style="float: left" >
14             <asp:UpdatePanel ID="UpdatePanel1" runat="server">
15                 <ContentTemplate>
16                     <asp:DropDownList ID="DropDownList1" runat="server" Width="105px" AutoPostBack="true"
17                         OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" >
18                     </asp:DropDownList>
19                     <asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="true" Width="105px"
20                         OnSelectedIndexChanged="DropDownList2_SelectedIndexChanged">
21                     </asp:DropDownList>
22                     <asp:DropDownList ID="DropDownList3" runat="server" AutoPostBack="true" Width="105px" 
23                         OnSelectedIndexChanged="DropDownList3_SelectedIndexChanged">
24                     </asp:DropDownList>
25                     <asp:DropDownList ID="DropDownList4" runat="server" AutoPostBack="true" Width="105px" 
26                         OnSelectedIndexChanged="DropDownList4_SelectedIndexChanged">
27                     </asp:DropDownList>
28                 </ContentTemplate>
29             </asp:UpdatePanel>
30         </div>
31         <div style="float: left" >
32              </div>
33   
34     </form>
35 </body>
36 </html>

后台代码:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Web;
 5 using System.Web.UI;
 6 using System.Web.UI.WebControls;
 7 using System.Data;
 8 using System.Data.SqlClient;
 9 using System.Collections;
10 using System.Text;
11 
12 
13 public partial class Admin_iframe_dropdown : System.Web.UI.Page
14 {
15     protected void Page_Load(object sender, EventArgs e)
16     {
17         
18         if (!IsPostBack)
19         {
20             Db1DataBind();
21             Db2DataBind();
22             Db3DataBind();
23             Db4DataBind();
24         }
25     }
26  
27     protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
28     {
29         Db2DataBind();
30         Db3DataBind();
31         Db4DataBind();
32     }
33     protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
34     {
35         Db3DataBind();
36         Db4DataBind();
37     }
38     protected void DropDownList3_SelectedIndexChanged(object sender, EventArgs e)
39     {
40         Db4DataBind();
41     }
42     protected void DropDownList4_SelectedIndexChanged(object sender, EventArgs e)
43     {
44       
45     }
46 
47     public void Db1DataBind()
48     {
49         string sqlStr = "select ModID,ModName,ParentID from Modules where ParentID=0000";
50         FillDropList(sqlStr, DropDownList1);
51 
52     }
53     public void Db2DataBind()
54     {
55         int PreID = Convert.ToInt32(DropDownList1.SelectedValue);
56         string sqlStr1 = "Select ModID,ModName,ParentID from Modules where ParentID='" + PreID.ToString() + "'";
57         FillDropList(sqlStr1, DropDownList2);
58     }
59     public void Db3DataBind()
60     {
61         int PreID = Convert.ToInt32(DropDownList2.SelectedValue);
62         string sqlStr2 = "Select ModID,ModName,ParentID from Modules where ParentID='" + PreID.ToString() + "'";
63         FillDropList(sqlStr2, DropDownList3);
64     }
65     public void Db4DataBind()
66     {
67         string PreID = Convert.ToString(DropDownList3.SelectedValue);
68         string sqlStr3 = "Select ModID,ModName,ParentID from Modules where ParentID='" + PreID.ToString() + "'";
69         FillDropList(sqlStr3, DropDownList4);
70     }
71     public void FillDropList(string SQLString, DropDownList drp)
72     {
73         SqlConnection connection = new SqlConnection("database=ZJExpress;server=(local);uid=sa;pwd=hellocheng");
74         SqlCommand cmd = new SqlCommand(SQLString, connection);
75         SqlDataAdapter sda = new SqlDataAdapter(cmd);
76         DataSet ds = new DataSet();
77         sda.Fill(ds, "DropList");
78         drp.DataSource = ds.Tables["DropList"].DefaultView;
79         drp.DataTextField = ds.Tables["DropList"].Columns[1].ColumnName;
80         drp.DataValueField = ds.Tables["DropList"].Columns[0].ColumnName;
81         drp.DataBind();
82     }
83 }

 

参考链接http://blog.csdn.net/wxd_860825/article/details/4563368

posted @ 2014-08-15 16:49  chaha  阅读(221)  评论(0编辑  收藏  举报