Controls You Can Use on Web Forms ASP.NET Data Controls FormView Control
Essentially, to create a working FormView control, you need to add the control to the page and bind the control to a data source.
The below procedures show the minimum steps needed to display data in a FormView control.
This first example shows a FormView control that is bound to an AccessDataSource control.
- Declare an <
asp:formview
> element on the page. For syntax, see FormView Control Syntax.
- Specify the control’s DataSourceID property.
- Optionally set the control’s base properties.
<asp:formview id="plansView" runat="server"
datasourceid="plans"
width="90%"
cellpadding=5>
- Within the FormView declaration, declare an <ItemTemplate> element.
- Within the ItemTemplate, define each field from the data source that you intend to present.
To display the value of a field in the ItemTemplate, use DataBinding
Expression Syntax.
<itemtemplate>
<div class="header">
<h3 style="color:maroon"><%# Eval ( "Model" ) %></h3>
</div>
<br>
...
<p>National Average Cost to Build: <br>
<b style="color:maroon"><%# Eval ( "Cost", "{0:c}" ) %></b>
...
</itemtemplate>
NOTE: The ItemTemplate must contain at least one HTML element or server control that binds to a field from the data source; otherwise the FormView has nothing to render.
The DataSource property of a FormView can also be set programmatically, just like any Web server control property. Below shows an example of binding a data source to a FormView in code.
<script language="C#" runat="server">
protected void Page_Load ( object src, EventArgs e ) {
string query = "SELECT ProductType, ProductCode, ... FROM Products";
productFormView.DataSource = fetchReader ( query, "dbtutor" );
productFormView.DataBind ( );
}
</script>
NOTE: To see actual examples of using a FormView in real-world scenarios, see our featured applications Books and more ... and Hotels and more .... The product details pages of both applications demonstrate extensive use of the FormView in various configurations.
The above examples apply several concepts and methods that are described elsewhere in this workshop. For particulars, see ADO.NET Primer, Introduction to Data Binding in Web Forms, Web Forms Server Controls Templates.
If you intend to let users add and/or edit an item in the FormView, you need to define an <InsertItemTemplate> and/or an <EditItemTemplate> as well.
Introduction to the FormView Control Specifying Row Format in a FormView Control