namespace WebApplicationDlh
{
using System;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClIEnt;
using Db;
/// <summary>
/// Area 的摘要說明。
/// </summary>
public class Area : System.Web.UI.UserControl
{
protected System.Web.UI.WebControls.DropDownList ddlProvince;
protected System.Web.UI.WebControls.DropDownList ddlCity;
public string strProvince
{
get
{
return ddlProvince.SelectedItem.Value;
}
set
{
ddlProvince.SelectedItem.Value = value;
}
}
public string strCity
{
get
{
return ddlCity.SelectedItem.Value;
}
set
{
ddlCity.SelectedItem.Value = value;
}
}
private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{
ddlProvinceBind();
ddlCity.Items.Insert(0,"-請選擇城市-");
}
}
private void ddlProvinceBind()
{
Db.Area myArea = new Db.Area();
SqlDataReader dr = myArea.GetAllProvince();
if(dr.Read())
{
ddlProvince.DataSource = dr;
ddlProvince.DataTextFIEld = "Province";
ddlProvince.DataValueFIEld = "Province";
ddlProvince.DataBind();
ddlProvince.Items.Insert(0,"-請選擇省份-");
}
}
#region Web 窗體設計器生成的代碼
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 該調用是 ASP.Net Web 窗體設計器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 設計器支持所需的方法 - 不要使用代碼編輯器
/// 修改此方法的內容。
/// </summary>
private void InitializeComponent()
{
this.ddlProvince.SelectedIndexChanged += new System.EventHandler(this.ddlProvince_SelectedIndExchanged);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void ddlProvince_SelectedIndExchanged(object sender, System.EventArgs e)
{
if(ddlProvince.SelectedItem.Value == "-請選擇省份-")
{
ddlCity.Items.Clear();
ddlCity.Items.Insert(ddlProvince.SelectedIndex,"-請選擇城市-");
}
else
{
Db.Area myArea = new Db.Area();
SqlDataReader dr = myArea.GetAllCityWhereProvince(ddlProvince.SelectedItem.Value);
if(dr.Read())
{
ddlCity.DataSource = dr;
ddlCity.DataTextFIEld = "City";
ddlCity.DataValueFIEld = "ID";
ddlCity.DataBind();
}
}
}
}
}
=============================================================〉
using System;
using System.Data;
using System.Data.SqlClIEnt;
using System.Configuration;
namespace Db
{
/// <summary>
/// Area 的摘要說明。
/// </summary>
public class Area:Base
{
public Area()
{
//
// TODO: 在此處添加構造函數邏輯
//
}
public SqlDataReader GetAllProvince()
{
strSp = "Sp_Area_SelectAll";
drSelectAll(strSp);
return dr;
}
public SqlDataReader GetAllCityWhereProvince(string a)
{
conn = new SqlConnection(ConfigurationSettings.APPSettings["dsn"]);
cmd = new SqlCommand("Sp_Area_SelectAllWhereProvince",conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@Province",SqlDbType.VarChar,50).Value = a.ToString();
conn.Open();
dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
return dr;
}
}
}
=====================================================〉
if exists (select * from dbo.sysobjects where id = object_id(N’[dbo].[Sp_Area_SelectAll]’) and OBJECTPROPERTY(id, N’IsProcedure’) = 1)
drop procedure [dbo].[Sp_Area_SelectAll]
GO
if exists (select * from dbo.sysobjects where id = object_id(N’[dbo].[Sp_Area_SelectAllWhereProvince]’) and OBJECTPROPERTY(id, N’IsProcedure’) = 1)
drop procedure [dbo].[Sp_Area_SelectAllWhereProvince]
GO
if exists (select * from dbo.sysobjects where id = object_id(N’[dbo].[Area]’) and OBJECTPROPERTY(id, N’IsUserTable’) = 1)
drop table [dbo].[Area]
GO
CREATE TABLE [dbo].[Area] (
[ID] [int] NOT NULL ,
[Province] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[City] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[其它] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL
) ON [PRIMARY]
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO
CREATE PROCEDURE Sp_Area_SelectAll
AS
SELECT DISTINCT Province
FROM Area
ORDER BY Province
RETURN
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO
CREATE PROCEDURE Sp_Area_SelectAllWhereProvince
@Province varchar(50)
AS
SELECT Area.*
FROM Area
WHERE (Province = @Province)
RETURN
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
================================================〉
using System;
using System.Data;
using System.Data.SqlClIEnt;
using System.Configuration; namespace Db
{
/// <summary>
/// Base 的摘要說明。
/// </summary>
public class Base
{
public Base()
{
//
// TODO: 在此處添加構造函數邏輯
//
}
protected string con = ConfigurationSettings.APPSettings["dsn"];
protected SqlConnection conn;
protected SqlCommand cmd;
protected SqlDataReader dr;
protected string strSp;
protected SqlDataReader drSelectAll(string strSp)
{
conn = new SqlConnection(con);
cmd = new SqlCommand(strSp,conn);
cmd.CommandType = CommandType.StoredProcedure; conn.Open();
dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
return dr;
}//返回一個SqlDataReader }
}
http://www.cnblogs.com/fubeidong/archive/2006/08/17/479412.Html