INSERT,UPDATE,DELETE IN LIST VIEW
LIST.ASPX
<%@ Page
Language="C#"
AutoEventWireup="true"
CodeBehind="list.aspx.cs"
Inherits="listview.list"
%>
<!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>Untitled Page</title>
<body>
<form id="form1" runat="server">
<div>
<asp:ListView ID="ListView1"
runat="server"
DataKeyNames="ProductID"
onselectedindexchanging="ListView1_SelectIndexChanging"
OnItemInserting="ContactsListView_ItemInserted"
InsertItemPosition="LastItem"
OnItemDeleting="Delete_Item"
OnItemEditing="Edit_item"
OnItemUpdating="Update_item"
onitemcanceling="ListView1_ItemCanceling"
>
<LayoutTemplate>
<table style="border: solid 2px
#336699;" cellspacing="0" cellpadding="3" rules="all">
<tr style="background-color: #336699; color: White;">
<th>Select</th>
<th>Product ID</th>
<th>Product Name</th>
<th>Unit Price</th>
</tr>
<tbody>
<asp:PlaceHolder ID="itemPlaceHolder"
runat="server"
/>
</tbody>
</table>
</LayoutTemplate>
<ItemTemplate>
<tr>
<td>
<asp:LinkButton ID="lnkSelect"
Text="Select"
CommandName="Select"
runat="server"
/>
<asp:LinkButton ID="lnkdelete"
Text="Delete"
CommandName="Delete"
runat="server"/>
<asp:LinkButton ID="lnkedit"
Text="Edit"
CommandName="Edit"
runat="server"
>
</asp:LinkButton>
<td>
<asp:Label ID="lblpid" runat="server"
Text='<%# Eval("ProductID") %>' />
</td>
<td>
<asp:TextBox ID="txtname" runat="server" Text='<%# Bind("ProductName") %>'></asp:TextBox>
</td>
<td>
<asp:TextBox ID="txtprice" runat="server" Text='<%# Bind("UnitPrice") %>'></asp:TextBox>
</td>
</td>
</tr>
</ItemTemplate>
<InsertItemTemplate
>
<tr>
<td>
<asp:LinkButton ID="insertbutton"
runat="server"
CommandName="Insert"
Text="Insert"></asp:LinkButton>
</td>
<td>
<asp:TextBox ID="txtpid" runat="server" Text= '<%# Bind("ProductID") %>'></asp:TextBox>
</td>
<td>
<asp:TextBox ID="txtname" runat="server" Text='<%# Bind("ProductName") %>'></asp:TextBox>
</td>
<td>
<asp:TextBox ID="txtprice" runat="server" Text='<%# Bind("UnitPrice") %>'></asp:TextBox>
</td>
</tr>
</InsertItemTemplate>
<EditItemTemplate>
<tr>
<td><asp:LinkButton ID="lnkupdate"
runat="server"
CommandName="Update"
Text="Update"></asp:LinkButton>
<asp:LinkButton ID="lnkcancel"
runat="server"
CommandName="cancel"
Text="Cancel"></asp:LinkButton>
</td>
<td>
<asp:Label ID="lblpid" runat="server"
Text='<%# Eval("ProductID") %>' />
</td>
<td>
<asp:TextBox ID="txtname" runat="server" Text='<%# Bind("ProductName") %>'></asp:TextBox>
</td>
<td>
<asp:TextBox ID="txtprice" runat="server" Text='<%# Bind("UnitPrice") %>'></asp:TextBox>
</td>
</tr>
</EditItemTemplate>
<SelectedItemTemplate>
<tr style="background-color: #336699; color: White;">
<td>
<asp:LinkButton ID="lnkSelect1"
Text="Select"
CommandName="Select"
runat="server"
ForeColor="White"
/>
</td>
<td><%# Eval("ProductID")%></td>
<td><%# Eval("ProductName")%></td>
<td><%# Eval("UnitPrice")%></td>
</tr>
</SelectedItemTemplate>
</asp:ListView>
<asp:Label ID="Label1" runat="server"
Text="Label"></asp:Label>
</div>
</form>
</body>
</html>
LIST.ASPX.CS
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
namespace listview
{
public partial class list :
System.Web.UI.Page
{
SqlConnection con = new
SqlConnection("Data
Source=.;Initial Catalog=aravindh;Integrated Security=True");
protected void
Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
BindData();
}
}
private void
BindData()
{
SqlDataAdapter da = new
SqlDataAdapter("SELECT
ProductID, ProductName, UnitPrice FROM Products", con);
DataSet ds = new DataSet();
da.Fill(ds);
ListView1.DataSource = ds;
ListView1.DataBind();
}
protected void
ListView1_SelectIndexChanging(object sender, ListViewSelectEventArgs e)
{
ListView1.SelectedIndex = e.NewSelectedIndex;
string pid =
ListView1.SelectedDataKey.Value.ToString();
//
Label1.Text = "Selected Product ID:
" + pid;
BindData();
}
protected void
ContactsListView_ItemInserted(object sender, ListViewInsertEventArgs e)
{
string pid = "",
pname = "", unit = "";
TextBox txt = (e.Item.FindControl("txtpid")) as
TextBox;
if (txt != null)
pid = txt.Text;
txt = (e.Item.FindControl("txtname"))
as TextBox;
if (txt != null)
pname = txt.Text;
txt = (e.Item.FindControl("txtprice"))
as TextBox;
if (txt != null)
unit = txt.Text;
con.Open();
SqlCommand cmd = new
SqlCommand("insert
into Products values ('" + pid + "','"
+ pname + "','" + unit + "')", con);
cmd.ExecuteNonQuery();
BindData();
con.Close();
}
protected void
Update_item(object sender, ListViewUpdateEventArgs e)
{
string pid = "",
name = "", price = "";
Label lbl =
(ListView1.Items[e.ItemIndex].FindControl("lblpid"))
as Label;
if (lbl != null)
pid = lbl.Text;
TextBox txt =
(ListView1.Items[e.ItemIndex].FindControl("txtname"))
as TextBox;
if (txt != null)
name = txt.Text;
TextBox txt1 =
(ListView1.Items[e.ItemIndex].FindControl("txtprice"))
as TextBox;
if (txt1 != null)
price = txt1.Text;
con.Open();
SqlCommand cmd = new
SqlCommand("update
Products set [ProductName]='" + name + "',[UnitPrice]='"
+ price + "' where [ProductID]='"
+ pid + "'", con);
cmd.ExecuteNonQuery();
ListView1.EditIndex = -1;
BindData();
con.Close();
}
protected void
Delete_Item(object sender, ListViewDeleteEventArgs e)
{
string pid = "";
Label lbl =
(ListView1.Items[e.ItemIndex].FindControl("lblpid"))
as Label;
if (lbl != null)
pid = lbl.Text;
string DeleteQuery = "Delete
from Products WHERE [ProductID] = '" + pid + "'";
con.Open();
SqlCommand com = new
SqlCommand(DeleteQuery, con);
com.ExecuteNonQuery();
ListView1.EditIndex = -1;
BindData();
con.Close();
}
protected void
Edit_item(object sender, ListViewEditEventArgs e)
{
ListView1.EditIndex = e.NewEditIndex;
BindData();
}
protected void
ListView1_ItemCanceling(object sender, ListViewCancelEventArgs e)
{
ListView1.EditIndex = -1;
BindData();
}
}
}
DOWNLOAD THE SAMPLE CODE:LISTVIEW