前台代码:

 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="iframe_dropdown.aspx.cs" Inherits="Admin_iframe_dropdown" %>

 <!DOCTYPE html>

 <html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<div style="float: left" >
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" Width="105px" AutoPostBack="true"
OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" >
</asp:DropDownList>
<asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="true" Width="105px"
OnSelectedIndexChanged="DropDownList2_SelectedIndexChanged">
</asp:DropDownList>
<asp:DropDownList ID="DropDownList3" runat="server" AutoPostBack="true" Width="105px"
OnSelectedIndexChanged="DropDownList3_SelectedIndexChanged">
</asp:DropDownList>
<asp:DropDownList ID="DropDownList4" runat="server" AutoPostBack="true" Width="105px"
OnSelectedIndexChanged="DropDownList4_SelectedIndexChanged">
</asp:DropDownList>
</ContentTemplate>
</asp:UpdatePanel>
</div>
<div style="float: left" >
 </div> </form>
</body>
</html>

后台代码:

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using System.Text; public partial class Admin_iframe_dropdown : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{ if (!IsPostBack)
{
Db1DataBind();
Db2DataBind();
Db3DataBind();
Db4DataBind();
}
} protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
Db2DataBind();
Db3DataBind();
Db4DataBind();
}
protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
{
Db3DataBind();
Db4DataBind();
}
protected void DropDownList3_SelectedIndexChanged(object sender, EventArgs e)
{
Db4DataBind();
}
protected void DropDownList4_SelectedIndexChanged(object sender, EventArgs e)
{ } public void Db1DataBind()
{
string sqlStr = "select ModID,ModName,ParentID from Modules where ParentID=0000";
FillDropList(sqlStr, DropDownList1); }
public void Db2DataBind()
{
int PreID = Convert.ToInt32(DropDownList1.SelectedValue);
string sqlStr1 = "Select ModID,ModName,ParentID from Modules where ParentID='" + PreID.ToString() + "'";
FillDropList(sqlStr1, DropDownList2);
}
public void Db3DataBind()
{
int PreID = Convert.ToInt32(DropDownList2.SelectedValue);
string sqlStr2 = "Select ModID,ModName,ParentID from Modules where ParentID='" + PreID.ToString() + "'";
FillDropList(sqlStr2, DropDownList3);
}
public void Db4DataBind()
{
string PreID = Convert.ToString(DropDownList3.SelectedValue);
string sqlStr3 = "Select ModID,ModName,ParentID from Modules where ParentID='" + PreID.ToString() + "'";
FillDropList(sqlStr3, DropDownList4);
}
public void FillDropList(string SQLString, DropDownList drp)
{
SqlConnection connection = new SqlConnection("database=ZJExpress;server=(local);uid=sa;pwd=hellocheng");
SqlCommand cmd = new SqlCommand(SQLString, connection);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
sda.Fill(ds, "DropList");
drp.DataSource = ds.Tables["DropList"].DefaultView;
drp.DataTextField = ds.Tables["DropList"].Columns[].ColumnName;
drp.DataValueField = ds.Tables["DropList"].Columns[].ColumnName;
drp.DataBind();
}
}

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

05-08 08:09