Introduction
In the last two tutorials, we saw how to use TemplateField to customize the GridView and DetailsView input. TemplateField allows us to define a particular column with a high degree of autonomy, but either the GridView or the DetailsView is a bit too regular, simply to say that they all have a boxy lattice appearance. In many cases the appearance of such a lattice is very good, but sometimes we need to use an irregular display appearance. When you need to display a separate record, you can use the FormView control to achieve this relatively casual rendering.
Unlike DetailsView, FormView are not made up of assorted columns. You cannot add BoundField or TemplateField to a FormView, but FormView is rendered using a template. We can do this to understand FormView as a DetailsView control that contains only one TemplateField. FormView supports the following templates:
· itemtemplate– is used to render a special record in the FormView species
· headertemplate– is used to specify an optional header row
· footertemplate– is used to specify an optional footer row
· emptydatatemplate– when FormView datasource missing Records, EmptyDataTemplate will replace
ItemTemplate to generate markup language for the control
· pagertemplate– This template can be used to customize the paging interface if FormView is enabled for paging
· edititemtemplate/insertitemtemplate– if the FormView supports editing or inserting functions, then these two templates can be used to customize the relevant interface
In this tutorial, we will explain how to use the FormView control to render an irregular appearance for the product. FormView's ItemTemplate will use a combination of a header element and a <table> to display values for names, categories, vendors, and so on, instead of using a variety of columns (as shown in Figure one).
Figure one: FormView broke the DetailsView look of the lattice
The first step: binding Data to FormView
Open the Formview.aspx page and drag a FormView from the toolbox to the designer. FormView just added to the page, it's a gray box, which tells us it needs a ItemTemplate.
Figure II: FormView does not appear in the designer until a ItemTemplate is added
You can either manually write code (in the Source view) to add ItemTemplate, or you can implement automatic add by binding FormView to a data source control in the designer. This auto-generated ItemTemplate contains HTML code to display the names of each field, as well as a label control that displays the values of each field, and of course, the Text property of the label controls is already bound to the respective fields. This operation also generates both InsertItemTemplate and EditItemTemplate, which render an input control for each field of the data source control.
If you want to automatically generate a template, you should first use the GetProducts () method of the Productsbll class to add a ObjectDataSource control to FormView, which you can do with the FormView smart tag. This allows you to create a FormView with ItemTemplate, InsertItemTemplate, and EditItemTemplate. In the source view, delete InsertItemTemplate and EditItemTemplate, because we are now not interested in creating an editable and pluggable FormView. Then, empty the markup language code in the ItemTemplate so that I can work on a clean environment.
If you prefer to create ItemTemplate manually, you can drag a ObjectDataSource from the toolbox to the designer and configure it, and do not add a data source to the FormView in the designer. We should manually set the Forview DataSourceID property to the ID of ObjectDataSource in the source view, and then add ItemTemplate manually. Whichever way you decide to use it, your final FormView declaration tag code should look like this:
<asp:formview id= "FormView1" runat= "Server" datasourceid= "ObjectDataSource1" >
<ItemTemplate>
</ItemTemplate>
</asp:FormView>
Take the time to check the Enable paging (enabled paging) checkbox in the Smart tab of FormView, so that you can add a property such as allowpaging= "True" to the FormView Declaration markup Code. In addition, set the EnableViewState property to False.
Step two: Define the ItemTemplate Tag code (MARKUP)
After binding FormView to the ObjectDataSource control and configuring it to support paging, we are ready to specify the contents of the ItemTemplate. In this tutorial, let's display the product name in a
You can add these markup codes in the designer either through the FormView template editing interface or by manually entering the code in the source view. When working with templates, I found that coding directly in the source view would come faster, but you could use any way that makes you feel good enough. The following declaration markup code shows what the FormView should look like after completing the ItemTemplate structure:
<asp:formview id= "FormView1" runat= "Server" datasourceid= "ObjectDataSource1" allowpaging= "True" enableviewstate= "False" > <ItemTemplate>
Note that this data-binding syntax--taking <%# Eval ("ProductName")%> as an example--can be inserted directly into the template's output. That's because it's not necessary to bind to the Text property of a label control. For example, for example, we want to use the value of ProductName
CSS classes Productpropertylabel and productpropertyvalue the style used to specify the names and values of the product properties in <table>. These CSS classes are defined in Styles.css, which make the names of the product properties bold and right-aligned, and they also add a right padding to the value of the Product property.
Since FormView does not have CheckBoxField, to display the discontinued value as a checkbox, we must add a CheckBox control ourselves. Set the Enabled property of this checkbox control to false so that it is read-only and bind its Checked property to the Discontinued field.
After the ItemTemplate is finished, the product information is displayed in a more irregular way. To compare the output of the DetailsView in the previous section (figure III) and the FormView output discussed in this section (Figure IV).
Figure III: Blunt DetailsView output
Figure IV: Soft FormView output
Summarize
Although the GridView and DetailsView controls can use TemplateField to customize their output, they all appear as a lattice-like boxy. FormView is an ideal choice for those who need to use an irregular look to display a single record. Like DetailsView, FormView displays a separate record from its datasource. But it also has a different place from DetailsView, it's made up only of templates, and it doesn't support fields at all (fields. Translator Note: For example, CheckBoxField.
As we have seen in this section, FormView provides a more complex way of rendering when displaying a single record. In future tutorials We'll explain the DataList and Repeater controls, which can provide as complex a rendering as FormView, but they can display multiple columns (like the GridView).
Happy programming!
About the author
Scott Mitchell, with six asp/asp. NET book, is the founder of 4GuysFromRolla.com, has been applying Microsoft Web technology since 1998. Scott is an independent technical consultant, trainer, writer, recently completed a new book to be published by Sams Press, proficient in asp.net 2.0 within 24 hours. His contact email is mitchell@4guysfromrolla.com, or he can contact him through his blog Http://ScottOnWriting.NET.