ARTICLE

FormView control Templates in ASP.NET 2.0

Posted by Yogesh Verma Articles | ASP.NET using VB.NET April 17, 2007
This article explain you the type of templates available in FormView.
 
Reader Level:

We use the FormView control for display a single record from the data source; there are seven types of Templates in which most of the Templates are optional.

 

You must create a Template for the mode in which the FormView control is configured. For example, a FormView control that supports updating records must have an EditItemTemplate defined.

 

Types of Templates: 

  • HeaderTemplate
  • FooterTemplate
  • ItemTemplate
  • InsertItemTemplate
  • EditItemTemplate
  • EmptyDataTemplate
  • PagerTemplate

HeaderTemplate:


Header Template defines the contents which we want to display in the header row of the FormView Control.

 

FooterTemplate:

Footer Template defines the contents which we want to display in the footer row of the FormView Control.


ItemTemplate:

 

ItemTemplates defines the contents for the FormView row when it is in read only mode. This template basically used for display the existing data from the data source.

 

InsertItemTemplate:

 

InsertItemTemplates defines the contents for the FormView row when it is in insert mode. This template contains the input controls and commands for inserting the new record.

 

EditItemTemplate:

 

EditItemTemplates defines the contents for the FormView row when it is in edit mode. This template contains the input controls and commands for editing the existing record.

 

EmptyDataTemplate:

 

EmptyDataTemplate basically used for showing the alert to the user if there is no record in the bound column.

 

PagerTemplate:

 

PagerTemplate defines the content for the pager row when the "AllowPaging" property is true. It also contains the controls by using that, user can navigate to another record.

Login to add your contents and source code to this article
Article Extensions
Contents added by manik prabhu on Sep 30, 2009

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

<title>Untitled Page</title>

</head>

<body>

<form id="form1" runat="server">

<asp:ScriptManager ID="ScriptManager1" runat="server" />

<div>

<asp:FormView ID="FormView1" runat="server" DataKeyNames="hostid" DataSourceID="SqlDataSource1" AllowPaging="True" Height="60px" Width="266px">

<EditItemTemplate>

hostid:

<asp:Label ID="hostidLabel1" runat="server" Text='<%#Eval("hostid") %>'></asp:Label><br />

hostingname:

<asp:TextBox ID="hostingnameTextBox" runat="server" Text='<%# Bind("hostingname") %>'>

</asp:TextBox><br />

<asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True" CommandName="Update"

Text="Update">

</asp:LinkButton>

<asp:LinkButton ID="UpdateCancelButton" runat="server" CausesValidation="False" CommandName="Cancel"

Text="Cancel">

</asp:LinkButton>

</EditItemTemplate>

<InsertItemTemplate>

hostingname:

<asp:TextBox ID="hostingnameTextBox" runat="server" Text='<%# Bind("hostingname") %>'>

</asp:TextBox><br />

<asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True" CommandName="Insert"

Text="Insert">

</asp:LinkButton>

<asp:LinkButton ID="InsertCancelButton" runat="server" CausesValidation="False" CommandName="Cancel"

Text="Cancel">

</asp:LinkButton>

</InsertItemTemplate>

<ItemTemplate>

hostid:

<asp:Label ID="hostidLabel" runat="server" Text='<%# Eval("hostid") %>'></asp:Label><br />

hostingname:

<asp:Label ID="hostingnameLabel" runat="server" Text='<%# Bind("hostingname") %>'>

</asp:Label><br />

<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="true" CommandArgument="Edit">Edit</asp:LinkButton>

</ItemTemplate>

<EmptyDataTemplate>

No Records Found

</EmptyDataTemplate>

<PagerTemplate>

<asp:LinkButton ID="btnFirst" CommandName="Page" CommandArgument="First" Text="First" runat="server"/>

<asp:LinkButton ID="btnPrevious" CommandName="Page" CommandArgument="Prev" Text="Previous" runat="server"/>

<asp:LinkButton ID="btnNext" CommandName="Page" CommandArgument="Next" Text="Next" runat="server"/>

<asp:LinkButton ID="btnLast" CommandName="Page" CommandArgument="Last" Text="Last" runat="server"/>

</PagerTemplate>

</asp:FormView>

<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:hostnowConnectionString %>"

SelectCommand="SELECT * FROM [tbl_Hosting]">

</asp:SqlDataSource>

&nbsp;

</div>

</form>

</body>

</html>

share this article :
post comment
 

Hi,

How to upload and display image thru formview control? I tried lot but its neither giving me error nor displaying image. In Sql database I have stored image with Image field type.

<asp:FormView ID="frmProfile" runat = "server" DataSourceID ="SqlDataSource1">

 

<ItemTemplate >

<table border ="0">

<tr><td><asp:LinkButton ID="lnkEdit" runat ="server" Text="Edit" CausesValidation ="false" CommandName ="Edit" /></td>

</tr><tr><td><asp:Image ID="imgProfile" runat ="server" ImageUrl ='<%# Eval("photo")%>' AlternateText ="Not Available" /></td>


</ItemTemplate>

<EditItemTemplate >

<table border ="0">

<tr><td><asp:LinkButton ID="lnkUpdate" runat="server" Text="Update" CausesValidation ="true" CommandName ="Update" /> </td>

<td><asp:LinkButton ID="lnkCancel" runat ="server" Text="Cancel" CausesValidation ="false" CommandName ="Cancel" /></td></tr> <hr />

<tr><td><asp:Image ID="imgProfileEdit" Width = "150" Height = "125" ImageUrl ='<%# Eval("photo")%>' runat = "server" AlternateText ="Not Available" /> </td>

</table>

</EditItemTemplate>

</asp:FormView>

<asp:SqlDataSource ID="SqlDataSource1" runat ="server" ConnectionString ="<%$ ConnectionStrings:cnnvastu %>" SelectCommand ="LoadData" UpdateCommand ="updateProfile" UpdateCommandType ="StoredProcedure" SelectCommandType="StoredProcedure">

my VB code is -

Public Sub frmProfile_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles frmProfile.DataBound

Dim row As FormViewRow = frmProfile.Row

Dim rowview As DataRowView = CType(frmProfile.DataItem, DataRowView)

Dim profilePhoto As Image = Nothing

Select Case frmProfile.CurrentMode

Case FormViewMode.ReadOnly

profilePhoto = CType(row.FindControl("imgProfile"), Image)

Case FormViewMode.Edit

profilePhoto = CType(row.FindControl("imgProfileEdit"), Image)

Case Else

End Select

If profilePhoto IsNot Nothing Then

profilePhoto.ToolTip = "Not Available"

End If

End Sub



Let me know why its not working.. its displaying AlternateText every time.

Posted by Sharmila Dec 21, 2009

please send me Formview edit,update,delete Code Sample in vb.net by using connection string,command objects.

Posted by Narayan Gadekar Aug 20, 2008

I need a example of form view in which i want to calculate some integer values i.e i put calues in two fields and sum of the two fields autometically display in the 3rd feild.... Plz send me that example ........... Thanx

Posted by zeeshan saleem Aug 16, 2008

please send me the example of form view control in c#. thanks & regards charanpreet

Posted by charanpreet singh Mar 20, 2008
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor