<body>
<form id="form1" runat="server">
<div>
<div style="text-align: center">
<table style="width: 458px; height: 24px">
<tr>
<td style="width: 100px">
<asp:DropDownList ID="danwei" runat="server" AutoPostBack="True" OnSelectedIndexChanged="danwei_SelectedIndexChanged"
Width="160px">
</asp:DropDownList></td>
<td style="width: 100px">
<asp:DropDownList ID="roadname" runat="server" AutoPostBack="True" Width="142px" OnSelectedIndexChanged="roadname_SelectedIndexChanged">
</asp:DropDownList></td>
<td style="width: 100px">
</td>
</tr>
</table>
<div style="text-align: center">
<table style="width: 318px; height: 22px">
<tr>
<td style="width: 100px">
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="按路名显示图片" /></td>
<td style="width: 100px">
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label></td>
</tr>
</table>
</div>
<br />
<div style="text-align: center">
<br />
<div style="text-align: center">
<table style="width: 356px; height: 83px">
<tr>
<td style="width: 100px">
<asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="按路段显示图片" /></td>
<td style="width: 100px">
<asp:DropDownList ID="roadnum" runat="server" Width="128px" OnSelectedIndexChanged="roadnum_SelectedIndexChanged" >
</asp:DropDownList></td>
</tr>
<tr>
<td style="width: 100px">
</td>
<td style="width: 100px">
<asp:Label ID="Label2" runat="server" Text="Label"></asp:Label></td>
</tr>
</table>
</div>
</div>
</div>
</div>
</form>
</body>
后台页
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
filldanwei();
}
}
public void filldanwei()
{
SqlConnection con = new SqlConnection("server=localhost;database=DQMQI;uid=sa;pwd=1234");
con.Open();
string sql = "select DepartmentName from TDQDepartment";
SqlCommand com = new SqlCommand(sql, con);
SqlDataReader dr = com.ExecuteReader();
while (dr.Read())
{
danwei.Items.Add(dr["DepartmentName"].ToString());
}
dr.Close();
con.Close();
luname();
lunum();
}
public void luname()
{
SqlConnection con = new SqlConnection("server=localhost;database=DQMQI;uid=sa;pwd=1234");
con.Open();
string sql = "select TDQRoadInfo.Department,TDQRoadInfo.RoadName from TDQRoadInfo,TDQDepartment where TDQDepartment.Department=TDQRoadInfo.Department and TDQDepartment.DepartmentName='" + danwei.SelectedItem.Text + "' ";
SqlCommand com = new SqlCommand(sql, con);
SqlDataReader dr = com.ExecuteReader();
while (dr.Read())
{
roadname.Items.Add(dr["RoadName"].ToString());
}
dr.Close();
con.Close();
}
protected void danwei_SelectedIndexChanged(object sender, EventArgs e)
{
roadname.Items.Clear();
roadnum.Items.Clear();
luname();
lunum();
}
public void lunum()
{
SqlConnection con = new SqlConnection("server=localhost;database=DQMQI;uid=sa;pwd=1234");
con.Open();
string sql = "select RoadSectionNum from RoadSectionInfo where RoadName='" + roadname.SelectedItem.Text + "' ";
SqlCommand com = new SqlCommand(sql, con);
SqlDataReader dr = com.ExecuteReader();
while (dr.Read())
{
roadnum.Items.Add(dr["RoadSectionNum"].ToString());
//Session["RoadSectionNum"] = roadnum.SelectedItem.Text;
Label1.Text = roadnum.SelectedItem.Text;
Session["RoadSectionNum"] = Label1.Text;
}
dr.Close();
con.Close();
}
protected void roadname_SelectedIndexChanged(object sender, EventArgs e)
{
roadnum.Items.Clear();
lunum();
}
其中 roadnum.Items.Clear(); 相当有用
他可以防止 数据的重复出现 即 把以前的内容 再在 dropdownlist 出现