This example integrates Entity Framework instead of DataSource wizard controls which
is covered on most tutorials available for this control.
To start with, execute the T-sql script and create an asp.net project. In your project add a WebForm called FormViewDemo and an ADO.NET Entity Data Model BooksEntities which points to the created table called BookDetails. Then use the codes below to complete this application.
SQL Code:
sql Code:
CREATE TABLE [dbo].[BookDetails]( [BookSerialNo] [int] IDENTITY(1,1) NOT NULL, [BookISBN] [nchar](15) NULL, [BookTitle] [varchar](120) NULL, [BookAuthor] [varchar](60) NULL, [BookPublisher] [varchar](50) NULL, [BookCategory] [varchar](20) NULL, PRIMARY KEY CLUSTERED ( [BookSerialNo] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO
ASPX Markup (Add inside the <form> element)
html Code:
<asp:FormView ID="FormViewBookDetails" runat="server" AllowPaging="True" DataKeyNames="BookSerialNo" GridLines="Both" ClientIDMode="Static" OnItemCommand="FormViewBookDetails_ItemCommand" OnPageIndexChanging="FormViewBookDetails_PageIndexChanging" OnModeChanging="FormViewBookDetails_ModeChanging" OnItemInserting="FormViewBookDetails_ItemInserting" OnItemDeleting="FormViewBookDetails_ItemDeleting" PagerSettings-Mode="NumericFirstLast" OnItemUpdating="FormViewBookDetails_ItemUpdating"> <headertemplate> <table class="" id="tblHeader"> <tr> <td colspan="2"> <div id="Header"> <asp:label id="lbl" Text="GHK Bookshop FormView Create-Update-Delete Demo" runat="server"/> </div> </td> </tr> </table> </headertemplate> <EditItemTemplate> <table id="tblEdit"> <tr> <td >Serial # : </td> <td> <asp:Label ID="lblBookSerialNo" runat="server" Text='<%# Eval("BookSerialNo") %>' /> </td> </tr> <tr> <td >ISBN : </td> <td> <asp:TextBox ID="txtBookISBN" runat="server" required="required" Text='<%# Bind("BookISBN") %>'></asp:TextBox> </td> </tr> <tr> <td >Title : </td> <td> <asp:TextBox ID="txtBookTitle" runat="server" required="required" Text='<%# Bind("BookTitle") %>'></asp:TextBox> </td> </tr> <tr> <td >Author : </td> <td> <asp:TextBox ID="txtBookAuthor" runat="server" required="required" Text='<%# Bind("BookAuthor") %>'></asp:TextBox> </td> </tr> <tr> <td >Publisher : </td> <td> <asp:TextBox ID="txtBookPublisher" runat="server" required="required" Text='<%# Bind("BookPublisher") %>'></asp:TextBox> </td> </tr> <tr> <td >Category : </td> <td> <asp:TextBox ID="txtBookCategory" runat="server" required="required" Text='<%# Bind("BookCategory") %>'></asp:TextBox> </td> </tr> <tr> <td colspan="2"> <asp:Button ID="btnUpdate" runat="server" CommandName="Update" Text="Update" /> <asp:Button ID="btnCancel" runat="server" CommandName="Cancel" Text="Cancel" UseSubmitBehavior="false" CausesValidation="false"/> </td> </tr> </table> </EditItemTemplate> <ItemTemplate> <table id="tblItem"> <tr> <td>Serial # : </td> <td> <asp:Label ID="EmployeeIDLabel" runat="server" Text='<%# Eval("BookSerialNo") %>' /> </td> </tr> <tr> <td >ISBN : </td> <td> <asp:Label ID="LastNameLabel" runat="server" Text='<%# Bind("BookISBN") %>' /> </td> </tr> <tr> <td >Title : </td> <td> <asp:Label ID="FirstNameLabel" runat="server" Text='<%# Bind("BookTitle") %>' /> </td> </tr> <tr> <td >Author : </td> <td> <asp:Label ID="CountryLabel" runat="server" Text='<%# Bind("BookAuthor") %>' /> </td> </tr> <tr> <td>Publisher :</td> <td> <asp:Label ID="NotesLabel" runat="server" Text='<%# Bind("BookPublisher") %>' /> </td> </tr> <tr> <td>Category :</td> <td> <asp:Label ID="Label1" runat="server" Text='<%# Bind("BookCategory") %>' /> </td> </tr> <tr> <td colspan="2"> <asp:Button ID="btnInsert" runat="server" Text="Add" CommandName="New" /> <asp:Button ID="btnEdit" runat="server" Text="Edit" CommandName="Edit" UseSubmitBehavior="false"/> <asp:Button ID="btnDelete" runat="server" Text="Delete" CommandName="Delete" /> </td> </tr> </table> </ItemTemplate> <InsertItemTemplate> <table id="tblInsert"> <tr> <td >ISBN : </td> <td> <asp:TextBox ID="txtInsertBookISBN" required="required" runat="server" Text='<%# Bind("BookISBN") %>'></asp:TextBox> </td> </tr> <tr> <td >Title : </td> <td> <asp:TextBox ID="txtInsertBookTitle" required="required" runat="server" Text='<%# Bind("BookTitle") %>'></asp:TextBox> </td> </tr> <tr> <td >Author : </td> <td> <asp:TextBox ID="txtInsertBookAuthor" required="required" runat="server" Text='<%# Bind("BookAuthor") %>'></asp:TextBox> </td> </tr> <tr> <td >Publisher : </td> <td> <asp:TextBox ID="txtInsertBookPublisher" required="required" runat="server" Text='<%# Bind("BookPublisher") %>'></asp:TextBox> </td> </tr> <tr> <td >Category : </td> <td> <asp:TextBox ID="txtInsertBookCategory" required="required" runat="server" Text='<%# Bind("BookCategory") %>'></asp:TextBox> </td> </tr> <tr> <td colspan="2"> <asp:Button ID="btnSave" runat="server" CommandName="Insert" Text="Save"/> <asp:Button ID="btnCancelInsert" runat="server" CommandName="Cancel" Text="Cancel" UseSubmitBehavior="false" CausesValidation="false" /> </td> </tr> </table> </InsertItemTemplate> </asp:FormView>
Code Behind
vb.net Code:
Public Class FormViewDemo Inherits System.Web.UI.Page Dim _context As BooksEntities Public Sub New() _context = New BooksEntities() End Sub Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load If Not Page.IsPostBack Then BindFormView() End If End Sub Private Sub BindFormView() Dim bookRecords = _context.BookDetails.ToList() FormViewBookDetails.DataSource = bookRecords FormViewBookDetails.DataBind() End Sub Protected Sub FormViewBookDetails_ItemUpdating(sender As Object, e As FormViewUpdateEventArgs) Dim id As Integer = Int32.Parse(FormViewBookDetails.DataKey(0).ToString()) Dim result = _context.BookDetails.Find(id) If result IsNot Nothing Then Dim item = New BookDetail() item.BookSerialNo = id item.BookISBN = DirectCast(FormViewBookDetails.FindControl("txtBookISBN"), TextBox).Text item.BookTitle = DirectCast(FormViewBookDetails.FindControl("txtBookTitle"), TextBox).Text item.BookAuthor = DirectCast(FormViewBookDetails.FindControl("txtBookAuthor"), TextBox).Text item.BookPublisher = DirectCast(FormViewBookDetails.FindControl("txtBookPublisher"), TextBox).Text item.BookCategory = DirectCast(FormViewBookDetails.FindControl("txtBookCategory"), TextBox).Text _context.Entry(result).CurrentValues.SetValues(item) _context.SaveChanges() ResetBinding() End If End Sub Protected Sub FormViewBookDetails_PageIndexChanging(sender As Object, e As FormViewPageEventArgs) FormViewBookDetails.PageIndex = e.NewPageIndex BindFormView() End Sub Protected Sub FormViewBookDetails_ItemCommand(sender As Object, e As FormViewCommandEventArgs) If e.CommandName = "Cancel" Then FormViewBookDetails.ChangeMode(FormViewMode.ReadOnly) ElseIf e.CommandName = "Edit" Then FormViewBookDetails.ChangeMode(FormViewMode.Edit) ElseIf e.CommandName = "New" Then FormViewBookDetails.ChangeMode(FormViewMode.Insert) ElseIf e.CommandName = "Delete" Then FormViewBookDetails.ChangeMode(FormViewMode.ReadOnly) BindFormView() End If End Sub Protected Sub FormViewBookDetails_ModeChanging(sender As Object, e As FormViewModeEventArgs) If e.NewMode = FormViewMode.Insert Then FormViewBookDetails.AllowPaging = False ElseIf e.NewMode = FormViewMode.Edit Then FormViewBookDetails.AllowPaging = False BindFormView() Else FormViewBookDetails.AllowPaging = True BindFormView() End If End Sub Protected Sub FormViewBookDetails_ItemInserting(sender As Object, e As FormViewInsertEventArgs) Dim item = New BookDetail() item.BookISBN = DirectCast(FormViewBookDetails.FindControl("txtInsertBookISBN"), TextBox).Text item.BookTitle = DirectCast(FormViewBookDetails.FindControl("txtInsertBookTitle"), TextBox).Text item.BookAuthor = DirectCast(FormViewBookDetails.FindControl("txtInsertBookAuthor"), TextBox).Text item.BookPublisher = DirectCast(FormViewBookDetails.FindControl("txtInsertBookPublisher"), TextBox).Text item.BookCategory = DirectCast(FormViewBookDetails.FindControl("txtInsertBookCategory"), TextBox).Text _context.BookDetails.Add(item) _context.SaveChanges() ResetBinding() End Sub Protected Sub FormViewBookDetails_ItemDeleting(sender As Object, e As FormViewDeleteEventArgs) If FormViewBookDetails.DataKey(0) IsNot Nothing Then Dim id = Int32.Parse(FormViewBookDetails.DataKey(0).ToString()) Dim itemDelete = _context.BookDetails.FirstOrDefault(Function(t) t.BookSerialNo = id) If itemDelete IsNot Nothing Then _context.Entry(itemDelete).State = EntityState.Deleted _context.SaveChanges() ResetBinding() End If End If End Sub Private Sub ResetBinding() FormViewBookDetails.ChangeMode(FormViewMode.ReadOnly) FormViewBookDetails.AllowPaging = True BindFormView() End Sub End Class