Default2.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DDLajax.aspx.cs" Inherits="Default2" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>DDLajax</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<br />
</div>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"
Width="233px">
</asp:DropDownList><br />
<asp:DropDownList ID="DropDownList2" runat="server" OnSelectedIndexChanged="DropDownList2_SelectedIndexChanged"
Width="233px">
</asp:DropDownList><br />
<br />
<asp:Label ID="Label1" runat="server" Text="Label" Width="234px"></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
<br />
<br />
<br />
<br />
<marquee>This is a trace message that marces across the bottom of the screen.</marquee>
</form>
</body>
</html>
Default2.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
DropDownList1.AutoPostBack = true;
DropDownList2.AutoPostBack = true;
if (!IsPostBack)
{
DropDownList1.Items.Add("Please Select Country");
DropDownList2.Items.Add("Please Select State");
DataRead("Select Country_ID, Country_Name from Country", DropDownList1);
DropDownList2.Enabled = false;
Label1.Text = "";
}
else if (DropDownList1.SelectedIndex == 0)
DropDownList2.Enabled = false;
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList2.Items.Clear();
DropDownList2.Items.Add("Please Select State");
if (DropDownList1.SelectedIndex == 0)
DropDownList2.Enabled = false;
else
{
DropDownList2.Enabled = true;
DataRead("Select State_ID, State_Name from State where State_Country = " + DropDownList1.SelectedValue, DropDownList2);
}
Label1.Text = "";
}
protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
{
Label1.Text = "You select " + this.DropDownList2.SelectedItem + ", " + this.DropDownList1.SelectedItem;
if (DropDownList2.SelectedIndex == 0)
Label1.Text = "";
}
protected void DataRead(string Sqlstr, DropDownList ddl)
{
SqlConnection conn = new SqlConnection("server=localhost;database=DropDownList;uid=sa;pwd=sa");
conn.Open();
SqlCommand com = new SqlCommand(Sqlstr, conn);
SqlDataReader sr = null;
sr = com.ExecuteReader();
while (sr.Read())
ddl.Items.Add(new ListItem(sr[1].ToString(),sr[0].ToString()));
conn.Close();
}
}
数据库
State_ID State_Name State_Country
----------- -------------------------------------------------- -------------
1 NewYork 1
2 Carifornia 1
3 Shanghai 2
4 Beijing 2
5 Tokyo 3
6 Osaka 3
(6 row(s) affected)
Country_ID Country_Name
----------- --------------------------------------------------
1 US
2 China
3 Japan
(3 row(s) affected)


浙公网安备 33010602011771号