DropDownList Kontrolu tek seçimlik tek satırlık açılır bir seçim listesi sunar. Çalışma mantığı RadioButtonList'e benzer.
Özellikler:
DataSource: Bir veri kaynağı bağlantısı sağlar. İstediğimiz bir veri kaynağına bağlantı yapabiliriz.
DataTextField: Bağlandığımız veri kaynağındaki istediğimiz veri alanını yani veri tablosundaki sutunu alanı adını yazarak bağlanmamızı sağlar.
DataTextFieldFormatString: CheckBoxList verilerinin biçimlendirilmesini sağlar. Aynı zamanda string olarak biçimini bize okur.
DataValueField: CheckBoxList içindeki her bir alanın değerini belirlemek için datasource da ki veri kaynağından bir alan seçer.
Items: DropDownList koleksiyonunun elemanlarını belirtir.
SelectedIndex: DropDownList içinde seçili elemanın indeks numarasını verir.
SelectedItem: DropDownList içinde seçili elemanın kendisini temsil eder.
Metotlar
OnSelectedIndexChanged: DropDownList için SelectedIndexChanged olayını üretir. SelectedIndexChanged olayı üzerinde düzenleme yapmamızı sağlar.
Olaylar
Örnek: DropdownList 'e veri,data yükleme. Listitem ile doldurma.
<%@ Page Language="C#" AutoEventWireup="True" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<script runat="server" >
void Page_Load(Object sender, EventArgs e)
{
if(!IsPostBack)
{
DropDownList1.Items.Add("Red");
//ve
ListItem blue = new ListItem();
blue.Text = "Blue";
blue.Value = "Blue";
DropDownList1.Items.Add(blue);
ListItem white = new ListItem();
white.Text = "White";
white.Value = "White";
DropDownList1.Items.Add(white);
DropDownList1.Items.Add(new ListItem("Green","Green"));
}
}
</script>
<head id="Head1" runat="server">
<title> DropDownList Örneği </title>
</head>
<body>
<form id="form1" runat="server">
DropDownList Örneği <br /><br /><hr /><br />
<asp:DropDownList ID="DropDownList1" AutoPostBack="True" runat="server"/>
</form>
</body>
</html>
Örnek
Örnek
<%@ Page Language="C#" AutoEventWireup="True" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<script runat="server" >
void Page_Load(Object sender, EventArgs e)
{
if(!IsPostBack)
{
DropDownList1.Items.Add("White");
//ve
ListItem blue = new ListItem();
blue.Text = "Blue";
blue.Value = "Blue";
DropDownList1.Items.Add(blue);
ListItem white = new ListItem();
white.Text = "Red";
white.Value = "Red";
DropDownList1.Items.Add(white);
DropDownList1.Items.Add(new ListItem("Green","Green"));
}
}
String zeminrengi = "#ffffff";
void DropDownList1_SelectedIndexChange(Object sender,EventArgs e)
{
Response.Write("Selected Index : " + DropDownList1.SelectedIndex.ToString() + "<br />");
Response.Write("Selected Item : " + DropDownList1.SelectedItem.Text + "<br />");
Response.Write("Selected Value : " + DropDownList1.SelectedValue.ToString() + "<br />");
zeminrengi = DropDownList1.SelectedItem.Text;
}
</script>
<head id="Head1" runat="server">
<title> DropDownList Örneği </title>
</head>
<body bgcolor="<%=zeminrengi%>">
<form id="form1" runat="server">
<hr /><br />
DropDownList Örneği <br /><br /><hr /><br />
<asp:DropDownList ID="DropDownList1" AutoPostBack="True" runat="server" OnSelectedIndexChanged="DropDownList1_SelectedIndexChange"/>
</form>
</body>
</html>
Örnek
<%@ Page Language="C#" AutoEventWireup="True" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<script runat="server" >
void DropDownList_Changed(Object sender, EventArgs e)
{
Calendar1.DayStyle.BackColor = System.Drawing.Color.FromName(DropDownList1.SelectedItem.Value);
}
</script>
<head id="Head1" runat="server">
<title> DropDownList Örneği </title>
</head>
<body>
<form id="form1" runat="server">
<h3> DropDownList Örneği </h3>
Calendar Kontrolu Gün Listesi için bir zemin rengi seçiniz.
<br /><br />
<asp:Calendar ID="Calendar1" ShowGridLines="True" ShowTitle="True" runat="server"/>
<br /><br />
<table cellpadding="5"><tr><td>Zemin Rengi:</td></tr>
<tr><td>
<asp:DropDownList ID="DropDownList1" AutoPostBack="True" OnSelectedIndexChanged="DropDownList_Changed" runat="server">
<asp:ListItem Selected="True" Value="White" Text="White"> </asp:ListItem>
<asp:ListItem Value="Silver"> Silver </asp:ListItem>
<asp:ListItem Value="Red" Text="Red" />
<asp:ListItem Value="Khaki"> Khaki </asp:ListItem>
<asp:ListItem Value="DarkKhaki"> Dark Khaki </asp:ListItem>
<asp:ListItem Value="Blue" Text="Blue" />
</asp:DropDownList>
</td></tr></table>
</form>
</body>
</html>
Örnek : DropDownList Datatable ile datasource bağlantı kurma.
<%@ Page Language="C#" AutoEventWireup="True" %>
<%@ Import Namespace="System.Data" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<script runat="server" >
void DropDownList1_Change(Object sender, EventArgs e)
{
Calendar1.DayStyle.BackColor = System.Drawing.Color.FromName(DropDownList1.SelectedItem.Value);
}
void Page_Load(Object sender, EventArgs e)
{
if(!IsPostBack)
{
DropDownList1.DataSource = CreateDataSource();
DropDownList1.DataTextField = "ColorTextField";
DropDownList1.DataValueField = "ColorValueField";
DropDownList1.DataBind();
DropDownList1.SelectedIndex = 0;
}
}
ICollection CreateDataSource()
{
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("ColorTextField", typeof(String)));
dt.Columns.Add(new DataColumn("ColorValueField", typeof(String)));
dt.Rows.Add(CreateRow("White", "White", dt));
dt.Rows.Add(CreateRow("Silver", "Silver", dt));
dt.Rows.Add(CreateRow("Dark Gray", "DarkGray", dt));
dt.Rows.Add(CreateRow("Khaki", "Khaki", dt));
dt.Rows.Add(CreateRow("Dark Khaki", "DarkKhaki", dt));
DataView dv = new DataView(dt);
return dv;
}
DataRow CreateRow(String Text, String Value, DataTable dt)
{
DataRow dr = dt.NewRow();
dr[0] = Text;
dr[1] = Value;
return dr;
}
</script>
<head id="Head1" runat="server">
<title> DropDownList Data Binding Örneği </title>
</head>
<body>
<form id="form1" runat="server">
<h3> DropDownList Data Binding Örneği </h3>
Calendar Kontrolu Gün Listesi için bir zemin rengi seçiniz.
<br /><br />
<asp:Calendar id="Calendar1" ShowGridLines="True" ShowTitle="True" runat="server"/>
<br /><br />
<table cellpadding="5">
<tr><td>
Zemin Rengi:
</td></tr>
<tr><td>
<asp:DropDownList ID="DropDownList1" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_Change" runat="server"/>
</td></tr></table>
</form>
</body>
çok güzel bir çalışma teşekkürler
YanıtlaSilSelectedIndexChanged: metodunu kullanarak veritabanından çektiğim verilerin olduğu tablonun içinde seo adlı bir alan var bu alan içindeki metinleri adres çubuğuna göndermemi mümkünmü mesela dropdownlistte ili seccem secince il veritabanındaki seo alanının içndeki valueye eşit seo yazısını alacak ilçeyi seçtikten sonra da aynı işlemi yapacak sonra arama butonuna tıkladımmı o seo ile sayfaya yönlendirilecekbunu nasıl yapabilirim yardımcı olabilirmisiniz?
YanıtlaSil