Default2.aspx
字串3
DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 字串4
<html xmlns="http://www.w3.org/1999/xhtml" > 字串5
<head runat="server">
<title>DDLajaxtitle>
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 字串4
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; 字串5
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
DropDownList1.AutoPostBack = true;
DropDownList2.AutoPostBack = true; 字串7
if (!IsPostBack)
{
DropDownList1.Items.Add("Please Select Country");
DropDownList2.Items.Add("Please Select State");
字串5
DataRead("Select Country_ID, Country_Name from Country", DropDownList1); 字串8
DropDownList2.Enabled = false;
Label1.Text = "";
}
else if (DropDownList1.SelectedIndex == 0)
DropDownList2.Enabled = false;
} 字串7
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 = "";
} 字串1
protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
{
Label1.Text = "You select " this.DropDownList2.SelectedItem ", " this.DropDownList1.SelectedItem;
if (DropDownList2.SelectedIndex == 0)
Label1.Text = "";
} 字串8
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();
}
}
字串6
数据库
State_ID State_Name State_Country
----------- -------------------------------------------------- -------------
1 NewYork 1
2 Carifornia 1
3 Shanghai 2
4 Beijing 2
5 Tokyo 3
6 Osaka 3

