The above event will actually populate
the editing row’s data into the input controls(In our case, its textbox) we have
specified in EditItemTemplate. We have also given 2 buttons in edit item
template, Update and Cancel. As the name suggest, update button will be used to
update the data and cancel button to cancel the edit.
The update event below uses LINQ query
to update the data into database.
protected void
dlEmployee_UpdateCommand(object source, DataListCommandEventArgs e)
{
string EmpID="";
Label lblTemp =
dlEmployee.Items[e.Item.ItemIndex].FindControl("EmpIDLabel1") as
Label;
if (lblTemp != null)
{
EmpID =
lblTemp.Text;
EmployeesDataClassesDataContext dbEmp = new
EmployeesDataClassesDataContext();
Employee empTemp = (from emp
in dbEmp.Employees
where
emp.EmployeeID == int.Parse(EmpID)
select
emp).Single();
TextBox txtTemp =
dlEmployee.Items[e.Item.ItemIndex].FindControl("EmpNameTextBox") as
TextBox;
if (txtTemp !=
null)
empTemp.EmployeeName =
txtTemp.Text;
txtTemp =
dlEmployee.Items[e.Item.ItemIndex].FindControl("txtDept") as TextBox;
if (txtTemp !=
null)
empTemp.Department =
txtTemp.Text;
txtTemp =
dlEmployee.Items[e.Item.ItemIndex].FindControl("txtAddress") as
TextBox;
if (txtTemp !=
null)
empTemp.Address =
txtTemp.Text;
txtTemp =
dlEmployee.Items[e.Item.ItemIndex].FindControl("txtCity") as TextBox;
if (txtTemp !=
null)
empTemp.City =
txtTemp.Text;
txtTemp =
dlEmployee.Items[e.Item.ItemIndex].FindControl("txtState") as
TextBox;
if (txtTemp !=
null)
empTemp.State =
txtTemp.Text;
txtTemp =
dlEmployee.Items[e.Item.ItemIndex].FindControl("txtCountry") as
TextBox;
if (txtTemp !=
null)
empTemp.Country =
txtTemp.Text;
dbEmp.SubmitChanges();
dlEmployee.EditItemIndex =
-1;
BindEmp();
}
}
protected void
dlEmployee_CancelCommand(object source, DataListCommandEventArgs e)
{
dlEmployee.EditItemIndex =
-1;
BindEmp();
}
Execute the application to see
edit/update in action.
Deleting Record in DataList
control
Like Edit and Update event, the
DataList control is packed with a delete event to delete a record. The below
delete event uses LINQ query to delete the current record.
protected void
dlEmployee_DeleteCommand(object source, DataListCommandEventArgs e)
{
EmployeesDataClassesDataContext
dbEmp = new EmployeesDataClassesDataContext();
Label lblTemp =
dlEmployee.Items[e.Item.ItemIndex].FindControl("EmpIDLabel") as
Label;
if (lblTemp != null)
{
string EmpID =
lblTemp.Text;
Employee empTemp = (from emp
in dbEmp.Employees
where
emp.EmployeeID == int.Parse(EmpID)
select
emp).Single();
dbEmp.Employees.DeleteOnSubmit(empTemp);
dbEmp.SubmitChanges();
BindEmp();
}
}
Execute and see delete feature in
action.
Insert Feature with DataList
control
By default, the DataList control does
not provide any template to insert new record like edit. But, we can
provide this feature in DataList control using the Footer Template and
ItemCommand of DataList control. We can construct the FooterTemplate to provide
insert feature using DataList control. Refer the below code,
<FooterTemplate>
<table>
<tr>
<td
width="100px">
<asp:Button
ID="btnInsertButton" runat="server" CommandName="Insert"
Text="Insert"
/>
</td>
<td
width="20px">
<asp:Label ID="EmpIDLabel1"
runat="server" />
</td>
<td
width="150px">
<asp:TextBox
ID="EmpNameTextBox" runat="server" Width="100px"/>
</td>
<td
width="80px">
<asp:TextBox ID="txtDept"
runat="server" Width="80px"/>
</td>
<td
width="100px">
<asp:TextBox ID="txtAddress"
runat="server" Width="100px"/>
</td>
<td
width="100px">
<asp:TextBox ID="txtCity"
runat="server" Width="100px"/>
</td>
<td
width="100px">
<asp:TextBox ID="txtState"
runat="server" Width="100px"/>
</td>
<td
width="100px">
<asp:TextBox ID="txtCountry"
runat="server" Width="100px"/>
</td>
</tr>
</table>
</FooterTemplate>
The Insert button in the above code
will generate an ItemCommand event where we can get the values and insert it
into the database using LINQ.
Refer the below code,
protected void
dlEmployee_ItemCommand(object source, DataListCommandEventArgs e)
{
string CommandName =
e.CommandName;
if (CommandName ==
"Insert")
{
EmployeesDataClassesDataContext dbEmp = new
EmployeesDataClassesDataContext();
Employee empTemp = new
Employee();
TextBox txtTemp =
e.Item.FindControl("EmpNameTextBox") as TextBox;
if (txtTemp !=
null)
empTemp.EmployeeName =
txtTemp.Text;
txtTemp =
e.Item.FindControl("txtDept") as TextBox;
if (txtTemp !=
null)
empTemp.Department =
txtTemp.Text;
txtTemp =
e.Item.FindControl("txtAddress") as TextBox;
if (txtTemp !=
null)
empTemp.Address =
txtTemp.Text;
txtTemp =
e.Item.FindControl("txtCity") as TextBox;
if (txtTemp !=
null)
empTemp.City =
txtTemp.Text;
txtTemp =
e.Item.FindControl("txtState") as TextBox;
if (txtTemp !=
null)
empTemp.State =
txtTemp.Text;
txtTemp =
e.Item.FindControl("txtCountry") as TextBox;
if (txtTemp !=
null)
empTemp.Country =
txtTemp.Text;
dbEmp.Employees.InsertOnSubmit(empTemp);
dbEmp.SubmitChanges();
BindEmp();
}
}
Execute the project and see it in
action. You can also move this insert feature outside on the DataList control if
it is required.
Download the source attached with this
article and see it in action.
|