(C#) ASP.NET DropDownlist & DataBinding |
(C#) ASP.NET DropDownlist & DataBinding เป็นการใช้ DropDownlist อ่านข้อมูลจาก Database ในตัวอย่างนี้ผมได้ยกตัวอย่างการใช้บน DataTable,TableRows,SortedList การเพิ่ม/ลบ Item และการใช้งานอื่น ๆ
Language Code : VB.NET || C#
Framework : 1,2,3,4
AspNetDropDownlistDataBind.aspx
<%@ Import Namespace="System.Data"%>
<%@ Import Namespace="System.Data.OleDb"%>
<%@ Page Language="C#" Debug="true" %>
<script runat="server">
void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
DropDownListDataTable();
DropDownListDataTableRows();
DropDownListSortedList();
DropDownListAddInsertItem();
}
}
//*** DropDownList & DataTable ***//
void DropDownListDataTable()
{
OleDbConnection objConn;
OleDbDataAdapter dtAdapter;
DataTable dt = new DataTable();
String strConnString;
strConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+
Server.MapPath("database/mydatabase.mdb")+"";
objConn = new OleDbConnection(strConnString);
objConn.Open();
String strSQL;
strSQL = "SELECT * FROM country";
dtAdapter = new OleDbDataAdapter(strSQL, objConn);
dtAdapter.Fill(dt);
dtAdapter = null;
objConn.Close();
objConn = null;
//*** DropDownList ***//
this.myDDL1.DataSource = dt;
this.myDDL1.DataTextField = "CountryName";
this.myDDL1.DataValueField = "CountryCode";
this.myDDL1.DataBind();
//*** Default Value ***//
myDDL1.SelectedIndex = myDDL1.Items.IndexOf(myDDL1.Items.FindByValue("TH")); //*** By DataValueField ***//
//myDDL1.SelectedIndex = myDDL1.Items.IndexOf(myDDL1.Items.FindByText("Thailand")); //*** By DataTextField ***//
}
//*** DropDownList & TableRows ***//
void DropDownListDataTableRows()
{
DataTable dt = new DataTable();
DataRow dr;
//*** Column ***//
dt.Columns.Add("Sex");
dt.Columns.Add("SexDesc");
//*** Rows ***//
dr = dt.NewRow();
dr["Sex"] = "M";
dr["SexDesc"] = "Man";
dt.Rows.Add(dr);
//*** Rows ***//
dr = dt.NewRow();
dr["Sex"] = "W";
dr["SexDesc"] = "Woman";
dt.Rows.Add(dr);
//*** DropDownList ***//
this.myDDL2.DataSource = dt;
this.myDDL2.DataTextField = "SexDesc";
this.myDDL2.DataValueField = "Sex";
this.myDDL2.DataBind();
//*** Default Value ***//
myDDL2.SelectedIndex = myDDL2.Items.IndexOf(myDDL2.Items.FindByValue("W")); //*** By DataValueField ***//
//myDDL2.SelectedIndex = myDDL2.Items.IndexOf(myDDL2.Items.FindByText("Woman")); //*** By DataTextField ***//
}
//*** DropDownList & SortedList ***//
void DropDownListSortedList()
{
SortedList mySortedList = new SortedList();
mySortedList.Add("M","Man");
mySortedList.Add("W","Woman");
//*** DropDownList ***//
this.myDDL3.DataSource = mySortedList;
this.myDDL3.DataTextField = "Value";
this.myDDL3.DataValueField = "Key";
this.myDDL3.DataBind();
//*** Default Value ***//
myDDL3.SelectedIndex = myDDL3.Items.IndexOf(myDDL3.Items.FindByValue("W")); //*** By DataValueField ***//
//myDDL3.SelectedIndex = myDDL3.Items.IndexOf(myDDL3.Items.FindByText("Woman")); //*** By DataTextField ***//
}
//*** Add/Insert Items ***//
void DropDownListAddInsertItem()
{
SortedList mySortedList = new SortedList();
mySortedList.Add("M","Man");
mySortedList.Add("W","Woman");
//*** DropDownList ***//
this.myDDL4.DataSource = mySortedList;
this.myDDL4.DataTextField = "Value";
this.myDDL4.DataValueField = "Key";
this.myDDL4.DataBind();
//*** Add & Insert New Item ***//
String strText,strValue;
//*** Insert Item ***//
strText = "";
strValue = "";
ListItem InsertItem = new ListItem(strText, strValue);
myDDL4.Items.Insert(0, InsertItem);
//*** Add Items ***//
strText = "Guy";
strValue = "G";
ListItem AddItem = new ListItem(strText, strValue);
myDDL4.Items.Add(AddItem);
//*** Default Value ***//
myDDL4.SelectedIndex = myDDL4.Items.IndexOf(myDDL4.Items.FindByValue("")); //*** By DataValueField ***//
//myDDL4.SelectedIndex = myDDL4.Items.IndexOf(myDDL4.Items.FindByText("")); //*** By DataTextField ***//
}
void Button1_OnClick(object sender, EventArgs e)
{
this.lblText1.Text = this.myDDL1.SelectedItem.Value; //*** Or this.myDDL1.SelectedItem.Text ***//
this.lblText2.Text = this.myDDL2.SelectedItem.Value; //*** Or this.myDDL2.SelectedItem.Text ***//
this.lblText3.Text = this.myDDL3.SelectedItem.Value; //*** Or this.myDDL3.SelectedItem.Text ***//
this.lblText4.Text = this.myDDL4.SelectedItem.Value; //*** Or this.myDDL4.SelectedItem.Text ***//
}
</script>
<html>
<head>
<title>ThaiCreate.Com ASP.NET - DropDownList & DataBind</title>
</head>
<body>
<form id="form1" runat="server">
<asp:DropDownList id="myDDL1" runat="server"></asp:DropDownList>
<asp:DropDownList id="myDDL2" runat="server"></asp:DropDownList>
<asp:DropDownList id="myDDL3" runat="server"></asp:DropDownList>
<asp:DropDownList id="myDDL4" runat="server"></asp:DropDownList>
<asp:Button id="Button1" onclick="Button1_OnClick" runat="server" Text="Button"></asp:Button>
<hr />
<asp:Label id="lblText1" runat="server"></asp:Label><br />
<asp:Label id="lblText2" runat="server"></asp:Label><br />
<asp:Label id="lblText3" runat="server"></asp:Label><br />
<asp:Label id="lblText4" runat="server"></asp:Label><br />
</form>
</body>
</html>
Screenshot
ASP.NET & AccessDataSource and DropDownlist
|
ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท
|
|
|
By : |
ThaiCreate.Com Team (บทความเป็นลิขสิทธิ์ของเว็บไทยครีเอทห้ามนำเผยแพร่ ณ เว็บไซต์อื่น ๆ) |
|
Score Rating : |
|
|
|
Create/Update Date : |
2008-11-15 06:49:41 /
2017-03-28 20:46:06 |
|
Download : |
|
|
Sponsored Links / Related |
|
|
|
|
|
|
|