One of the most commonly used server controls in ASP.NET is the DropDownList control. In this blog I will show you how to bind a list of objects to a DropDownList control.
1. Create a .aspx page
2. Click on the "Toolbox" panel
4. Create a connection string in the Web.Config file
<connectionStrings> <add name="NorthwindConnectionString" connectionString="Data Source=(local); Initial Catalog=Northwind;Integrated Security=True" providerName="System.Data.SqlClient"/> </connectionStrings>
5. Create a new folder in your project call "BusinessObjects" then add a .cs file call "Category.cs"

6. In the "Category.cs" file type in the following code
using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace BusinessObjects { public class Category { private int _categoryID; private string _categoryName; public Category(int Id, string Name) { this._categoryID = Id; this._categoryName = Name; } public int CategoryID { get { return this._categoryID; } set { this._categoryID = value; } } public string CategoryName { get { return this._categoryName; } set { this._categoryName = value; } } } }
7. In the code behind page(.cs) page of the .aspx page, type in the following lines at the top to use the following libraries
using System.Web.Configuration; using System.Data.SqlClient; using System.Data; using BusinessObjects;8. Create a new method call BindCategoriesListObjects() that returns void
protected void BindCategoriesListObjects() { DataTable dtCategories = new DataTable(); string connectString = WebConfigurationManager.ConnectionStrings["NorthwindConnectionString"]. ConnectionString; ListThe code above creates new DataTable call dtCategories, then it connects to the database using the "NorthwindConnectionString", and queries the CategoryID, and CategoryName in the Categories table in the Northwind database. The query results are then stored in the DataTable. Then we iterate through the DataTable dtCategories with the foreach loop and create a list of "Category" objects call "categories". The list "Category" objects is then bind to the DropDownList1 control.categories = new List (); using (SqlConnection conn = new SqlConnection(connectString)) { SqlCommand cmd = new SqlCommand("SELECT CategoryID,CategoryName FROM Categories", conn); conn.Open(); SqlDataAdapter adapter = new SqlDataAdapter(cmd); adapter.Fill(dtCategories); foreach (DataRow row in dtCategories.Rows) { categories.Add(new Category(Convert.ToInt32(row["CategoryID"].ToString()), row["CategoryName"].ToString())); } //used to set the DropDownList1.DataSource to categories list of objects DropDownList1.DataSource = categories; DropDownList1.DataTextField = "CategoryName"; DropDownList1.DataValueField = "CategoryID"; DropDownList1.DataBind(); } }
//used to set the DropDownList1.DataSource to categories list of objects DropDownList1.DataSource = categories; DropDownList1.DataTextField = "CategoryName"; DropDownList1.DataValueField = "CategoryID"; DropDownList1.DataBind();9. Call the BindCategoriesListObjects() method in the Page_Load method
protected void Page_Load(object sender, EventArgs e) { BindCategoriesListObjects(); }10. Run the application and you will see the DropDownList1 control populated with the Category Names in the Northwind database
11. If you look at the HTML source code you will see the following
<select name="DropDownList1" id="DropDownList1"> <option value="1">Beverages</option> <option value="2">Condiments</option> <option value="3">Confections</option> <option value="4">Dairy Products</option> <option value="5">Grains/Cereals</option> <option value="6">Meat/Poultry</option> <option value="7">Produce</option> <option value="8">Seafood</option> </select>
Related Blogs:
- Bind a DataTable To A DropDownList
- (DropDownList) Setting Default Value on First Page Load
- Use The DropDownList Control To Populate GridView
- Bind DropDownList To A List Of Objects
Mua vé tại Aivivu, tham khảo
ReplyDeletevé máy bay đi Mỹ hạng thương gia
đã có chuyến bay từ mỹ về việt nam chưa
giá vé máy bay từ Vancouver về việt nam
từ nhật bản về việt nam bao nhiêu tiếng
đặt vé máy bay từ hàn quốc về việt nam
Vé máy bay từ Đài Loan về VN
chi phí vé máy bay cho chuyên gia nước ngoài
I like the helpful info you provide in the articles. I will bookmark
ReplyDeleteFree Iphone 7 Government Phone
Antalya
ReplyDeleteKonya
Adana
Ankara
Van
FC5V51
ankara
ReplyDeletesakarya
tekirdağ
kastamonu
amasya
G5SHO
7C841
ReplyDeleteankara sesli sohbet uygulamaları
sohbet sitesi
en iyi ücretsiz sohbet siteleri
siirt sesli sohbet odası
bilecik kadınlarla ücretsiz sohbet
trabzon görüntülü sohbet siteleri ücretsiz
mobil sohbet bedava
sivas sesli görüntülü sohbet
bedava sohbet siteleri
95B96
ReplyDeleteRaca Coin Hangi Borsada
Tiktok Beğeni Hilesi
Kripto Para Kazanma
Binance Nasıl Oynanır
Clysterum Coin Hangi Borsada
Expanse Coin Hangi Borsada
Kwai Takipçi Satın Al
Satoshi Coin Hangi Borsada
Threads İzlenme Satın Al
D5897
ReplyDeletepoocoin
metamask
looksrare
yearn
onekey
ledger desktop
poocoin
shiba
dexscreener
1F699
ReplyDeletetoptan sabun
mexc
bitcoin haram mı
gate io
kucoin
kefir sabunu
canlı sohbet uygulamaları
argan sabunu
bybit
E8A1D
ReplyDeletehuobi
paribu
bitcoin giriş
coinex
https://kapinagelsin.com.tr/
toptan sabun
canlı sohbet
kucoin
probit
C23AC
ReplyDeletebitmex
referans kimliği nedir
bingx
cointiger
kripto para nereden alınır
mexc
probit
bingx
bybit
98562
ReplyDeletebinance referans kimliği
mobil 4g proxy
rastgele canlı sohbet
huobi
telegram türk kripto kanalları
gate io
bitcoin haram mı
binance referans kimliği nedir
referans kod
7C4AE
ReplyDeleteTelegram Grup
fuar standı
Yapay Zeka Uygulamaları
seo nedir
vds satın al
dedicated server
fiziksel sunucu
Online Oyunlar
Telegram Kanal
69069
ReplyDeleteEnj Coin Yorum
Dai Coin Yorum
Lto Coin Yorum
BTC Son Dakika
Lazio Coin Yorum
Waxp Coin Yorum
Rose Coin Yorum
Gtc Coin Yorum
Lrc Coin Yorum
EBF9E
ReplyDeletesanal show canlı
0094F
ReplyDeletegörüntülü ücretli show
81D8E
ReplyDeletewhatsapp görüntülü show
C565D
ReplyDeleteücretli show
409AF2878A
ReplyDeletestag
cobra vega
telegram show
maxman
viagra
canli cam show
canli web cam show
görüntülü şov whatsapp numarası
ereksiyon hapı
BC3CA3EF7A
ReplyDeletelifta
sildegra
sertleştirici
delay
performans arttırıcı
ücretli show
yapay kızlık zarı
cam şov
viagra
FB36FFAA5D
ReplyDeletetwitter ucuz beğeni
2229639E36
ReplyDeleteyoutube ucuz beğeni
B73AB0D918
ReplyDeleteTelegram Coin Botları Güvenilir mi
En İyi Telegram Mining Botları
Telegram Güvenilir Mining Botları
Yeni Telegram Airdrop Oyunları
Telegram Güvenilir Farm Botları
2A8E8B9DAD
ReplyDelete-
-
bayan kusadası
-
-