(C#) ASP.NET BulletedList & DataBinding |
(C#) ASP.NET BulletedList & DataBinding เป็นการใช้ BulletedList เรียกข้อมูลจาก Database ผ่าน ADO.NET ตัวอย่างนี้ผมได้ยกตัวอย่างการใช้บน DataTable,TableRows,SortedList รวมทั้งการเพิ่ม Item เข้าไปใน Control
Language Code : VB.NET || C#
Framework : 1,2,3,4
AspNetBulletedListDataBind.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)
{
BulletedListDataTable();
BulletedListDataTableRows();
BulletedListSortedList();
BulletedListAddInsertItem();
}
}
//*** BulletedList & DataTable ***//
void BulletedListDataTable()
{
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;
//*** BulletedList ***//
this.myBuList1.DataSource = dt;
this.myBuList1.DataTextField = "CountryName";
this.myBuList1.DataValueField = "CountryCode";
this.myBuList1.DataBind();
}
//*** BulletedList & TableRows ***//
void BulletedListDataTableRows()
{
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);
//*** BulletedList ***//
this.myBuList2.DataSource = dt;
this.myBuList2.DataTextField = "SexDesc";
this.myBuList2.DataValueField = "Sex";
this.myBuList2.DataBind();
}
//*** BulletedList & SortedList ***//
void BulletedListSortedList()
{
SortedList mySortedList = new SortedList();
mySortedList.Add("M","Man");
mySortedList.Add("W","Woman");
//*** BulletedList ***//
this.myBuList3.DataSource = mySortedList;
this.myBuList3.DataTextField = "Value";
this.myBuList3.DataValueField = "Key";
this.myBuList3.DataBind();
}
//*** Add/Insert Items ***//
void BulletedListAddInsertItem()
{
SortedList mySortedList = new SortedList();
mySortedList.Add("M","Man");
mySortedList.Add("W","Woman");
//*** BulletedList ***//
this.myBuList4.DataSource = mySortedList;
this.myBuList4.DataTextField = "Value";
this.myBuList4.DataValueField = "Key";
this.myBuList4.DataBind();
//*** Add & Insert New Item ***//
String strText,strValue;
//*** Insert Item ***//
strText = "";
strValue = "";
ListItem InsertItem = new ListItem(strText, strValue);
myBuList4.Items.Insert(0, InsertItem);
//*** Add Items ***//
strText = "Guy";
strValue = "G";
ListItem AddItem = new ListItem(strText, strValue);
myBuList4.Items.Add(AddItem);
}
</script>
<html>
<head>
<title>ThaiCreate.Com ASP.NET - BulletedList & DataBind</title>
</head>
<body>
<form id="form1" runat="server">
<asp:BulletedList id="myBuList1" runat="server"></asp:BulletedList><hr />
<asp:BulletedList id="myBuList2" runat="server"></asp:BulletedList><hr />
<asp:BulletedList id="myBuList3" runat="server"></asp:BulletedList><hr />
<asp:BulletedList id="myBuList4" runat="server"></asp:BulletedList>
</form>
</body>
</html>
Screenshot
ASP.NET & AccessDataSource and BulletedList
|
ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท
|
|
|
By : |
ThaiCreate.Com Team (บทความเป็นลิขสิทธิ์ของเว็บไทยครีเอทห้ามนำเผยแพร่ ณ เว็บไซต์อื่น ๆ) |
|
Score Rating : |
|
|
|
Create/Update Date : |
2008-11-22 09:24:58 /
2017-03-28 20:47:00 |
|
Download : |
|
|
Sponsored Links / Related |
|
|
|
|
|
|
|