[]
The FormView control displays a single record of data. It provides a built-in mechanism for navigating from record to record; as well, it supports the updating, insertion, and deletion of a record. As a result, the FormView control provides a way to create a Web-based data-entry form somewhat analogous to the forms in a program like Microsoft Access or Oracle Forms.
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="FormView.aspx.cs" Inherits="FormView" %>
<!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 runat="server">
<title>Untitled Page</title>
<style type="text/css">
.dataValues{font-size:20px}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:FormView ID="FormView1" runat="server" AllowPaging="true" OnPageIndexChanging="FormView1_PageIndexChanging"
OnItemUpdating="FormView1_ItemUpdating" OnModeChanging="FormView1_ModeChanging"
DataKeyNames="ISBN" OnItemInserting="FormView1_ItemInserting" OnItemDeleting="FormView1_ItemDeleting">
<HeaderTemplate>
<h2>
Book Details</h2>
</HeaderTemplate>
<ItemTemplate>
<asp:Button ID="btnInsert" runat="Server" Text="Edit" CommandName="Edit" />
<asp:Button ID="Button1" runat="Server" Text="New" CommandName="New" />
<asp:Button ID="Button2" runat="Server" Text="Delete" CommandName="Delete" />
<div>
ISBN:
<br />
<asp:Label ID="labIsbn" runat="server" Text='<%# Eval("ISBN") %>' CssClass="dataValues" />
<br />
Title:<br />
<asp:Label ID="labTitle" runat="server" Text='<%# Eval("Title") %>' CssClass="dataValues" />
<br />
Publisher:<br />
<asp:Label ID="labPublisher" runat="server" Text='<%# Eval("Publisher") %>' CssClass="dataValues" />
<br />
Year:<br />
<asp:Label ID="labDesc" runat="server" Text='<%# Eval("Year") %>' CssClass="dataValues" />
<br />
</div>
</ItemTemplate>
<EditItemTemplate>
<div class="singleBook">
ISBN:
<br />
<asp:Label ID="labEditIsbn" runat="server" CssClass="dataValues" Text='<%# Eval("ISBN") %>' /><br />
Title:<br />
<asp:TextBox ID="txtTitle" runat="server" CssClass="dataValues" Text='<%# Bind("Title") %>'
Columns="75" Width="358px" /><br />
Publisher:<br />
<asp:TextBox ID="txtPub" runat="server" CssClass="dataValues" Text='<%# Bind("Publisher") %>'
Columns="4" Width="363px" /><br />
Year:<br />
<asp:TextBox ID="txtYear" runat="server" CssClass="dataValues" Text='<%# Bind("Year") %>'
Columns="4" Width="362px" /><br />
<asp:RequiredFieldValidator ID="reqTitle" runat="server" ControlToValidate="txtTitle"
ErrorMessage="Title can not be blank" />
<div class="actionBar">
<asp:LinkButton ID="lbtnUpdate" runat="server" Text="Update" CommandName="Update" />
<asp:LinkButton ID="lbtnCancel" CausesValidation="false" runat="server" Text="Cancel"
CommandName="Cancel" />
</div>
</div>
</EditItemTemplate>
<InsertItemTemplate>
<div class="singleBook">
ISBN:
<br />
<asp:Label ID="labEditIsbn" runat="server" CssClass="dataValues" Text='<%# Eval("ISBN") %>' /><br />
Title:<br />
<asp:TextBox ID="txtTitle" runat="server" CssClass="dataValues" Text='<%# Bind("Title") %>'
Columns="75" Width="358px" /><br />
Publisher:<br />
<asp:TextBox ID="txtPub" runat="server" CssClass="dataValues" Text='<%# Bind("Publisher") %>'
Columns="4" Width="363px" /><br />
Year:<br />
<asp:TextBox ID="txtYear" runat="server" CssClass="dataValues" Text='<%# Bind("Year") %>'
Columns="4" Width="362px" /><br />
<asp:RequiredFieldValidator ID="reqTitle" runat="server" ControlToValidate="txtTitle"
ErrorMessage="Title can not be blank" />
<div class="actionBar">
<asp:LinkButton ID="lbtnUpdate" runat="server" Text="Insert" CommandName="Insert" />
<asp:LinkButton ID="lbtnCancel" CausesValidation="false" runat="server" Text="Cancel"
CommandName="Cancel" />
</div>
</div>
</InsertItemTemplate>
</asp:FormView>
</div>
</form>
</body>
</html>
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class FormView : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindFormView();
}
}
private void BindFormView()
{
if (Session["strTemp"] != null)
{
FormView1.DataSource = Session["strTemp"] as DataTable;
FormView1.DataBind();
}
else
{
FormView1.DataSource = GetCustomMadeDataTable();
FormView1.DataBind();
}
}
public DataTable GetCustomMadeDataTable()
{
//Create a new DataTable object
System.Data.DataTable objDataTable = new System.Data.DataTable();
//Create three columns with string as their type
objDataTable.Columns.Add("ISBN", typeof(string));
objDataTable.Columns.Add("Title", typeof(string));
objDataTable.Columns.Add("Publisher", typeof(string));
objDataTable.Columns.Add("Year", typeof(string));
DataColumn[] dcPk = new DataColumn[1];
dcPk[0] = objDataTable.Columns["ISBN"];
objDataTable.PrimaryKey = dcPk;
objDataTable.Columns["ISBN"].AutoIncrement = true;
objDataTable.Columns["ISBN"].AutoIncrementSeed = 1;
//Adding some data in the rows of this DataTable
DataRow dr;
for (int i = 1; i <= 5; i++)
{
dr = objDataTable.NewRow();
dr[1] = "Title" + i.ToString();
dr[2] = "Publisher" + i.ToString();
dr[3] = "200" + i.ToString();
objDataTable.Rows.Add(dr);
}
Session["strTemp"] = objDataTable;
return objDataTable;
}
protected void FormView1_PageIndexChanging(object sender, FormViewPageEventArgs e)
{
FormView1.PageIndex = e.NewPageIndex;
BindFormView();
}
protected void FormView1_ModeChanging(object sender, FormViewModeEventArgs e)
{
if (e.NewMode.ToString() == "Edit")
{
FormView1.ChangeMode(e.NewMode);
}
else if (e.NewMode.ToString() == "Insert")
{
FormView1.ChangeMode(e.NewMode);
}
else
FormView1.ChangeMode(e.NewMode);
BindFormView();
}
protected void FormView1_ItemUpdating(object sender, FormViewUpdateEventArgs e)
{
string ISBN = FormView1.DataKey.Value.ToString();
DataTable dt = Session["strTemp"] as DataTable;
DataRow dr;
dr = dt.NewRow();
dr = dt.Rows.Find(ISBN);
dr[1] = ((TextBox)FormView1.FindControl("txtTitle")).Text;
dr[2] = ((TextBox)FormView1.FindControl("txtPub")).Text;
dr[3] = ((TextBox)FormView1.FindControl("txtYear")).Text;
dt.GetChanges();
Session["strTemp"] = dt;
FormView1.ChangeMode(FormViewMode.ReadOnly);
BindFormView();
}
protected void FormView1_ItemInserting(object sender, FormViewInsertEventArgs e)
{
DataTable dt = Session["strTemp"] as DataTable;
DataRow dr;
dr = dt.NewRow();
dr[1] = ((TextBox)FormView1.FindControl("txtTitle")).Text;
dr[2] = ((TextBox)FormView1.FindControl("txtPub")).Text;
dr[3] = ((TextBox)FormView1.FindControl("txtYear")).Text;
dt.Rows.Add(dr);
Session["strTemp"] = dt;
FormView1.ChangeMode(FormViewMode.ReadOnly);
BindFormView();
}
protected void FormView1_ItemDeleting(object sender, FormViewDeleteEventArgs e)
{
string ISBN = FormView1.DataKey.Value.ToString();
DataTable dt = Session["strTemp"] as DataTable;
DataRow dr;
dr = dt.NewRow();
dt.Rows.Find(ISBN).Delete();
dt.GetChanges();
Session["strTemp"] = dt;
FormView1.ChangeMode(FormViewMode.ReadOnly);
BindFormView();
}
}