欢迎光临
我们一直在努力

ASP.NET实现下拉框二级联动组件-.NET教程,Asp.Net开发

建站超值云服务器,限时71元/月

 

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 }
}

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » ASP.NET实现下拉框二级联动组件-.NET教程,Asp.Net开发
分享到: 更多 (0)