In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-07 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces how DropDownList binds data tables to achieve two-level linkage, the article introduces in great detail, has a certain reference value, interested friends must read it!
Scenario 1: usually when we add a drop-down option under the DropDownList control, we will use its Item.Add method to add it directly under the code. If we want to add or modify drop-down options, we have to modify the source code. If several DropDownList controls have the same drop-down options, we need to add them many times, which is very inconvenient for later maintenance.
Scenario 2: when we bought tickets on the 12306 website, we must have encountered such a scenario: we need to select the province of the destination first, and then the cities of the province will be automatically loaded in the city box after the province is selected, thus achieving two-level linkage.
For the above two scenarios, we can bind the data table directly with DropDownList and dynamically load the cities under the province according to the selected province. Just talk without practice, not a hero. Let me use a small Demo to demonstrate the detailed process.
First, we need to set up two tables in the database, one is the Province (province) table, and the other is the City (city) table. The statement of the table is as follows:
Create Table Province
(
ProID int primary key
ProName varchar (20) not null
)
Create Table City
(
CityID int primary key
ProID int foreign key references Province (ProID)
CityName varchar (20)
)
Insert into Province values ('1century,' Beijing')
Insert into Province values ('2Zhi Jing' Hebei')
Insert into Province values ('3Zhi Jing' Shandong')
Insert into City values ('1century, 1 month, week, week, day, week, day, week, week
Insert into City values ('2 years, 1 month, 1 month, week, week, day, week, day, day, week, week,
Insert into City values ('3 times, 1 month, 1 month, week, week, day, week, week,
Insert into City values ('4 years, 2 months, 2 weeks, Hengshui,)
Insert into City values ('514, 2, 5, 5, 4, 5, 5, 5, 5, 5, 4, 5, 5, 4, 5, 4, 5, 4, 4, 4, 2, 4, 2, 4, 2, 4, 4, 4, 4, 4, 2, 4, 2, 4, 2, 4, 5, 5, 5, 4, 5, 5, 4, 2, 4, 2, 4, 2, 4, 5, 5, 5, 5, 2, 4, 2, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 2, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
Insert into City values ('6 years, 2 months, 2 weeks, 6 months, 6 months, 7 months, 2 months, 5 years, 5 years, 2 years, 2 months, 6 months, 6 months, 5 years, 2 years, 2 years, Baoding, Baoding, etc.)
Insert into City values ('7 years, 3 months, 7 months, 7 months, 3 months, 7 months, 7 months, 3 months, 5 years, 3 years, 5 years
Insert into City values ('8 years, 3 months, 3 months, month, week, month, month, week, month,
Insert into City values ('9 years, 3 months, 3 weeks, Qingdao')
We can know that there are three cities under Beijing-Haidian, Fengtai and Daxing, three cities under Hebei-Hengshui, Langfang and Baoding, and three cities in Shandong-Jinan, Yantai and Qingdao.
Then we put the control in the Web form, and the effect is as follows:
The names of dropDownList controls are ddlProvince and ddlCity, respectively.
Then we implement the function in the Web background code. We need to bind the ddlProvince control to the Province table when the Web form loads, and the ddlCity control to the City table when the ddlProvince drop-down option changes. The implementation code is as follows:
Establish a database connection class:
The copy code is as follows:
Public class DB
{
/ / the string that connects to the database
Public static SqlConnection CreateConnection ()
{
SqlConnection con = new SqlConnection ("Data Source=.; Initial Catalog=test;uid=sa;pwd=123456;")
Return con
}
}
Execute code when the Web form loads:
The copy code is as follows:
Protected void Page_Load (object sender, EventArgs e)
{
/ / if the form is loaded for the first time
If (! this.IsPostBack)
{
/ / bind provinces
SqlConnection con = DB.CreateConnection ()
/ / Open database connection
Con.Open ()
SqlCommand cmdProvince = new SqlCommand ("select * from Province", con)
SqlDataReader sdrProvince = cmdProvince.ExecuteReader ()
/ / bind the contents of sdrProvince to the ddlProvince drop-down list
This.ddlProvince.DataSource = sdrProvince
/ / the contents of the data table Province to be displayed
This.ddlProvince.DataTextField = "ProName"
/ / the primary key in the data table Province to be displayed
This.ddlProvince.DataValueField = "ProID"
This.ddlProvince.DataBind ()
SdrProvince.Close ()
/ / close database connection
Con.Close ()
}
}
The code that executes when the drop-down option of the ddlProvince control changes:
The copy code is as follows:
Protected void ddlProvince_SelectedIndexChanged (object sender, EventArgs e)
{
SqlConnection con = DB.CreateConnection ()
/ / Open database connection
Con.Open ()
/ / bind a city
SqlCommand cmdCity = new SqlCommand ("select * from City where ProID=" + this.ddlProvince.SelectedValue, con)
SqlDataReader sdrCity = cmdCity.ExecuteReader ()
/ / bind the contents of sdrCity to the ddlCity drop-down list
This.ddlCity.DataSource = sdrCity
/ / the contents of the data table City to be displayed
This.ddlCity.DataTextField = "CityName"
/ / the primary key in the data table City to be displayed
This.ddlCity.DataValueField = "CityID"
This.ddlCity.DataBind ()
SdrCity.Close ()
/ / close database connection
Con.Close ()
}
In this way, we use DropDownList to dynamically bind the data table and realize the function of dynamically pulling down the cities under the province according to the selected province, which achieves the purpose of decoupling in object-oriented design and enhances the maintainability of the code and the experience of users.
These are all the contents of the article "how to bind data tables in DropDownList to achieve two-level linkage". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.