日期:2010-10-28  浏览次数:20498 次

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