<%@ Import Namespace="System.Data"%> <%@ Import Namespace="System.Data.OleDb"%> <%@ Page Language="C#" Debug="true" %> <script runat="server"> OleDbConnection objConn; OleDbCommand objCmd; void Page_Load(object sender,EventArgs e) { String strConnString; strConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("database/mydatabase.mdb") + ";"; objConn = new OleDbConnection(strConnString); objConn.Open(); if(!Page.IsPostBack) { BindData(); } } void BindData() { String strSQL; strSQL = "SELECT * FROM customer"; OleDbDataReader dtReader; objCmd = new OleDbCommand(strSQL, objConn); dtReader = objCmd.ExecuteReader(); //*** BindData to ListView ***// myListView.DataSource = dtReader; myListView.DataBind(); dtReader.Close(); dtReader = null; } void Page_UnLoad() { objConn.Close(); objConn = null; } protected void myListView_ItemDataBound(Object sender, ListViewItemEventArgs e) { if (e.Item.ItemType == ListViewItemType.DataItem) { ListViewDataItem lvDataItem = (ListViewDataItem)e.Item; DataRowView rowView = (DataRowView)lvDataItem.DataItem; //*** CustomerID ***// Label lblCustomerID = (Label)(e.Item.FindControl("lblCustomerID")); if (lblCustomerID != null) { lblCustomerID.Text = rowView["CustomerID"].ToString(); } //*** Name ***// Label lblName = (Label)(e.Item.FindControl("lblName")); if (lblName != null) { lblName.Text = rowView["Name"].ToString(); } //*** Email ***// Label lblEmail = (Label)(e.Item.FindControl("lblEmail")); if (lblEmail != null) { lblEmail.Text = rowView["Email"].ToString(); } //*** CountryCode ***// Label lblCountryCode = (Label)(e.Item.FindControl("lblCountryCode")); if (lblCountryCode != null) { lblCountryCode.Text = rowView["CountryCode"].ToString(); } //*** Budget ***// Label lblBudget = (Label)(e.Item.FindControl("lblBudget")); if (lblBudget != null) { lblBudget.Text = rowView["Budget"].ToString(); } //*** Used ***// Label lblUsed = (Label)(e.Item.FindControl("lblUsed")); if (lblUsed != null) { lblUsed.Text = rowView["Used"].ToString(); } } } </script> <html> <head> <title>ThaiCreate.Com ASP.NET - ListView</title> </head> <body> <form id="form1" runat="server"> <asp:ListView ID="myListView" runat="server" DataKeyNames="CustomerID" onItemDataBound="myListView_ItemDataBound"> <LayoutTemplate> <table> <tr> <td> <table runat="server" border="1"> <tr> <th runat="server"> CustomerID</th> <th runat="server"> Name</th> <th runat="server"> Email</th> <th runat="server"> CountryCode</th> <th runat="server"> Budget</th> <th runat="server"> Used</th> </tr> <tr ID="itemPlaceholder" runat="server"> </tr> </table> </td> </tr> </table> </LayoutTemplate> <ItemTemplate> <tr> <td> <asp:Label ID="lblCustomerID" runat="server"/> </td> <td> <asp:Label ID="lblName" runat="server"/> </td> <td> <asp:Label ID="lblEmail" runat="server"/> </td> <td> <asp:Label ID="lblCountryCode" runat="server" /> </td> <td> <asp:Label ID="lblBudget" runat="server" /> </td> <td> <asp:Label ID="lblUsed" runat="server" /> </td> </tr> </ItemTemplate> </asp:ListView> </form> </body> </html>
ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท