数据库内容是:
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
麻烦各位老师,朋友帮我看看,我的效果怎么出不来呀

posted on 2007-03-02 14:20  xf雪儿  阅读(273)  评论(2编辑  收藏  举报