(C#) ASP.NET ListView Control - MySQL Database - MySql.Data.MySqlClient |
(C#) ASP.NET ListView & MySQL (Framework 2.0,3.5) เป็นตัวอย่างการ เรียกใช้งาน Control ชื่อ ListView เพื่อดุงข้อมูลจากฐานข้อมูล MySQL มาแสดง โดยใช้ NameSpace ชื่อ MySql.Data.MySqlClient ในตัวอย่างผมพัฒนาด้วย Visual Studio 2005,2008 ซึ่ง Run บน Framework 2.0,3.5 ในรูปแบบของ Code-Behind แยกในส่วนของ Tag และ Code ไว้คนล่ะส่วน
Language Code : VB.NET || C#
Framework : 3,4
ListView1.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ListView1.aspx.cs" Inherits="ListView1" %>
<%@ Register assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" namespace="System.Web.UI.WebControls" tagprefix="asp" %>
<!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">
<head runat="server">
<title>ThaiCreate.Com ASP.NET - ListView & MySQL</title>
</head>
<body>
<form id="form1" runat="server">
<asp:ListView ID="myListView" runat="server" DataKeyNames="CustomerID">
<LayoutTemplate>
<table>
<tr>
<td>
<table id="Table1" runat="server" border="1">
<tr>
<th id="Th1" runat="server">
Select</th>
<th id="Th2" runat="server">
CustomerID</th>
<th id="Th3" runat="server">
Name</th>
<th id="Th4" runat="server">
Email</th>
<th id="Th5" runat="server">
CountryCode</th>
<th id="Th6" runat="server">
Budget</th>
<th id="Th7" runat="server">
Used</th>
</tr>
<tr ID="itemPlaceholder" runat="server">
</tr>
</table>
</td>
</tr>
</table>
</LayoutTemplate>
<ItemTemplate>
<tr>
<td>
<asp:CheckBox id="chkCustomerID" runat="server"/>
</td>
<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>
<br />
<asp:Button id="Button1" onclick="Button1_Click" runat="server" Text="Submit"></asp:Button>
<hr />
<asp:Label id="lblText" runat="server"></asp:Label>
</form>
</body>
</html>
ListView1.aspx.cs
using System;
using System.Collections;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data;
using MySql.Data.MySqlClient;
public partial class ListView1 : System.Web.UI.Page
{
MySqlConnection objConn;
MySqlCommand objCmd;
protected void Page_Load(object sender, EventArgs e)
{
String strConnString;
strConnString = "Server=localhost;User Id=root; Password=root; Database=mydatabase; Pooling=false";
objConn = new MySqlConnection(strConnString);
objConn.Open();
if (!Page.IsPostBack)
{
BindData();
}
}
protected void BindData()
{
String strSQL;
strSQL = "SELECT * FROM customer";
MySqlDataReader dtReader;
objCmd = new MySqlCommand(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();
}
}
}
protected void Button1_Click(object sender, EventArgs e)
{
CheckBox chkCusID;
Label lblID;
int i;
lblText.Text = "";
for (i = 0; i <= myListView.Items.Count - 1; i++)
{
chkCusID = (CheckBox)myListView.Items[i].FindControl("chkCustomerID");
lblID = (Label)myListView.Items[i].FindControl("lblCustomerID");
if (chkCusID.Checked)
{
//*** Have lblID.Text ***//
this.lblText.Text = this.lblText.Text + "<br>" + lblID.Text;
}
}
}
}
Screenshot
ASP.NET & MySql.Data.MySqlClient
ASP.NET & MySQL
|
ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท
|
|
|
By : |
ThaiCreate.Com Team (บทความเป็นลิขสิทธิ์ของเว็บไทยครีเอทห้ามนำเผยแพร่ ณ เว็บไซต์อื่น ๆ) |
|
Score Rating : |
|
|
|
Create/Update Date : |
2009-09-22 19:45:52 /
2017-03-28 21:48:35 |
|
Download : |
|
|
Sponsored Links / Related |
|
|
|
|
|
|
|