SlideShare a Scribd company logo
form view
form view
The working of Form View control is same as
Detail View control but the default UI of Form
View control is different.
Detail View control displays the records in tabular
format. Form View control does not have
predefined layout but it depend upon template.
According to the need of application, we can
display the data with the help of template.
 We can insert, update, delete and paging the
record in Form View.
 Adding validation controls to a Form View is
easier than Detail View control. For displaying
data, Item Template is used.
Displaying Data with the Form View
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web.UI.WebControls;
using System.Web.UI;
public partial class FormViewDemo :
System.Web.UI.Page
{
SqlConnection conn;
SqlDataAdapter adapter;
DataSet ds;
SqlCommand cmd;
string cs =
ConfigurationManager.ConnectionStrings["conStri
ng"].ConnectionString;
protected void Page_Load(object sender,
EventArgs e)
{
if (!IsPostBack)
{
PopulateFormView();
}
}
protected void PopulateFormView()
{
try
{
conn = new SqlConnection(cs);
adapter = new SqlDataAdapter("select *
from tblEmps", conn);
ds = new DataSet();
adapter.Fill(ds);
FormView1.DataSource = ds;
FormView1.DataBind();
}
 catch (Exception ex)
{
Label1.Text = "ERROR :: " + ex.Message;
}
}
protected void
FormView1_ModeChanging(object sender,
FormViewModeEventArgs e)
{
FormView1.ChangeMode(e.NewMode);
PopulateFormView();
}
}

The ItemTemplate supports a databinding
expression that is used to bind the database
table column. The Eval() or Bind() method is used
to retrieves the values of these columns.
<ItemTemplate>
<table border="1">
<tr>
<th><b>EmpID:</b></th>
<td ><%# Eval("EmpID") %></td></tr>
<tr>
<td><b>Name:</b></td>
<td ><%# Eval("Name") %></td> </tr>
<tr>
<td><b>Gender:</b></td>
<td ><%#Eval("Gender") %></td></tr>
<tr>
<td><b>Salary:</b></td>
<td><%# Eval("Salary") %></td> </tr>
<tr>
<td><b>Address:</b></td>
<td><%# Eval("Address") %></td>
</tr>
<tr>
<td><b>DepID:</b></td>
<td><%#Eval("DepID") %>
</tr>
</table>
<asp:LinkButton ID="lnkEdit" Text="Edit
" CommandName="Edit" runat="server" />
<asp:LinkButton ID="lnkNew" Text="Ne
w" CommandName="New" runat="server" />
<asp:LinkButton ID="lnkDelete"
Text="Delete" CommandName="Delete"
runat="server" />
</ItemTemplate>
 We can perform paging with Form View
Control by using its AllowPaging property.
 By default Allow Paging property is false. Set
the value of Allow Paging property as true for
performing the paging.
 It will generate the paging link automatically.
Write code in Page Index Changing event as
follows.
protected void
FormView1_PageIndexChanging(object sender,
FormViewPageEventArgs e)
{
FormView1.PageIndex = e.NewPageIndex;
PopulateFormView();
}
OUTPUT:
 The Form View control uses templates to edit
records.
 It enables us to use Edit ItemTemplate and
specify the user interface.
 If we want simply display the record then use
Eval() method otherwise use Bind() method.
 For editing the record you must have editable
control, therefore we have used Text Box control
within Edit Item Template and bind the Text
property of Text Box
 If we click on Edit link, then Update and Cancel
link will display in place of Edit Link.
 By default these links are not available in Form
View control.
 We must provide these links in template fields
explicitly.
 Form View control has a Data Key Names
property that is used to hold the name of the
primary key from the database table.
 We have to specify a primary key explicitly when
editing records.
 This Link Button supports a Command Name
property which has the value Edit.
 When we click on Edit link the Form View mode is
changed from Read Only mode to "Edit" mode.
 We can use Button or Image Button control in
place of Link Button.
 The Bind("EmpID") method binds the Employee ID
column database table to the Text property of
the Text Box control.
<EditItemTemplate>
<table border="1">
<tr>
<th><b>EmpID:</b></th>
<td><asp:TextBox ID="txtEmpID"
Text='<%# Bind("EmpID") %>' runat="Server"
/></td>
</tr>
<tr>
<td><b>Name:</b></td>
<td><asp:TextBox ID="txtName"
Text='<%# Bind("Name") %>' runat="Server" /></td>
</tr>
<tr>
<td><b>Gender:</b></td>
<td><asp:TextBox ID="txtGender"
Text='<%# Bind("Gender") %>' runat="Server"/>
</td>
</tr>
<tr>
 <td><b>Salary:</b></td>
<td><asp:TextBox ID="txtSalary"
Text='<%# Bind("Salary") %>' runat="Server"
/></td>
</tr>
<tr>
<td><b>Address:</b></td>
<td><asp:TextBox
ID="txtAddress" Text='<%# Bind("Address") %>'
runat="Server" /></td>
</tr>
<tr>
<td><b>DepID:</b></td>
<td><asp:TextBox
ID="txtDeptID" Text='<%# Bind("DepID") %>'
runat="Server" /></td>
</tr>
</table>
<asp:LinkButton
ID="lnkUpdate" Text="Update"
CommandName="Update" runat="server" />
<asp:LinkButton
ID="lnkCancel" Text="Cancel"
CommandName="Cancel" runat="server" />
</EditItemTemplate>
 When we click on Update link ItemUpdating event
fires.
 We have already added TextBox in
EditItemTemplate. Access these textboxes with
the help of FindControl method. Write code for
updating the record as given below.
protected void FormView1_ItemUpdating(object sender,
FormViewUpdateEventArgs e)
{
int ID =
Convert.ToInt32((FormView1.FindControl("txtEmpID") as
TextBox).Text);
string empName = (FormView1.FindControl("txtName") as
TextBox).Text;
string empGender = (FormView1.FindControl("txtGender")
as TextBox).Text;
double empSalary =
Convert.ToDouble((FormView1.FindControl("txtSalary") as
TextBox).Text);
string empAddress = (FormView1.FindControl("txtAddress")
as TextBox).Text;
int depID =
Convert.ToInt32((FormView1.FindControl("txtDeptID") as
TextBox).Text);
string updateQuery = "update tblEmps set
name='"+empName+"',
gender='"+empGender+"',salary="+empSalary+",address='
"+empAddress+"',
depid="+depID+" where empid="+ID;
conn = new SqlConnection(cs);
cmd = new SqlCommand(updateQuery, conn);
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
FormView1.ChangeMode(FormViewMode.Re
adOnly);
PopulateFormView();
}
OUTPUT:
 We can insert new record into the database with
the help of Form View Control.
 Form View control supports Insert Item Template
that is used to insert new record in database.
 The Item Template contains the "New" Link
Button. When you click on "New" Link Button,
control goes from Read Only mode to Insert
mode. Insert and Cancel Link Button is appeared
in place of "New" Link Button.
<InsertItemTemplate>
<table border="1">
<tr>
<th><b>EmpID:</b></th>
<td><asp:TextBox ID="txtEmpID"
Text='<%# Bind("EmpID") %>' runat="Server" /></td>
</tr>
<tr>
<td><b>Name:</b></td>
<td><asp:TextBox ID="txtName"
Text='<%# Bind("Name") %>' runat="Server" /></td>
</tr>
<tr>
<td><b>Gender:</b></td>
<td><asp:TextBox ID="txtGender"
Text='<%# Bind("Gender") %>' runat="Server" /> </td>
</tr>
<tr>
<td><b>Salary:</b></td>
<td><asp:TextBox ID="txtSalary"
Text='<%# Bind("Salary") %>' runat="Server"/>
</td>
</tr>
<tr>
<td><b>Address:</b></td>
<td><asp:TextBox
ID="txtAddress" Text='<%# Bind("Address") %>'
runat="Server" /> </td>
</tr>
<tr>
<td><b>DepID:</b></td>
<td><asp:TextBox
ID="txtDeptID" Text='<%# Bind("DepID") %>'
runat="Server" /></td>
</tr>
</table>
<asp:LinkButton ID="lnkInsert"
Text="Insert" CommandName="Insert" r
<asp:LinkButton ID="lnkCancel" Text="Cancel"
CommandName="Cancel" runat="server" />
</InsertItemTemplate>
When you click on Insert Link Button, ItemInserting event is fires.
protected void FormView1_ItemInserting(object sender,
FormViewInsertEventArgs e)
{
string ID = (FormView1.FindControl("txtEmpID") as
TextBox).Text;
string empName = (FormView1.FindControl("txtName") as
TextBox).Text;
string empGender = (FormView1.FindControl("txtGender") as
TextBox).Text;
double empSalary =
Convert.ToDouble((FormView1.FindControl("txtSalary") as
TextBox).Text);
string empAddress = (FormView1.FindControl("txtAddress") as
TextBox).Text;
string depID = (FormView1.FindControl("txtDeptID") as
TextBox).Text;
string insertQuery = "insert into tblEmps
values("+ID+",'"+empName+"','"+empGender+"',
"+empSalary+",'"+empAddress+"',"+depID+")";
conn = new SqlConnection(cs);
cmd = new SqlCommand(insertQuery, conn);
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
FormView1.ChangeMode(FormViewMode.Re
adOnly);
PopulateFormView();
}
OUTPUT:
form view

More Related Content

PDF
Java Programming :Event Handling(Types of Events)
PPTX
Event handling
PPT
Server Controls of ASP.Net
PDF
Asp.net state management
PPT
6. Integrity and Security in DBMS
Java Programming :Event Handling(Types of Events)
Event handling
Server Controls of ASP.Net
Asp.net state management
6. Integrity and Security in DBMS

What's hot (20)

PPTX
Validation Controls in asp.net
PPTX
Delegates and events in C#
PPTX
SWING USING JAVA WITH VARIOUS COMPONENTS
PPTX
Event Handling in JAVA
PPT
Web controls
PPTX
Estimating Software Maintenance Costs
PPTX
Java Server Pages
PPTX
Software architectural patterns - A Quick Understanding Guide
PPT
14. Query Optimization in DBMS
DOCX
Data flow oriented modeling
PPT
ADO .Net
PPT
UML Diagrams
PPTX
Real time and distributed design
PPTX
source code metrics and other maintenance tools and techniques
PPT
Capability Maturity Model (CMM) in Software Engineering
PPTX
Software Cost Estimation Techniques
PPTX
Asp.NET Validation controls
PPTX
Introduction to php
PPTX
C# Arrays
Validation Controls in asp.net
Delegates and events in C#
SWING USING JAVA WITH VARIOUS COMPONENTS
Event Handling in JAVA
Web controls
Estimating Software Maintenance Costs
Java Server Pages
Software architectural patterns - A Quick Understanding Guide
14. Query Optimization in DBMS
Data flow oriented modeling
ADO .Net
UML Diagrams
Real time and distributed design
source code metrics and other maintenance tools and techniques
Capability Maturity Model (CMM) in Software Engineering
Software Cost Estimation Techniques
Asp.NET Validation controls
Introduction to php
C# Arrays
Ad

Similar to form view (20)

PPTX
Detail view in distributed technologies
DOC
Cis407 a ilab 4 web application development devry university
DOCX
This is part 1 of 3STEP 1 Modify the clsDataLayer to Use a Two-St.docx
DOCX
Cis 407 i lab 5 of 7
DOCX
Cis 407 i lab 4 of 7
PDF
The entity framework 4.0 and asp.net web forms getting started
DOC
Cis407 a ilab 5 web application development devry university
DOCX
Cis 407 i lab 2 of 7
PPTX
Asp PPT (.NET )
PPTX
Application of Insert and select notes.ppt x
DOCX
Grid view control
PDF
The entity framework 4 and asp net web forms
PDF
Windows Forms For Beginners Part - 3
PPTX
Microsoft dynamics ax2012 : forms and tables methods call sequences, How To?
PPTX
Microsoft asp.net online training
PPTX
Microsoft asp.net online training
DOC
Cis407 a ilab 2 web application development devry university
DOCX
A Skills Approach Access 2013 Chapter 3 Working with Forms a.docx
PPTX
Basic Net Online Training in Hyderabad
PDF
Entity frame work by Salman Mushtaq -1-
Detail view in distributed technologies
Cis407 a ilab 4 web application development devry university
This is part 1 of 3STEP 1 Modify the clsDataLayer to Use a Two-St.docx
Cis 407 i lab 5 of 7
Cis 407 i lab 4 of 7
The entity framework 4.0 and asp.net web forms getting started
Cis407 a ilab 5 web application development devry university
Cis 407 i lab 2 of 7
Asp PPT (.NET )
Application of Insert and select notes.ppt x
Grid view control
The entity framework 4 and asp net web forms
Windows Forms For Beginners Part - 3
Microsoft dynamics ax2012 : forms and tables methods call sequences, How To?
Microsoft asp.net online training
Microsoft asp.net online training
Cis407 a ilab 2 web application development devry university
A Skills Approach Access 2013 Chapter 3 Working with Forms a.docx
Basic Net Online Training in Hyderabad
Entity frame work by Salman Mushtaq -1-
Ad

Recently uploaded (20)

PPTX
A powerpoint presentation on the Revised K-10 Science Shaping Paper
PPTX
ELIAS-SEZIURE AND EPilepsy semmioan session.pptx
PDF
Τίμαιος είναι φιλοσοφικός διάλογος του Πλάτωνα
PDF
Practical Manual AGRO-233 Principles and Practices of Natural Farming
PDF
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
PPTX
Introduction to pro and eukaryotes and differences.pptx
PDF
What if we spent less time fighting change, and more time building what’s rig...
PDF
David L Page_DCI Research Study Journey_how Methodology can inform one's prac...
PDF
احياء السادس العلمي - الفصل الثالث (التكاثر) منهج متميزين/كلية بغداد/موهوبين
PDF
Weekly quiz Compilation Jan -July 25.pdf
PPTX
202450812 BayCHI UCSC-SV 20250812 v17.pptx
PDF
Indian roads congress 037 - 2012 Flexible pavement
PDF
Empowerment Technology for Senior High School Guide
PDF
AI-driven educational solutions for real-life interventions in the Philippine...
PDF
BP 704 T. NOVEL DRUG DELIVERY SYSTEMS (UNIT 1)
PDF
ChatGPT for Dummies - Pam Baker Ccesa007.pdf
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PPTX
Virtual and Augmented Reality in Current Scenario
PDF
CISA (Certified Information Systems Auditor) Domain-Wise Summary.pdf
PPTX
Computer Architecture Input Output Memory.pptx
A powerpoint presentation on the Revised K-10 Science Shaping Paper
ELIAS-SEZIURE AND EPilepsy semmioan session.pptx
Τίμαιος είναι φιλοσοφικός διάλογος του Πλάτωνα
Practical Manual AGRO-233 Principles and Practices of Natural Farming
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
Introduction to pro and eukaryotes and differences.pptx
What if we spent less time fighting change, and more time building what’s rig...
David L Page_DCI Research Study Journey_how Methodology can inform one's prac...
احياء السادس العلمي - الفصل الثالث (التكاثر) منهج متميزين/كلية بغداد/موهوبين
Weekly quiz Compilation Jan -July 25.pdf
202450812 BayCHI UCSC-SV 20250812 v17.pptx
Indian roads congress 037 - 2012 Flexible pavement
Empowerment Technology for Senior High School Guide
AI-driven educational solutions for real-life interventions in the Philippine...
BP 704 T. NOVEL DRUG DELIVERY SYSTEMS (UNIT 1)
ChatGPT for Dummies - Pam Baker Ccesa007.pdf
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
Virtual and Augmented Reality in Current Scenario
CISA (Certified Information Systems Auditor) Domain-Wise Summary.pdf
Computer Architecture Input Output Memory.pptx

form view

  • 3. The working of Form View control is same as Detail View control but the default UI of Form View control is different. Detail View control displays the records in tabular format. Form View control does not have predefined layout but it depend upon template. According to the need of application, we can display the data with the help of template.
  • 4.  We can insert, update, delete and paging the record in Form View.  Adding validation controls to a Form View is easier than Detail View control. For displaying data, Item Template is used. Displaying Data with the Form View
  • 5. using System; using System.Data; using System.Data.SqlClient; using System.Configuration; using System.Web.UI.WebControls; using System.Web.UI; public partial class FormViewDemo : System.Web.UI.Page { SqlConnection conn; SqlDataAdapter adapter; DataSet ds; SqlCommand cmd; string cs = ConfigurationManager.ConnectionStrings["conStri ng"].ConnectionString; protected void Page_Load(object sender, EventArgs e)
  • 6. { if (!IsPostBack) { PopulateFormView(); } } protected void PopulateFormView() { try { conn = new SqlConnection(cs); adapter = new SqlDataAdapter("select * from tblEmps", conn); ds = new DataSet(); adapter.Fill(ds); FormView1.DataSource = ds; FormView1.DataBind(); }
  • 7.  catch (Exception ex) { Label1.Text = "ERROR :: " + ex.Message; } } protected void FormView1_ModeChanging(object sender, FormViewModeEventArgs e) { FormView1.ChangeMode(e.NewMode); PopulateFormView(); } }  The ItemTemplate supports a databinding expression that is used to bind the database table column. The Eval() or Bind() method is used to retrieves the values of these columns.
  • 8. <ItemTemplate> <table border="1"> <tr> <th><b>EmpID:</b></th> <td ><%# Eval("EmpID") %></td></tr> <tr> <td><b>Name:</b></td> <td ><%# Eval("Name") %></td> </tr> <tr> <td><b>Gender:</b></td> <td ><%#Eval("Gender") %></td></tr> <tr> <td><b>Salary:</b></td> <td><%# Eval("Salary") %></td> </tr> <tr> <td><b>Address:</b></td>
  • 9. <td><%# Eval("Address") %></td> </tr> <tr> <td><b>DepID:</b></td> <td><%#Eval("DepID") %> </tr> </table> <asp:LinkButton ID="lnkEdit" Text="Edit " CommandName="Edit" runat="server" /> <asp:LinkButton ID="lnkNew" Text="Ne w" CommandName="New" runat="server" /> <asp:LinkButton ID="lnkDelete" Text="Delete" CommandName="Delete" runat="server" /> </ItemTemplate>
  • 10.  We can perform paging with Form View Control by using its AllowPaging property.  By default Allow Paging property is false. Set the value of Allow Paging property as true for performing the paging.  It will generate the paging link automatically. Write code in Page Index Changing event as follows.
  • 11. protected void FormView1_PageIndexChanging(object sender, FormViewPageEventArgs e) { FormView1.PageIndex = e.NewPageIndex; PopulateFormView(); } OUTPUT:
  • 12.  The Form View control uses templates to edit records.  It enables us to use Edit ItemTemplate and specify the user interface.  If we want simply display the record then use Eval() method otherwise use Bind() method.  For editing the record you must have editable control, therefore we have used Text Box control within Edit Item Template and bind the Text property of Text Box
  • 13.  If we click on Edit link, then Update and Cancel link will display in place of Edit Link.  By default these links are not available in Form View control.  We must provide these links in template fields explicitly.  Form View control has a Data Key Names property that is used to hold the name of the primary key from the database table.  We have to specify a primary key explicitly when editing records.
  • 14.  This Link Button supports a Command Name property which has the value Edit.  When we click on Edit link the Form View mode is changed from Read Only mode to "Edit" mode.  We can use Button or Image Button control in place of Link Button.  The Bind("EmpID") method binds the Employee ID column database table to the Text property of the Text Box control.
  • 15. <EditItemTemplate> <table border="1"> <tr> <th><b>EmpID:</b></th> <td><asp:TextBox ID="txtEmpID" Text='<%# Bind("EmpID") %>' runat="Server" /></td> </tr> <tr> <td><b>Name:</b></td> <td><asp:TextBox ID="txtName" Text='<%# Bind("Name") %>' runat="Server" /></td> </tr> <tr> <td><b>Gender:</b></td> <td><asp:TextBox ID="txtGender" Text='<%# Bind("Gender") %>' runat="Server"/> </td> </tr> <tr>
  • 16.  <td><b>Salary:</b></td> <td><asp:TextBox ID="txtSalary" Text='<%# Bind("Salary") %>' runat="Server" /></td> </tr> <tr> <td><b>Address:</b></td> <td><asp:TextBox ID="txtAddress" Text='<%# Bind("Address") %>' runat="Server" /></td> </tr> <tr> <td><b>DepID:</b></td> <td><asp:TextBox ID="txtDeptID" Text='<%# Bind("DepID") %>' runat="Server" /></td>
  • 17. </tr> </table> <asp:LinkButton ID="lnkUpdate" Text="Update" CommandName="Update" runat="server" /> <asp:LinkButton ID="lnkCancel" Text="Cancel" CommandName="Cancel" runat="server" /> </EditItemTemplate>  When we click on Update link ItemUpdating event fires.  We have already added TextBox in EditItemTemplate. Access these textboxes with the help of FindControl method. Write code for updating the record as given below.
  • 18. protected void FormView1_ItemUpdating(object sender, FormViewUpdateEventArgs e) { int ID = Convert.ToInt32((FormView1.FindControl("txtEmpID") as TextBox).Text); string empName = (FormView1.FindControl("txtName") as TextBox).Text; string empGender = (FormView1.FindControl("txtGender") as TextBox).Text; double empSalary = Convert.ToDouble((FormView1.FindControl("txtSalary") as TextBox).Text); string empAddress = (FormView1.FindControl("txtAddress") as TextBox).Text; int depID = Convert.ToInt32((FormView1.FindControl("txtDeptID") as TextBox).Text); string updateQuery = "update tblEmps set name='"+empName+"', gender='"+empGender+"',salary="+empSalary+",address=' "+empAddress+"', depid="+depID+" where empid="+ID; conn = new SqlConnection(cs); cmd = new SqlCommand(updateQuery, conn);
  • 20.  We can insert new record into the database with the help of Form View Control.  Form View control supports Insert Item Template that is used to insert new record in database.  The Item Template contains the "New" Link Button. When you click on "New" Link Button, control goes from Read Only mode to Insert mode. Insert and Cancel Link Button is appeared in place of "New" Link Button.
  • 21. <InsertItemTemplate> <table border="1"> <tr> <th><b>EmpID:</b></th> <td><asp:TextBox ID="txtEmpID" Text='<%# Bind("EmpID") %>' runat="Server" /></td> </tr> <tr> <td><b>Name:</b></td> <td><asp:TextBox ID="txtName" Text='<%# Bind("Name") %>' runat="Server" /></td> </tr> <tr> <td><b>Gender:</b></td> <td><asp:TextBox ID="txtGender" Text='<%# Bind("Gender") %>' runat="Server" /> </td> </tr> <tr>
  • 22. <td><b>Salary:</b></td> <td><asp:TextBox ID="txtSalary" Text='<%# Bind("Salary") %>' runat="Server"/> </td> </tr> <tr> <td><b>Address:</b></td> <td><asp:TextBox ID="txtAddress" Text='<%# Bind("Address") %>' runat="Server" /> </td> </tr> <tr> <td><b>DepID:</b></td> <td><asp:TextBox ID="txtDeptID" Text='<%# Bind("DepID") %>' runat="Server" /></td> </tr> </table> <asp:LinkButton ID="lnkInsert" Text="Insert" CommandName="Insert" r
  • 23. <asp:LinkButton ID="lnkCancel" Text="Cancel" CommandName="Cancel" runat="server" /> </InsertItemTemplate> When you click on Insert Link Button, ItemInserting event is fires. protected void FormView1_ItemInserting(object sender, FormViewInsertEventArgs e) { string ID = (FormView1.FindControl("txtEmpID") as TextBox).Text; string empName = (FormView1.FindControl("txtName") as TextBox).Text; string empGender = (FormView1.FindControl("txtGender") as TextBox).Text; double empSalary = Convert.ToDouble((FormView1.FindControl("txtSalary") as TextBox).Text); string empAddress = (FormView1.FindControl("txtAddress") as TextBox).Text; string depID = (FormView1.FindControl("txtDeptID") as TextBox).Text; string insertQuery = "insert into tblEmps values("+ID+",'"+empName+"','"+empGender+"', "+empSalary+",'"+empAddress+"',"+depID+")"; conn = new SqlConnection(cs); cmd = new SqlCommand(insertQuery, conn);