Asp.Net从零开始学-8

    技术2022-05-11  23

          ListBox控件 

    //显示选择的内容private void ListBox1_SelectedIndexChanged(object sender, System.EventArgs e)  {   this.Label1.Text="你选择了"+this.ListBox1.SelectedItem.Text;  }//添加ListBox新项目 private void Button1_Click(object sender, System.EventArgs e)  {   this.ListBox1.Items.Add(this.TextBox1.Text);  }

          DropDownList控件方法实现

    建立一个dropDownTest数据库create database dropDownTestgouse dropDownTestgocreate table province(proID int primary key,proName varchar(50) not null)insert into province values(1,'北京')insert into province values(2,'福建')insert into province values(3,'辽宁')insert into province values(4,'黑龙江')insert into province values(5,'山东')gocreate table city(  cityID int primary key,  proID int foreign key references province(proID),  cityName varchar(50) not null)goinsert into city values(1,1,'北京')insert into city values(2,2,'福州')insert into city values(3,2,'厦门')insert into city values(4,2,'泉州')insert into city values(5,2,'漳州')insert into city values(6,2,'宁德')insert into city values(7,3,'沈阳')insert into city values(8,3,'大连')insert into city values(9,4,'哈尔滨')insert into city values(10,4,'佳木斯')insert into city values(11,5,'济南')insert into city values(12,5,'烟台')goselect * from provincegoselect * from citygo

    //数据绑定到DropDownList控件private void Page_Load(object sender, System.EventArgs e)  {   // 在此处放置用户代码以初始化页面   if(!this.IsPostBack){    //绑定省    SqlConnection con=DB.createConnection();    con.Open();    SqlCommand cmd=new SqlCommand("select * from province",con);    SqlDataReader sdr=cmd.ExecuteReader();    this.DropDownList1.DataSource=sdr;    this.DropDownList1.DataTextField="proName";    this.DropDownList1.DataValueField="proID";    this.DropDownList1.DataBind();    sdr.Close();    //绑定市    SqlCommand cmdCity=new SqlCommand("select * from city where proID="+this.DropDownList1.SelectedValue,con);    sdr=cmdCity.ExecuteReader();    this.DropDownList2.DataSource=sdr;    this.DropDownList2.DataTextField="cityName";    this.DropDownList2.DataValueField="cityID";    this.DropDownList2.DataBind();    sdr.Close();    con.Close();   }  }//DropDownList1与DropDownList2之间互动   DropDownList1的AutoPostBack属性设置为Trueprivate void DropDownList1_SelectedIndexChanged(object sender, System.EventArgs e)  {   string proID=this.DropDownList1.SelectedValue;   SqlConnection con=DB.createConnection();   con.Open();   SqlCommand cmdCity=new SqlCommand("select * from city where proID="+proID,con);   SqlDataReader sdr=cmdCity.ExecuteReader();   this.DropDownList2.DataSource=sdr;   this.DropDownList2.DataTextField="cityName";   this.DropDownList2.DataValueField="cityID";   this.DropDownList2.DataBind();   sdr.Close();   con.Close();  }


    最新回复(0)