程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> .NET實例教程 >> ASP.NET實現下拉框二級聯動組件

ASP.NET實現下拉框二級聯動組件

編輯:.NET實例教程


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

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved