ASP.NET Web Form is still used by a large number of developers for rapid application development. This tutorial is a CRUD (Create, Retrieve, Update, and Delete) Web Application with the "search for a record" functionality. The records are displayed in a ListView Control which involves model binding. Data Pager is used for pagination of the records.
The data-bound controls (such as the ListView, the Repeater, the GridView, the DetailsView, and the FormView) are Strongly-Typed Data-Bound Controls.
Software required for this tutorial is -
- Microsoft .NET Framework 4.5.1or any higher version.
- Microsoft Visual Studio 2013 or Visual Studio Express 2013 for Web or any higher version of Microsoft Visual Studio above 2012
- Entity Framework 6.0
Lunch Visual Studio and create a new ASP.NET project. Name it as List View Search.
Go to File >> New Project.
The project looks like below.
Default.aspx Page
- <%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="ListViewSearch._Default" %>
- <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
- <h1> Employee Manager</h1>
- <div>
- <asp:TextBox ID="txtSearch" AutoPostBack="true" runat="server" ToolTip="Search by part of EmployeeName"></asp:TextBox>
- <asp:Button ID="btnSearch" Text="Search" runat="server" CausesValidation="false" OnClick="btnSearch_Click" />
- </div>
- <div>
- <asp:ListView ID="EmployeeList" ItemType="ListViewSearch.Models.Employee" SelectMethod="GetSearchedItems" DataKeyNames="Id" UpdateMethod="UpdateEmployee" DeleteMethod="DeleteEmployee" OnSelectedIndexChanged="EmployeeList_SelectedIndexChanged" runat="server">
- <EmptyDataTemplate>
- <table>
- <tr>
- <td>Sorry,No Record Found.Try Again!</td>
- </tr>
- </table>
- </EmptyDataTemplate>
- <EmptyItemTemplate>
- <td />
- </EmptyItemTemplate>
- <LayoutTemplate>
- <div class="outerContainer">
- <table id="EmployeeTable">
- <caption style="color:deeppink"><b>Employees</b></caption>
- <tr style="background-color:crimson">
- <th>EmpId</th>
- <th>FirstNam</th>
- <th>LastName</th>
- <th>Salary</th>
- <th>ContactNo</th>
- <th>DateOfBirth</th>
- <th style="width:140px;"> Action</th>
- </tr>
- <tr runat="server" id="itemPlaceholder"></tr>
- </table>
- </div>
- </LayoutTemplate>
- <ItemTemplate>
- <tr>
- <td>
- <%# Item.Id %>
- </td>
- <td>
- <%# Item.FirstName %>
- </td>
- <td>
- <%# Item.LastName %>
- </td>
- <td>
- <%# Item.Salary %>
- </td>
- <td>
- <%# Item.ContactNo %>
- </td>
- <td>
- <%# Item.Date_Of_Birth %>
- </td>
- <td>
- <asp:Button BackColor="#ff3399" CommandName="Edit" Text="Edit" tooltip="Edit a record" CausesValidation="false" runat="server" />
- <asp:Button BackColor="#ff3399" CommandName="Delete" tooltip="Delete a record" CausesValidation="false" onclientclick="javascript:return confirm('Are you sure to delete record?')" Text="Delete" runat="server" />
- </td>
- </tr>
- </ItemTemplate>
- <EditItemTemplate>
- <tr>
- <input type="hidden" name="EmpId" value="<%# Item.Id %>" />
- <td>EmpId:
- <asp:TextBox ID="txtEmpId" runat="server" TextMode="SingleLine" Text='<%# BindItem.Id%>' />
- </td>
- <td>FirstName:
- <asp:TextBox ID="txtFName" runat="server" TextMode="SingleLine" Text='<%# BindItem.FirstName %>' />
- </td>
- <td>LastName:
- <asp:TextBox ID="txtLName" runat="server" TextMode="SingleLine" Text='<%# BindItem.LastName %>' />
- </td>
- <td>Salary:
- <asp:TextBox ID="txtSalary" runat="server" TextMode="SingleLine" Text='<%# BindItem.Salary %>' />
- </td>
- <td>ContactNo:
- <asp:TextBox ID="txtContactNo" runat="server" TextMode="SingleLine" Text='<%# BindItem.ContactNo %>' />
- </td>
- <td>Date of Birth:
- <asp:TextBox ID="txtDOB" runat="server" TextMode="SingleLine" Text='<%# BindItem.Date_Of_Birth %>' />
- </td>
- <td>
- <asp:Button BackColor="#ff0066" CommandName="Update" Text="Update" CausesValidation="false" runat="server" />
- <asp:Button BackColor="#ff0066" CommandName="Cancel" Text="Cancel" CausesValidation="false" runat="server" />
- </td>
- </tr>
- </EditItemTemplate>
- </asp:ListView>
- <hr style="color:darkblue" />
- <div style="clear: both;">
- <asp:DataPager ID="DataPager1" PagedControlID="EmployeeList" PageSize="2" runat="server">
- <Fields>
- <asp:NextPreviousPagerField ButtonType="Button" ShowFirstPageButton="true" ShowPreviousPageButton="true" ShowNextPageButton="false" ShowLastPageButton="false" />
- <asp:NumericPagerField ButtonType="Link" />
- <asp:NextPreviousPagerField ButtonType="Button" ShowLastPageButton="true" ShowNextPageButton="true" ShowFirstPageButton="false" ShowPreviousPageButton="false" />
- </Fields>
- </asp:DataPager>
- </div>
- </div>
- <hr style="width:5px;color:darkblue;" />
- <asp:Label ID="LabelStatus" BackColor="Blue" Width="540px" Height="5px" runat="server" Text=""></asp:Label>
- <h3>Add Employee:</h3>
- <table>
- <tr>
- <td>
- <asp:Label ID="LabelAddId" runat="server">ID:</asp:Label>
- </td>
- <td>
- <asp:TextBox ID="AddId" runat="server"></asp:TextBox>
- <asp:RequiredFieldValidator ID="RequiredFieldValidator6" runat="server" Text="* Id required." ControlToValidate="AddId" SetFocusOnError="true" Display="Dynamic"></asp:RequiredFieldValidator>
- </td>
- </tr>
- <tr>
- <td>
- <asp:Label ID="LabelAddFName" runat="server">FirstName:</asp:Label>
- </td>
- <td>
- <asp:TextBox ID="AddFirstName" runat="server"></asp:TextBox>
- <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" Text="* First name required." ControlToValidate="AddFirstName" SetFocusOnError="true" Display="Dynamic"></asp:RequiredFieldValidator>
- </td>
- </tr>
- <tr>
- <td>
- <asp:Label ID="LabelAddLName" runat="server">LastName:</asp:Label>
- </td>
- <td>
- <asp:TextBox ID="AddLastName" runat="server"></asp:TextBox>
- <asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server" Text="* Last name required." ControlToValidate="AddLastName" SetFocusOnError="true" Display="Dynamic"></asp:RequiredFieldValidator>
- </td>
- </tr>
- <tr>
- <td>
- <asp:Label ID="LabelAddSalary" runat="server">Salary:</asp:Label>
- </td>
- <td>
- <asp:TextBox ID="AddSalary" runat="server"></asp:TextBox>
- <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" Text="* Salary required." ControlToValidate="AddSalary" SetFocusOnError="true" Display="Dynamic"></asp:RequiredFieldValidator>
- </td>
- </tr>
- <tr>
- <td>
- <asp:Label ID="LabelAddContactNo" runat="server">ContactNo:</asp:Label>
- </td>
- <td>
- <asp:TextBox ID="AddContactNo" runat="server"></asp:TextBox>
- <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" Text="* ContactNo required." ControlToValidate="AddContactNo" SetFocusOnError="true" Display="Dynamic"></asp:RequiredFieldValidator>
- </td>
- </tr>
- <tr>
- <td>
- <asp:Label ID="LabelAddDOB" runat="server">Date Of Birth:</asp:Label>
- </td>
- <td>
- <asp:TextBox ID="AddDOB" runat="server"></asp:TextBox>
- <asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" Text="* Date of Birth required." ControlToValidate="AddDOB" SetFocusOnError="true" Display="Dynamic"></asp:RequiredFieldValidator>
- </td>
- </tr>
- </table>
- <p></p>
- <p></p>
- <asp:Button ID="AddEmployeeButton" runat="server" Text="Add Employee" OnClick="AddEmployeeButton_Click" CausesValidation="true" />
- <p></p>
- </asp:Content>







First LastPosted Jan 24, 2019, 8:30 AM
When the edit button is clicked, how is that associated with the Edit template to make it appear?