各位老师,你们好,请帮我看看我的这两个下拉列表,第一个下拉列表为什么不能让第二个下拉列表跟着变化?我用(VS2005做的)
2007-03-02 14:20
399 查看
数据库内容是:
create database sheng
use sheng
create table sf
(
ID int primary key,
sfname varchar(100) not null
)
create table city
(
cityID int primary key,
proID int foreign key references sf(ID) not null,
cityName varchar(100) not null
)
insert into sf values(1,'北京')
insert into sf values(2,'吉林')
insert into sf values(3,'广东')
insert into sf values(4,'四川')
nsert into city values(1,1,'北京')
insert into city values(2,2,'吉林')
insert into city values(3,2,'长春')
insert into city values(4,3,'广州')
insert into city values(5,3,'佛山')
insert into city values(6,4,'成都')
=================================
在VS2005中做的:
第一个外部类文件是:dbcon.cs
using System;
using System.Data;
using System.Configuration;
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;
/// <summary>
/// dbcon 的摘要说明
/// </summary>
public class dbcon
{
public static SqlConnection con()
{
return new SqlConnection("server=.;database=sheng;uid=sa;pwd=750807");
}
}
第二个是:Default.aspx.cs
using System;
using System.Data;
using System.Configuration;
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;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
SqlConnection con = dbcon.con();
con.Open();
SqlCommand cmd=new SqlCommand("select * from sf",con);
this.DropDownList1.DataSource=cmd.ExecuteReader();
this.DropDownList1.DataValueField = "ID";
this.DropDownList1.DataTextField="sfname";
this.DropDownList1.DataBind();
con.Close();
con.Open();
SqlCommand cmd1=new SqlCommand("select * from city where proID="+this.DropDownList1.SelectedValue,con);
this.DropDownList2.DataSource = cmd1.ExecuteReader();
this.DropDownList2.DataValueField = "cityID";
this.DropDownList2.DataTextField = "cityName";
this.DropDownList2.DataBind();
con.Close();
}}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
SqlConnection con = dbcon.con();
con.Open();
SqlCommand cmd1 = new SqlCommand("select * from city where proID="+this.DropDownList1.SelectedValue,con);
this.DropDownList2.DataSource = cmd1.ExecuteReader();
this.DropDownList2.DataValueField = "cityID";
this.DropDownList2.DataTextField = "cityName";
this.DropDownList2.DataBind();
con.Close();
}
}
三,在Default.aspx中拉入了两个DropDownList,第一个是DropDownList1,第二个是DropDownList2
麻烦各位老师,朋友帮我看看,我的效果怎么出不来呀
create database sheng
use sheng
create table sf
(
ID int primary key,
sfname varchar(100) not null
)
create table city
(
cityID int primary key,
proID int foreign key references sf(ID) not null,
cityName varchar(100) not null
)
insert into sf values(1,'北京')
insert into sf values(2,'吉林')
insert into sf values(3,'广东')
insert into sf values(4,'四川')
nsert into city values(1,1,'北京')
insert into city values(2,2,'吉林')
insert into city values(3,2,'长春')
insert into city values(4,3,'广州')
insert into city values(5,3,'佛山')
insert into city values(6,4,'成都')
=================================
在VS2005中做的:
第一个外部类文件是:dbcon.cs
using System;
using System.Data;
using System.Configuration;
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;
/// <summary>
/// dbcon 的摘要说明
/// </summary>
public class dbcon
{
public static SqlConnection con()
{
return new SqlConnection("server=.;database=sheng;uid=sa;pwd=750807");
}
}
第二个是:Default.aspx.cs
using System;
using System.Data;
using System.Configuration;
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;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
SqlConnection con = dbcon.con();
con.Open();
SqlCommand cmd=new SqlCommand("select * from sf",con);
this.DropDownList1.DataSource=cmd.ExecuteReader();
this.DropDownList1.DataValueField = "ID";
this.DropDownList1.DataTextField="sfname";
this.DropDownList1.DataBind();
con.Close();
con.Open();
SqlCommand cmd1=new SqlCommand("select * from city where proID="+this.DropDownList1.SelectedValue,con);
this.DropDownList2.DataSource = cmd1.ExecuteReader();
this.DropDownList2.DataValueField = "cityID";
this.DropDownList2.DataTextField = "cityName";
this.DropDownList2.DataBind();
con.Close();
}}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
SqlConnection con = dbcon.con();
con.Open();
SqlCommand cmd1 = new SqlCommand("select * from city where proID="+this.DropDownList1.SelectedValue,con);
this.DropDownList2.DataSource = cmd1.ExecuteReader();
this.DropDownList2.DataValueField = "cityID";
this.DropDownList2.DataTextField = "cityName";
this.DropDownList2.DataBind();
con.Close();
}
}
三,在Default.aspx中拉入了两个DropDownList,第一个是DropDownList1,第二个是DropDownList2
麻烦各位老师,朋友帮我看看,我的效果怎么出不来呀
相关文章推荐
- 请各位帮忙看一下,第一个程序为什么不能输出和第二个程序一样的结果?这两个程序只有求最大公约束的代码不同
- 两个关联的下拉框,第一个改变,第二个跟着变
- 两个数组[n] [m] n>m 第一个数组的数字无序排列 第二个数组为空 取出第一个数组的最小值 放到第二个数组中第一个位置, 依次类推. 不能改变A数组,不能对之进行排序,也不可以倒到别的数组中。
- 各位帮我看看我的代码为什么不能通过vba向access添加新纪录?
- 两个数组 [n] [m] n>m 第一个数组的数字无序排列 第二个数组为空 取出第一个数组的最小值 放到第二个数组中第一个位置, 依次类推. 不能改变A数组,不能对之进行排序,也不可以倒到别的数组中。
- 很有意思的两个菜单,第一个手动点击实现张开与关闭,第二个快速滑来滑去,菜单也跟着滑来滑去
- jquery easy ui中根据第一个下拉框框选中的值,设置第二个下拉框是否可以编辑
- 华为机试题:输入两个超长整型构成的字符串,其间使用一个空格分隔,每个字符串最大长度为100个字符。求第一个整数除以第二个整数以后的余数。。
- 点击搜索框下拉列表不能触发跳转
- 网页中有两个table, 默认只显示第一个table,点击一个按钮后显示第二个table,第一个table隐藏
- MFC编程中,组合框下拉列表不能弹出
- 麻烦各位大神看看我用codeblocks写的C,为什么会停止运行!!!!
- 我的第一个网站上线了,你们帮忙看看哪里需要改正的
- 为什么标准C++中两个直接字符串常量不能相加
- 算法:C语言实现》习题4.8—— 给定两个序列,判断第二个序列是否可以由第一个序列出栈得到
- 为什么两个float类型的数值不能直接相等
- 为什么有时出现VS2005不能新建Win32 智能设备项目(转)
- 三个div快 第一个左浮动 第二个不浮动 第三个右浮动 为什么右浮动上不去?
- 数组a中每个元素包含两个值第一个是整型第二个是字符串写一个sort函数以第一个值为键按从小到大排序
- 实现HTML下拉列表中不能选择的项