(C#) ASP.NET DataGrid Control - VS .NET 2003 (FX 1.1) |
(C#) ASP.NET DataGrid Control - Visual Studio .NET 2003 (Framework 1.1) จากตัวอย่างก่อนหน้านี้จะเป็นการใช้งาน DataGrid และเขียนในรูปแบบของ ASP Style คือเขียนร่วมกับกับส่วนของ HTML Element แต่ในตัวอย่างนี้ผมได้ยกการเขียนร่วมกับ Visual Studio .NET 2003 ซึ่ง Run บน Framework 1.1 ในรูปแบบของ Code-Behind แยกในส่วนของ Tag และ Code ไว้คนล่ะส่วน
Code นี้จะเป็นการ Add,Edit,Update และ Delete ข้อมูลในฐานข้อมูล
Language Code : VB.NET || C#
Framework : 1.1
DataGrid1.aspx
<%@ Page language="c#" Codebehind="DataGrid1.aspx.cs" AutoEventWireup="false" Inherits="DataGrid1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>ThaiCreate.Com ASP.NET - DataGrid</title>
<meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<asp:DataGrid id="myDataGrid" runat="server" AutoGenerateColumns="False" ShowFooter="True" DataKeyField="CustomerID">
<Columns>
<asp:TemplateColumn HeaderText="CustomerID">
<ItemTemplate>
<asp:Label id="lblCustomerID" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.CustomerID") %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id="txtEditCustomerID" size="5" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.CustomerID") %>'>
</asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox id="txtAddCustomerID" size="5" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Name">
<ItemTemplate>
<asp:Label id="lblName" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Name") %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id="txtEditName" size="10" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Name") %>'>
</asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox id="txtAddName" size="10" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Email">
<ItemTemplate>
<asp:Label id="lblEmail" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Email") %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id="txtEditEmail" size="20" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Email") %>'>
</asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox id="txtAddEmail" size="20" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="CountryCode">
<ItemTemplate>
<asp:Label id="lblCountryCode" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.CountryCode") %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id="txtEditCountryCode" size="2" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.CountryCode") %>'>
</asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox id="txtAddCountryCode" size="2" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Budget">
<ItemTemplate>
<asp:Label id="lblBudget" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Budget") %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id="txtEditBudget" size="6" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Budget") %>'>
</asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox id="txtAddBudget" size="6" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Used">
<ItemTemplate>
<asp:Label id="lblUsed" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Used") %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id="txtEditUsed" size="6" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Used") %>'>
</asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox id="txtAddUsed" size="6" runat="server"></asp:TextBox>
<asp:Button id="btnAdd" runat="server" Text="Add" CommandName="Add"></asp:Button>
</FooterTemplate>
</asp:TemplateColumn>
<asp:EditCommandColumn ButtonType="LinkButton" UpdateText="Update" CancelText="Cancel" EditText="Edit"
HeaderText="Modify"></asp:EditCommandColumn>
<asp:ButtonColumn Text="Delete" CommandName="Delete" HeaderText="Delete"></asp:ButtonColumn>
</Columns>
</asp:DataGrid>
</form>
</body>
</HTML>
DataGrid1.aspx.cs
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;
public class DataGrid1 : System.Web.UI.Page
{
override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
this.myDataGrid.ItemCommand += new DataGridCommandEventHandler(this.myDataGrid_ItemCommand);
this.myDataGrid.CancelCommand += new DataGridCommandEventHandler(this.myDataGrid_CancelCommand);
this.myDataGrid.EditCommand += new DataGridCommandEventHandler(this.modEditCommand);
this.myDataGrid.UpdateCommand += new DataGridCommandEventHandler(this.modUpdateCommand);
this.myDataGrid.DeleteCommand += new DataGridCommandEventHandler(this.modDeleteCommand);
}
OleDbConnection objConn;
OleDbCommand objCmd;
String strSQL;
protected void Page_Load(object sender, System.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();
}
}
protected void BindData()
{
String strSQL;
strSQL = "SELECT * FROM customer";
OleDbDataReader dtReader;
objCmd = new OleDbCommand(strSQL, objConn);
dtReader = objCmd.ExecuteReader();
//*** BindData to DataGrid ***//
myDataGrid.DataSource = dtReader;
myDataGrid.DataBind();
dtReader.Close();
dtReader = null;
}
protected void Page_UnLoad(object sender, EventArgs e)
{
objConn.Close();
objConn = null;
}
protected void myDataGrid_CancelCommand(object source, DataGridCommandEventArgs e)
{
myDataGrid.EditItemIndex = -1;
myDataGrid.ShowFooter = true;
BindData();
}
protected void modEditCommand(object source, DataGridCommandEventArgs e)
{
myDataGrid.EditItemIndex = e.Item.ItemIndex;
myDataGrid.ShowFooter = false;
BindData();
}
protected void modDeleteCommand(object source, DataGridCommandEventArgs e)
{
strSQL = "DELETE FROM customer WHERE CustomerID = '"+ myDataGrid.DataKeys[e.Item.ItemIndex]+"'";
objCmd = new OleDbCommand(strSQL, objConn);
objCmd.ExecuteNonQuery();
myDataGrid.EditItemIndex = -1;
BindData();
}
protected void modUpdateCommand(object source, DataGridCommandEventArgs e)
{
//*** CustomerID ***//
TextBox txtCustomerID = (TextBox)e.Item.FindControl("txtEditCustomerID");
//*** Email ***//
TextBox txtName = (TextBox)e.Item.FindControl("txtEditName");
//*** Name ***//
TextBox txtEmail = (TextBox)e.Item.FindControl("txtEditEmail");
//*** CountryCode ***//
TextBox txtCountryCode = (TextBox)e.Item.FindControl("txtEditCountryCode");
//*** Budget ***//
TextBox txtBudget = (TextBox)e.Item.FindControl("txtEditBudget");
//*** Used ***//
TextBox txtUsed = (TextBox)e.Item.FindControl("txtEditUsed");
strSQL = "UPDATE customer SET CustomerID = '" + txtCustomerID.Text + "' " +
" ,Name = '" + txtName.Text + "' " +
" ,Email = '" + txtEmail.Text + "' " +
" ,CountryCode = '" + txtCountryCode.Text + "' " +
" ,Budget = '" + txtBudget.Text + "' " +
" ,Used = '" + txtUsed.Text + "' " +
" WHERE CustomerID = '" + myDataGrid.DataKeys[e.Item.ItemIndex] + "'";
objCmd = new OleDbCommand(strSQL, objConn);
objCmd.ExecuteNonQuery();
myDataGrid.EditItemIndex = -1;
myDataGrid.ShowFooter = true;
BindData();
}
protected void myDataGrid_ItemCommand(object source, DataGridCommandEventArgs e)
{
if(e.CommandName == "Add")
{
//*** CustomerID ***//
TextBox txtCustomerID = (TextBox)e.Item.FindControl("txtAddCustomerID");
//*** Email ***//
TextBox txtName = (TextBox)e.Item.FindControl("txtAddName");
//*** Name ***//
TextBox txtEmail = (TextBox)e.Item.FindControl("txtAddEmail");
//*** CountryCode ***//
TextBox txtCountryCode = (TextBox)e.Item.FindControl("txtAddCountryCode");
//*** Budget ***//
TextBox txtBudget = (TextBox)e.Item.FindControl("txtAddBudget");
//*** Used ***//
TextBox txtUsed = (TextBox)e.Item.FindControl("txtAddUsed");
strSQL = "INSERT INTO customer (CustomerID,Name,Email,CountryCode,Budget,Used) " +
" VALUES ('" + txtCustomerID.Text + "','" + txtName.Text + "','" + txtEmail.Text + "' " +
" ,'" + txtCountryCode.Text + "','" + txtBudget.Text + "','" + txtUsed.Text + "') ";
objCmd = new OleDbCommand(strSQL, objConn);
objCmd.ExecuteNonQuery();
BindData();
}
}
}
Screenshot
|
ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท
|
|
|
By : |
ThaiCreate.Com Team (บทความเป็นลิขสิทธิ์ของเว็บไทยครีเอทห้ามนำเผยแพร่ ณ เว็บไซต์อื่น ๆ) |
|
Score Rating : |
|
|
|
Create/Update Date : |
2008-11-13 07:08:42 /
2017-03-28 21:09:33 |
|
Download : |
|
|
Sponsored Links / Related |
|
|
|
|
|
|
|