RSS
热门关键字:  Linux  图形  项目管理  LAMP  java
当前位置 : 主页>开源技术>AJAX技术>列表

用ASP.NET AJAX实现无刷新多个DropDownList的动态联动

来源:中国开源社区 作者:sherman 时间:2007-09-18 点击:

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" >
<head runat="server">
<title>DDLajaxtitle>
head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server">
asp:ScriptManager>
<br />
&nbsp;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 />

字串5


<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>
字串8

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");

字串7

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

数据库

字串7

State_ID State_Name State_Country
----------- -------------------------------------------------- -------------
1 NewYork 1
2 Carifornia 1
3 Shanghai 2
4 Beijing 2
5 Tokyo 3
6 Osaka 3

最新评论共有 0 位网友发表了评论
发表评论
评论内容:不能超过250字,需审核,请自觉遵守互联网相关政策法规。
用户名: 密码:
匿名?
注册