单位上下级联动,从一个数据库里取单位,数据库内容如下:
DeptID PDeptID DeptName
1 0 一分公司
2 0 二分公司
3 0 三分公司
4 1 通信队
5 1 网络队
6 1 线路队
7 2 小车队
8 2 苗木队
9 3 调度
10 3 勘察
大概就是这么个情况,通过 DeptID 和 PDeptID 关系区分上下级,我用的控件为DropDownList1和DropDownList2,
DropDownList1用来显示一级单位,DropDownList2用来显示二级单位,当选择DropDownList1里的一个单位时候,DropDownList2里显
示相对应的单位,例如:DropDownList1里选择三分公司,那么DropDownList2里自动过滤出“调度”“勘察”。代码如下,但是DropDownList1和DropDownList2里的内容都没实现,请指教,谢谢。 protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{ SqlConnection conn = new SqlConnection(WebConfigurationManager.ConnectionStrings ["ythzhConnectionString"].ConnectionString);
conn.Open();
string sql = "select * from RTX_Dept where [PDeptID] = 0";
SqlDataAdapter sda = new SqlDataAdapter(sql, conn);
DataSet ds = new DataSet();
sda.Fill(ds); DropDownList1.DataTextField = "DeptName";
DropDownList1.DataValueField = "DeptID";
DropDownList1.DataBind();
conn.Close(); SqlConnection conn1 = new SqlConnection(WebConfigurationManager.ConnectionStrings["ythzhConnectionString"].ConnectionString);
conn1.Open(); Session["T_ID"] = DropDownList1.SelectedValue;
string sql1 = "select * from RTX_Dept where PDeptID = '" + Session["T_ID"] + "'";
SqlDataAdapter sda1 = new SqlDataAdapter(sql1, conn);
DataSet ds1 = new DataSet();
sda1.Fill(ds1); DropDownList2.DataTextField = "DeptName";
DropDownList2.DataValueField = "PDeptID";
DropDownList2.DataBind();
conn1.Close();
DropDownList2.Visible = true;
}
DeptID PDeptID DeptName
1 0 一分公司
2 0 二分公司
3 0 三分公司
4 1 通信队
5 1 网络队
6 1 线路队
7 2 小车队
8 2 苗木队
9 3 调度
10 3 勘察
大概就是这么个情况,通过 DeptID 和 PDeptID 关系区分上下级,我用的控件为DropDownList1和DropDownList2,
DropDownList1用来显示一级单位,DropDownList2用来显示二级单位,当选择DropDownList1里的一个单位时候,DropDownList2里显
示相对应的单位,例如:DropDownList1里选择三分公司,那么DropDownList2里自动过滤出“调度”“勘察”。代码如下,但是DropDownList1和DropDownList2里的内容都没实现,请指教,谢谢。 protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{ SqlConnection conn = new SqlConnection(WebConfigurationManager.ConnectionStrings ["ythzhConnectionString"].ConnectionString);
conn.Open();
string sql = "select * from RTX_Dept where [PDeptID] = 0";
SqlDataAdapter sda = new SqlDataAdapter(sql, conn);
DataSet ds = new DataSet();
sda.Fill(ds); DropDownList1.DataTextField = "DeptName";
DropDownList1.DataValueField = "DeptID";
DropDownList1.DataBind();
conn.Close(); SqlConnection conn1 = new SqlConnection(WebConfigurationManager.ConnectionStrings["ythzhConnectionString"].ConnectionString);
conn1.Open(); Session["T_ID"] = DropDownList1.SelectedValue;
string sql1 = "select * from RTX_Dept where PDeptID = '" + Session["T_ID"] + "'";
SqlDataAdapter sda1 = new SqlDataAdapter(sql1, conn);
DataSet ds1 = new DataSet();
sda1.Fill(ds1); DropDownList2.DataTextField = "DeptName";
DropDownList2.DataValueField = "PDeptID";
DropDownList2.DataBind();
conn1.Close();
DropDownList2.Visible = true;
}
PDeptID 是数字,sql里面无需加''
<%@ Page Language="C#" EnableViewState="true" AutoEventWireup="true" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
System.Data.DataTable dt = new System.Data.DataTable();
protected void Page_Load(object sender, EventArgs e)
{
//下面的数据只是例子,你可以下哦那个数据库读取
dt.Columns.Add(new System.Data.DataColumn("DeptID", typeof(System.Int32)));
dt.Columns.Add(new System.Data.DataColumn("PDeptID", typeof(System.Int32)));
dt.Columns.Add(new System.Data.DataColumn("DeptName", typeof(System.String)));
dt.Rows.Add(new Object[] { 1, 0, "一分公司" });
dt.Rows.Add(new Object[] { 2, 0, "二分公司" });
dt.Rows.Add(new Object[] { 3, 0, "三分公司" });
dt.Rows.Add(new Object[] { 4, 1, "通信队" });
dt.Rows.Add(new Object[] { 5, 1, "网络队" });
dt.Rows.Add(new Object[] { 6, 1, "线路队" });
dt.Rows.Add(new Object[] { 7, 2, "小车队" });
dt.Rows.Add(new Object[] { 8, 2, "苗木队" });
dt.Rows.Add(new Object[] { 9, 3, "调度" });
dt.Rows.Add(new Object[] { 10, 3, "勘察" });
if (!Page.IsPostBack)
{
System.Data.DataView dv = dt.DefaultView;
dv.RowFilter = "PDeptID=0";
DropDownList1.DataSource = dv;
DropDownList1.DataTextField = "DeptName";
DropDownList1.DataValueField = "DeptID";
DropDownList1.DataBind();
System.Data.DataView dv2 = dt.DefaultView;
dv2.RowFilter = "PDeptID=" + DropDownList1.SelectedValue;
DropDownList2.DataSource = dv2;
DropDownList2.DataTextField = "DeptName";
DropDownList2.DataValueField = "DeptID";
DropDownList2.DataBind();
}
} protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
System.Data.DataView dv2 = dt.DefaultView;
dv2.RowFilter = "PDeptID=" + DropDownList1.SelectedValue;
DropDownList2.DataSource = dv2;
DropDownList2.DataTextField = "DeptName";
DropDownList2.DataValueField = "DeptID";
DropDownList2.DataBind();
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
</asp:DropDownList>
<asp:DropDownList ID="DropDownList2" runat="server">
</asp:DropDownList>
</form>
</body>
</html>
protected void Page_Load(object sender, EventArgs e)
{ SqlConnection conn = new SqlConnection(WebConfigurationManager.ConnectionStrings["ythzhConnectionString"].ConnectionString);
conn.Open();
string sql = "select * from RTX_Dept where [PDeptID] = 0";
SqlDataAdapter sda = new SqlDataAdapter(sql, conn);
DataSet ds = new DataSet();
sda.Fill(ds); DropDownList1.DataTextField = "DeptName";
DropDownList1.DataValueField = "DeptID";
DropDownList1.DataBind();
conn.Close(); }
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{ SqlConnection conn1 = new SqlConnection(WebConfigurationManager.ConnectionStrings["ythzhConnectionString"].ConnectionString);
conn1.Open(); Session["T_ID"] = DropDownList1.SelectedValue;
string sql1 = "select * from RTX_Dept where PDeptID = '" + Session["T_ID"] + "'";
SqlDataAdapter sda1 = new SqlDataAdapter(sql1, conn1);
DataSet ds1 = new DataSet();
sda1.Fill(ds1); DropDownList2.DataTextField = "DeptName";
DropDownList2.DataValueField = "PDeptID";
DropDownList2.DataBind();
conn1.Close();
DropDownList2.Visible = true;
}
2,看清楚我的例子的顺序,if(!Page.IsPostBack)
3.sql语句,不要加''不是早告诉你了吗
string sql1 = "select * from RTX_Dept where PDeptID =" + Session["T_ID"];
试了你告诉我的方法直接拷贝粘贴,另存为xx.aspx直接浏览,好使。
protected void Page_Load(object sender, EventArgs e)
{ string sql = "select * from RTX_Dept";
SqlConnection conn = new SqlConnection(WebConfigurationManager.ConnectionStrings["ythzhConnectionString"].ConnectionString);
SqlCommand scomm = new SqlCommand(sql, conn);
conn.Open();
scomm.ExecuteNonQuery(); SqlDataReader dt;
dt = scomm.ExecuteReader(); if (!Page.IsPostBack)
{
System.Data.DataView dv = dt.DefaultView;
dv.RowFilter = "PDeptID=0";
DropDownList1.DataSource = dv;
DropDownList1.DataTextField = "DeptName";
DropDownList1.DataValueField = "DeptID";
DropDownList1.DataBind();
System.Data.DataView dv2 = dt.DefaultView;
dv2.RowFilter = "PDeptID=" + DropDownList1.SelectedValue;
DropDownList2.DataSource = dv2;
DropDownList2.DataTextField = "DeptName";
DropDownList2.DataValueField = "PDeptID";
DropDownList2.DataBind();
} }