ASP.Net ตอนที่ 6 : Delete Data (LINQ, Entity Framework) |
ASP.Net ตอนที่ 6 : Delete Data (LINQ, Entity Framework) ในตัวอย่างของตอนที่ 6 นี้จะเป็นการใช้ Entity Framework อ่านข้อมูลจาก Model Entities แสดงผลบน GridView และคลิกเลือกรายการลบน GridView เพื่อลบหรือ Delete ข้อมูล
Example : การใช้งาน LINQ to Entities แสดงผลบน GridView และเลือกรายการที่จะ Delete (ลบ)
Code Default.aspx (HTML)
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="frmMain" runat="server">
<asp:GridView ID="myGridView" runat="server" AutoGenerateColumns="False"
DataKeyNames="CUSTOMER_ID"
OnRowDataBound="myGridView_RowDataBound" onRowDeleting="myGridView_RowDeleting" >
<Columns>
<asp:TemplateField HeaderText="Customer ID">
<ItemTemplate>
<asp:Label ID="lblCustomerID" runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:Label ID="lblName" runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Email">
<ItemTemplate>
<asp:Label ID="lblEmail" runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Country Code">
<ItemTemplate>
<asp:Label ID="lblCountryCode" runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Budget">
<ItemTemplate>
<asp:Label ID="lblBudget" runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Used">
<ItemTemplate>
<asp:Label ID="lblUsed" runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Delete">
<ItemTemplate>
<asp:LinkButton ID="lnkDelete" CommandName="Delete" runat="server">Delete</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</form>
</body>
</html>
Code Default.aspx.cs (C#)
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
BindData();
}
}
protected void BindData()
{
// Create new entities Object
using (var db = new myDatabaseEntities())
{
// Get data from CUSTOMER
var ds = (from c in db.CUSTOMER
select new
{
c.CUSTOMER_ID,
c.NAME,
c.EMAIL,
c.COUNTRY_CODE,
c.BUDGET,
c.USED
}).ToList();
// Assign to GridView
if (ds.Count > 0)
{
this.myGridView.DataSource = ds;
this.myGridView.DataBind();
}
}
}
protected void myGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow && DataBinder.Eval(e.Row.DataItem, "CUSTOMER_ID") != null)
{
//*** CustomerID ***//
Label lblCustomerID = (Label)(e.Row.FindControl("lblCustomerID"));
if (lblCustomerID != null)
{
lblCustomerID.Text = DataBinder.Eval(e.Row.DataItem, "CUSTOMER_ID").ToString();
}
//*** Name ***//
Label lblName = (Label)(e.Row.FindControl("lblName"));
if (lblName != null)
{
lblName.Text = DataBinder.Eval(e.Row.DataItem, "NAME").ToString();
}
//*** Email ***//
Label lblEmail = (Label)(e.Row.FindControl("lblEmail"));
if (lblEmail != null)
{
lblEmail.Text = DataBinder.Eval(e.Row.DataItem, "EMAIL").ToString();
}
//*** CountryCode ***//
Label lblCountryCode = (Label)(e.Row.FindControl("lblCountryCode"));
if (lblCountryCode != null)
{
lblCountryCode.Text = DataBinder.Eval(e.Row.DataItem, "COUNTRY_CODE").ToString();
}
//*** Budget ***//
Label lblBudget = (Label)(e.Row.FindControl("lblBudget"));
if (lblBudget != null)
{
lblBudget.Text = DataBinder.Eval(e.Row.DataItem, "BUDGET").ToString();
}
//*** Used ***//
Label lblUsed = (Label)(e.Row.FindControl("lblUsed"));
if (lblUsed != null)
{
lblUsed.Text = DataBinder.Eval(e.Row.DataItem, "USED").ToString();
}
//*** Delete ***//
LinkButton lnkDelete = (LinkButton)(e.Row.FindControl("lnkDelete"));
if (lnkDelete != null)
{
lnkDelete.Attributes.Add("OnClick", "return confirm('Delete Record " + lblCustomerID.Text + " ?');");
}
}
}
protected void myGridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
// Get Customer ID from DataKey
string strCustomerID = this.myGridView.DataKeys[e.RowIndex].Value.ToString();
// Create new entities from Entities
using (var db = new myDatabaseEntities())
{
// Delete CUSTOMER
var del = (from c in db.CUSTOMER
where c.CUSTOMER_ID == strCustomerID
select c).FirstOrDefault();
if (del != null)
{
db.CUSTOMER.Remove(del);
}
db.SaveChanges();
BindData();
ClientScript.RegisterStartupScript(this.GetType(), "PopupScript", "<script>alert('" + string.Format("Delete {0} Successfully.", strCustomerID) + "');</script>");
}
}
}
Screenshot
เลือกรายการที่ต้องการจะลบ และคลิกที Delete ซึ่งระบบจะแสดง Dialog ให้ Confirm การ Delete ข้อมูล
หลังจากที่ Delete เรียบร้อยแล้วจะทำการ Bind ข้อมูลใน GridView ใหม่
Code Default.aspx.vb (VB.Net)
Public Class _Default
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
BindData()
End If
End Sub
Protected Sub BindData()
' Create new entities Object
Using db = New myDatabaseEntities()
' Get data from CUSTOMER
Dim ds = (From c In db.CUSTOMER
Select New With { _
c.CUSTOMER_ID, _
c.NAME, _
c.EMAIL, _
c.COUNTRY_CODE, _
c.BUDGET, _
c.USED _
}).ToList()
' Assign to GridView
If ds.Count > 0 Then
Me.myGridView.DataSource = ds
Me.myGridView.DataBind()
End If
End Using
End Sub
Protected Sub myGridView_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles myGridView.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow And Not IsNothing(DataBinder.Eval(e.Row.DataItem, "CUSTOMER_ID")) Then
'*** CustomerID ***'
Dim lblCustomerID As Label = DirectCast(e.Row.FindControl("lblCustomerID"), Label)
If Not IsNothing(lblCustomerID) Then
lblCustomerID.Text = DataBinder.Eval(e.Row.DataItem, "CUSTOMER_ID").ToString()
End If
'*** Name ***'
Dim lblName As Label = DirectCast(e.Row.FindControl("lblName"), Label)
If Not IsNothing(lblName) Then
lblName.Text = DataBinder.Eval(e.Row.DataItem, "NAME").ToString()
End If
'*** Email ***'
Dim lblEmail As Label = DirectCast(e.Row.FindControl("lblEmail"), Label)
If Not IsNothing(lblEmail) Then
lblEmail.Text = DataBinder.Eval(e.Row.DataItem, "EMAIL").ToString()
End If
'*** CountryCode ***'
Dim lblCountryCode As Label = DirectCast(e.Row.FindControl("lblCountryCode"), Label)
If Not IsNothing(lblCountryCode) Then
lblCountryCode.Text = DataBinder.Eval(e.Row.DataItem, "COUNTRY_CODE").ToString()
End If
'*** Budget ***'
Dim lblBudget As Label = DirectCast(e.Row.FindControl("lblBudget"), Label)
If Not IsNothing(lblBudget) Then
lblBudget.Text = DataBinder.Eval(e.Row.DataItem, "BUDGET").ToString()
End If
'*** Used ***'
Dim lblUsed As Label = DirectCast(e.Row.FindControl("lblUsed"), Label)
If Not IsNothing(lblUsed) Then
lblUsed.Text = DataBinder.Eval(e.Row.DataItem, "USED").ToString()
End If
'*** Delete ***'
Dim lnkDelete As LinkButton = DirectCast(e.Row.FindControl("lnkDelete"), LinkButton)
If Not IsNothing(lnkDelete) Then
lnkDelete.Attributes.Add("OnClick", "return confirm('Delete Record " + lblCustomerID.Text + " ?');")
End If
End If
End Sub
Protected Sub myGridView_RowDeleting(sender As Object, e As GridViewDeleteEventArgs) Handles myGridView.RowDeleting
' Get Customer ID from DataKey
Dim strCustomerID As String = Me.myGridView.DataKeys(e.RowIndex).Value.ToString()
' Create new entities from Entities
Using db = New myDatabaseEntities()
' Delete CUSTOMER
Dim del = (From c In db.CUSTOMER
Where c.CUSTOMER_ID = strCustomerID
Select c).FirstOrDefault()
If Not IsNothing(del) Then
db.CUSTOMER.Remove(del)
End If
db.SaveChanges()
BindData()
ClientScript.RegisterStartupScript(Me.GetType, "PopupScript", "<script>alert('" + String.Format("Delete {0} Successfully.", strCustomerID) + "');</script>")
End Using
End Sub
End Class
|
ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท
|
|
|
By : |
ThaiCreate.Com Team (บทความเป็นลิขสิทธิ์ของเว็บไทยครีเอทห้ามนำเผยแพร่ ณ เว็บไซต์อื่น ๆ) |
|
Score Rating : |
|
|
|
Create/Update Date : |
2015-10-02 21:22:04 /
2017-03-24 22:59:00 |
|
Download : |
No files |
|
Sponsored Links / Related |
|
|
|
|
|
|
|