Edit/Update/Delete/Insert feature without
using DataSource controls
1. Drag a ListView control inside a webform as we did in previous
section. Specify the LayoutTemplate, ItemTemplate, EditItemTemplate,
InsertItemTemplate that displays the ListView similar to the previous section.
<asp:ListView ID="lvEmployee"
runat="server"
onitemediting="lvEmployee_ItemEditing"
onitemupdating="lvEmployee_ItemUpdating"
onitemcanceling="lvEmployee_ItemCanceling"
onitemdeleting="lvEmployee_ItemDeleting"
InsertItemPosition="LastItem"
oniteminserting="lvEmployee_ItemInserting">
<LayoutTemplate>
<table id="Table1"
runat="server">
<tr id="Tr1"
runat="server">
<td id="Td1"
runat="server">
<table
ID="itemPlaceholderContainer" runat="server" border="0" style="">
<tr
id="Tr2" runat="server" style="">
<th
id="Th1" runat="server">
</th>
<th
id="Th2" runat="server">
EmpID</th>
<th
id="Th3" runat="server">
EmpName</th>
<th
id="Th4" runat="server">
Department</th>
<th
id="Th5" runat="server">
Age</th>
<th
id="Th6" runat="server">
Address</th>
</tr>
<tr
ID="itemPlaceholder" runat="server">
</tr>
</table>
</td>
</tr>
<tr id="Tr3"
runat="server">
<td id="Td2"
runat="server" style="">
</td>
</tr>
</table>
</LayoutTemplate>
<ItemTemplate>
<tr style="">
<td>
<asp:Button
ID="DeleteButton" runat="server" CommandName="Delete"
Text="Delete"
/>
<asp:Button
ID="EditButton" runat="server" CommandName="Edit" Text="Edit" />
</td>
<td>
<asp:Label
ID="EmpIDLabel" runat="server" Text='<%# Eval("EmpID") %>'
/>
</td>
<td>
<asp:Label
ID="EmpNameLabel" runat="server" Text='<%# Eval("EmpName") %>'
/>
</td>
<td>
<asp:Label
ID="DepartmentLabel" runat="server"
Text='<%#
Eval("Department") %>' />
</td>
<td>
<asp:Label
ID="AgeLabel" runat="server" Text='<%# Eval("Age") %>' />
</td>
<td>
<asp:Label
ID="AddressLabel" runat="server" Text='<%# Eval("Address") %>'
/>
</td>
</tr>
</ItemTemplate>
<EditItemTemplate>
<tr
style="">
<td>
<asp:Button
ID="UpdateButton" runat="server" CommandName="Update"
Text="Update"
/>
<asp:Button
ID="CancelButton" runat="server" CommandName="Cancel"
Text="Cancel"
/>
</td>
<td>
<asp:Label
ID="EmpIDLabel1" runat="server" Text='<%# Eval("EmpID") %>'
/>
</td>
<td>
<asp:TextBox
ID="EmpNameTextBox" runat="server" Text='<%# Bind("EmpName") %>'
/>
</td>
<td>
<asp:TextBox
ID="DepartmentTextBox" runat="server"
Text='<%#
Bind("Department") %>' />
</td>
<td>
<asp:TextBox
ID="AgeTextBox" runat="server" Text='<%# Bind("Age") %>' />
</td>
<td>
<asp:TextBox
ID="AddressTextBox" runat="server" Text='<%# Bind("Address") %>'
/>
</td>
</tr>
</EditItemTemplate>
<InsertItemTemplate>
<tr
style="">
<td>
<asp:Button
ID="InsertButton" runat="server" CommandName="Insert"
Text="Insert"
/>
<asp:Button
ID="CancelButton" runat="server" CommandName="Cancel"
Text="Clear"
/>
</td>
<td>
</td>
<td>
<asp:TextBox
ID="EmpNameTextBox" runat="server" Text='<%# Bind("EmpName") %>'
/>
</td>
<td>
<asp:TextBox
ID="DepartmentTextBox" runat="server"
Text='<%#
Bind("Department") %>' />
</td>
<td>
<asp:TextBox
ID="AgeTextBox" runat="server" Text='<%# Bind("Age") %>' />
</td>
<td>
<asp:TextBox
ID="AddressTextBox" runat="server" Text='<%# Bind("Address") %>'
/>
</td>
</tr>
</InsertItemTemplate>
</asp:ListView>
2. In the codebehind file, assign the DataTable or DataSet to the
DataSource Property of the ListView control, like we do for a GridView control.
protected void Page_Load(object
sender, EventArgs e)
{
if (!IsPostBack)
{
lvEmployee.DataSource =
GetEmployee("Select * from Employee");
lvEmployee.DataBind();
}
}
public DataTable GetEmployee(string
query)
{
SqlConnection con = new
SqlConnection(ConfigurationManager.ConnectionStrings["DatabaseConnectionString"].ConnectionString);
SqlDataAdapter ada = new
SqlDataAdapter(query, con);
DataTable dtEmp = new
DataTable();
ada.Fill(dtEmp);
return dtEmp;
}
3. We can write our own code to edit, update, delete and insert in the
corresponding events of the ListView control. Refer the below code.
protected void
lvEmployee_ItemEditing(object sender, ListViewEditEventArgs e)
{
lvEmployee.EditIndex =
e.NewEditIndex;
lvEmployee.DataSource =
GetEmployee("Select * from Employee");
lvEmployee.DataBind();
}
protected void
lvEmployee_ItemUpdating(object sender, ListViewUpdateEventArgs e)
{
string empid = "", name = "",
dept = "", age = "", address = "";
Label lbl =
(lvEmployee.Items[e.ItemIndex].FindControl("EmpIDLabel1")) as Label;
if (lbl != null)
empid =
lbl.Text;
TextBox txt =
(lvEmployee.Items[e.ItemIndex].FindControl("EmpNameTextBox")) as
TextBox;
if (txt != null)
name =
txt.Text;
txt =
(lvEmployee.Items[e.ItemIndex].FindControl("DepartmentTextBox")) as
TextBox;
if (txt != null)
dept =
txt.Text;
txt =
(lvEmployee.Items[e.ItemIndex].FindControl("AgeTextBox")) as TextBox;
if (txt != null)
age = txt.Text;
txt =
(lvEmployee.Items[e.ItemIndex].FindControl("AddressTextBox")) as
TextBox;
if (txt != null)
address =
txt.Text;
string UpdateQuery = "UPDATE
[Employee] SET [EmpName] = '" + name + "', [Department] = '" + dept + "', [Age]
= '" + age + "', [Address] = '" + address + "' WHERE [EmpID] = '" + empid +
"'";
SqlConnection con = new
SqlConnection(ConfigurationManager.ConnectionStrings["DatabaseConnectionString"].ConnectionString);
con.Open();
SqlCommand com = new
SqlCommand(UpdateQuery, con);
com.ExecuteNonQuery();
con.Close();
lvEmployee.EditIndex =
-1;
lvEmployee.DataSource =
GetEmployee("Select * from Employee");
lvEmployee.DataBind();
}
protected void
lvEmployee_ItemCanceling(object sender, ListViewCancelEventArgs e)
{
lvEmployee.EditIndex =
-1;
lvEmployee.DataSource =
GetEmployee("Select * from Employee");
lvEmployee.DataBind();
}
protected void
lvEmployee_ItemDeleting(object sender, ListViewDeleteEventArgs e)
{
string empid = "";
Label lbl =
(lvEmployee.Items[e.ItemIndex].FindControl("EmpIDLabel")) as Label;
if (lbl != null)
empid = lbl.Text;
string DeleteQuery = "Delete from
Employee WHERE [EmpID] = '" + empid + "'";
SqlConnection con = new
SqlConnection(ConfigurationManager.ConnectionStrings["DatabaseConnectionString"].ConnectionString);
con.Open();
SqlCommand com = new
SqlCommand(DeleteQuery, con);
com.ExecuteNonQuery();
con.Close();
lvEmployee.EditIndex =
-1;
lvEmployee.DataSource =
GetEmployee("Select * from Employee");
lvEmployee.DataBind();
}
protected void
lvEmployee_ItemInserting(object sender, ListViewInsertEventArgs e)
{
string name = "", dept = "", age =
"", address = "";
TextBox txt =
(e.Item.FindControl("EmpNameTextBox")) as TextBox;
if (txt != null)
name = txt.Text;
txt =
(e.Item.FindControl("DepartmentTextBox")) as TextBox;
if (txt != null)
dept = txt.Text;
txt =
(e.Item.FindControl("AgeTextBox")) as TextBox;
if (txt != null)
age = txt.Text;
txt =
(e.Item.FindControl("AddressTextBox")) as TextBox;
if (txt != null)
address = txt.Text;
string INSERTQuery = "INSERT INTO
[Employee] (EmpName,Department,Age,Address) VALUES ('" + name + "','" + dept +
"','" + age + "','" + address + "')";
SqlConnection con = new
SqlConnection(ConfigurationManager.ConnectionStrings["DatabaseConnectionString"].ConnectionString);
con.Open();
SqlCommand com = new
SqlCommand(INSERTQuery, con);
com.ExecuteNonQuery();
con.Close();
lvEmployee.EditIndex = -1;
lvEmployee.DataSource =
GetEmployee("Select * from Employee");
lvEmployee.DataBind();
}
|