(C#) ASP.NET DataList Control - MySQL Database - MySql.Data.MySqlClient |
(C#) ASP.NET DataList & MySQL เป็นตัวอย่างการเขียน ASP.NET โดยใช้ Tool ชื่อ DataList กับฐานข้อมูล MySQL ใช้ NameSpace ของ MySql.Data.MySqlClient เพื่อนำข้อมูลในฐานข้อมูลมาใช้ในรูปแบบต่าง ๆ ตัวอย่างนี้ผมพัฒนาบน .NET Framework 2 และ 3 (Visual Studio 2005,2008)
Language Code : VB.NET || C#
Framework : 2,3,4
DataList1.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DataList1.aspx.cs" Inherits="DataList1" %>
<!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 id="Head1" runat="server">
<title>ThaiCreate.Com ASP.NET - DataList & MySQL</title>
</head>
<body>
<form id="form1" runat="server" enctype="multipart/form-data">
<asp:DataList id="myDataList"
runat="server" RepeatColumns="2"
cellpadding="2"
cellspacing="2"
borderstyle="inset" OnCancelCommand="myDataList_CancelCommand" OnEditCommand="myDataList_EditCommand" OnItemDataBound="myDataList_ItemDataBound" OnUpdateCommand="myDataList_UpdateCommand">
<HeaderTemplate>
<b>My Category</b><br/>
</HeaderTemplate>
<ItemTemplate>
<div style="width:100px" align="center">
<asp:Image id="imgPicture" runat="server"></asp:Image>
<br />
<asp:HyperLink id="hplCategory" runat="server"></asp:HyperLink>
<br />
<asp:LinkButton id="lnkEdit" runat="server" CommandName="Edit">Edit</asp:LinkButton>
</div>
</ItemTemplate>
<EditItemTemplate>
<asp:Label id="lblCateID" runat="server" Visible = "False" text='<%# DataBinder.Eval(Container.DataItem, "CategoryID") %>'></asp:Label>
<asp:TextBox id="txtCategory" runat="server" text='<%# DataBinder.Eval(Container.DataItem, "CategoryName") %>'></asp:TextBox><br />
<input id="filPicture" type="file" runat="server"><br />
<asp:LinkButton id="lnkUpdate" runat="server" CommandName="Update">Update</asp:LinkButton>
<asp:LinkButton id="lnkCancel" runat="server" CommandName="Cancel">Cancel</asp:LinkButton>
</EditItemTemplate>
</asp:DataList>
</form>
</body>
</html>
DataList1.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using MySql.Data.MySqlClient;
public partial class DataList1 : System.Web.UI.Page
{
MySqlConnection objConn;
MySqlCommand objCmd;
String strSQL;
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 category";
MySqlDataReader dtReader;
objCmd = new MySqlCommand(strSQL, objConn);
dtReader = objCmd.ExecuteReader();
//*** BindData to DataList ***//
myDataList.DataSource = dtReader;
myDataList.DataBind();
dtReader.Close();
dtReader = null;
}
protected void Page_UnLoad()
{
objConn.Close();
objConn = null;
}
protected void myDataList_ItemDataBound(object sender, DataListItemEventArgs e)
{
//*** Image ***//
Image img = (Image)(e.Item.FindControl("imgPicture"));
if (img != null)
{
img.ImageUrl = (string)DataBinder.Eval(e.Item.DataItem, "Picture");
//img.Attributes.Add("OnClick","window.location='https://www.thaicreate.com?Cateid=" +
//DataBinder.Eval(e.Item.DataItem, "CategoryID").ToString() + "'");
//img.Style.Add("cursor","hand");
}
//*** HyperLink ***//
HyperLink hplCate = (HyperLink)(e.Item.FindControl("hplCategory"));
if (hplCate != null)
{
hplCate.Text = (string)DataBinder.Eval(e.Item.DataItem, "CategoryName");
hplCate.ToolTip = (string)DataBinder.Eval(e.Item.DataItem, "CategoryName");
hplCate.NavigateUrl = "https://www.thaicreate.com?Cateid=" + DataBinder.Eval(e.Item.DataItem, "CategoryID").ToString();
}
}
protected void myDataList_EditCommand(object source, DataListCommandEventArgs e)
{
myDataList.EditItemIndex = e.Item.ItemIndex;
BindData();
}
protected void myDataList_CancelCommand(object source, DataListCommandEventArgs e)
{
myDataList.EditItemIndex = -1;
BindData();
}
protected void myDataList_UpdateCommand(object source, DataListCommandEventArgs e)
{
//*** CategoryID ***//
Label lblCateID = (Label)(e.Item.FindControl("lblCateID"));
//*** Category ***//
TextBox txtCategory = (TextBox)(e.Item.FindControl("txtCategory"));
strSQL = "UPDATE category SET CategoryName = '" + txtCategory.Text + "' " +
" WHERE CategoryID = " + lblCateID.Text + " ";
objCmd = new MySqlCommand(strSQL, objConn);
objCmd.ExecuteNonQuery();
//*** If Select File Upload ***//
HtmlInputFile filPicture = (HtmlInputFile)(e.Item.FindControl("filPicture"));
String strFileName;
if (filPicture.PostedFile.FileName != "")
{
strFileName = System.IO.Path.GetFileName(filPicture.Value);
filPicture.PostedFile.SaveAs(Server.MapPath("images/" + strFileName));
strSQL = "UPDATE category SET Picture = 'images/" + strFileName + "' " +
" WHERE CategoryID = " + lblCateID.Text + " ";
objCmd = new MySqlCommand(strSQL, objConn);
objCmd.ExecuteNonQuery();
}
myDataList.EditItemIndex = -1;
BindData();
}
}
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:30:55 /
2017-03-28 21:30:21 |
|
Download : |
|
|
Sponsored Links / Related |
|
|
|
|
|
|
|