asp.net.ph

FormView Class

System.Web.UI.WebControls Namespace


.NET Framework version 2.0

Displays the values of a single record from a data source using user-defined templates. The FormView control allows you to edit, delete, and insert records.

FormView Class Members

Collapse   Constructors

Visibility Constructor Parameters
public FormView ( )

Collapse   Properties

Visibility Name Value Type Accessibility
public AllowPaging Boolean [ Get , Set ]
public BackImageUrl String [ Get , Set ]
public BottomPagerRow FormViewRow [ Get ]
public Caption String [ Get , Set ]
public CaptionAlign TableCaptionAlign [ Get , Set ]
public CellPadding Int32 [ Get , Set ]
public CellSpacing Int32 [ Get , Set ]
public CurrentMode FormViewMode [ Get ]
public DataItem Object [ Get ]
public DataItemCount Int32 [ Get ]
public DataItemIndex Int32 [ Get ]
public DataKey DataKey [ Get ]
public DataKeyNames String [ Get , Set ]
public DefaultMode FormViewMode [ Get , Set ]
public DeleteMethod String [ Get , Set ]
public EditItemTemplate ITemplate [ Get , Set ]
public EditRowStyle TableItemStyle [ Get ]
public EmptyDataRowStyle TableItemStyle [ Get ]
public EmptyDataTemplate ITemplate [ Get , Set ]
public EmptyDataText String [ Get , Set ]
public EnableModelValidation Boolean [ Get , Set ]
public FooterRow FormViewRow [ Get ]
public FooterStyle TableItemStyle [ Get ]
public FooterTemplate ITemplate [ Get , Set ]
public FooterText String [ Get , Set ]
public GridLines GridLines [ Get , Set ]
public HeaderRow FormViewRow [ Get ]
public HeaderStyle TableItemStyle [ Get ]
public HeaderTemplate ITemplate [ Get , Set ]
public HeaderText String [ Get , Set ]
public HorizontalAlign HorizontalAlign [ Get , Set ]
public InsertItemTemplate ITemplate [ Get , Set ]
public InsertMethod String [ Get , Set ]
public InsertRowStyle TableItemStyle [ Get ]
public ItemTemplate ITemplate [ Get , Set ]
public PageCount Int32 [ Get ]
public PageIndex Int32 [ Get , Set ]
public PagerSettings PagerSettings [ Get ]
public PagerStyle TableItemStyle [ Get ]
public PagerTemplate ITemplate [ Get , Set ]
public RenderOuterTable Boolean [ Get , Set ]
public Row FormViewRow [ Get ]
public RowStyle TableItemStyle [ Get ]
public SelectedValue Object [ Get ]
public TopPagerRow FormViewRow [ Get ]
public UpdateMethod String [ Get , Set ]

Collapse   Methods

Visibility Name Parameters Return Type
public ChangeMode ( FormViewMode newMode ) Void
protected CreateChildControls ( IEnumerable dataSource , Boolean dataBinding ) Int32
protected CreateControlStyle ( ) Style
protected CreateDataSourceSelectArguments ( ) DataSourceSelectArguments
protected CreateRow ( Int32 itemIndex , DataControlRowType rowType , DataControlRowState rowState ) FormViewRow
protected CreateTable ( ) Table
public DataBind ( ) Void
public DeleteItem ( ) Void
protected EnsureDataBound ( ) Void
protected ExtractRowValues ( IOrderedDictionary fieldValues , Boolean includeKeys ) Void
protected InitializePager ( FormViewRow row , PagedDataSource pagedDataSource ) Void
protected InitializeRow ( FormViewRow row ) Void
public InsertItem ( Boolean causesValidation ) Void
public IsBindableType ( Type type ) Boolean
protected LoadViewState ( Object savedState ) Void
protected OnBubbleEvent ( Object source , EventArgs e ) Boolean
protected OnItemCommand ( FormViewCommandEventArgs e ) Void
protected OnItemCreated ( EventArgs e ) Void
protected OnItemDeleted ( FormViewDeletedEventArgs e ) Void
protected OnItemDeleting ( FormViewDeleteEventArgs e ) Void
protected OnItemInserted ( FormViewInsertedEventArgs e ) Void
protected OnItemInserting ( FormViewInsertEventArgs e ) Void
protected OnItemUpdated ( FormViewUpdatedEventArgs e ) Void
protected OnItemUpdating ( FormViewUpdateEventArgs e ) Void
protected OnModeChanged ( EventArgs e ) Void
protected OnModeChanging ( FormViewModeEventArgs e ) Void
protected OnPageIndexChanged ( EventArgs e ) Void
protected OnPageIndexChanging ( FormViewPageEventArgs e ) Void
protected RaisePostBackEvent ( String eventArgument ) Void
protected SaveViewState ( ) Object
public SetPageIndex ( Int32 index ) Void
protected TrackViewState ( ) Void
public UpdateItem ( Boolean causesValidation ) Void

Remarks

The FormView control is used to display a single record from a data source. It is similar to the DetailsView control, except it displays user-defined templates instead of row fields. Creating your own templates gives you greater flexibility in controlling how the data is displayed. The FormView control supports the following features:

  • Binding to data source controls, such as SqlDataSource and ObjectDataSource.
  • Built-in inserting capabilities.
  • Built-in updating and deleting capabilities.
  • Built-in paging capabilities.
  • Programmatic access to the FormView object model to dynamically set properties, handle events, and so on.
  • Customizable appearance through user-defined templates, themes, and styles.

Templates

For the FormView control to display content, you need to create templates for the different parts of the control. Most templates are optional; however, you must create a template for the mode in which the control is configured. For example, a FormView control that supports inserting records must have an insert item template defined. The following table lists the different templates that you can create.

Template type Description
EditItemTemplate Defines the content for the data row when the FormView control is in edit mode. This template usually contains input controls and command buttons with which the user can edit an existing record.
EmptyDataTemplate Defines the content for the empty data row displayed when the FormView control is bound to a data source that does not contain any records. This template usually contains content to alert the user that the data source does not contain any records.
FooterTemplate Defines the content for the footer row. This template usually contains any additional content you would like to display in the footer row.

NOTE: As an alternative, you can simply specify text to display in the footer row by setting the FooterText property.

HeaderTemplate Defines the content for the header row. This template usually contains any additional content you would like to display in the header row.

NOTE: As an alternative, you can simply specify text to display in the header row by setting the HeaderText property.

ItemTemplate Defines the content for the data row when the FormView control is in read-only mode. This template usually contains content to display the values of an existing record.
InsertItemTemplate Defines the content for the data row when the FormView control is in insert mode. This template usually contains input controls and command buttons with which the user can add a new record.
PagerTemplate Defines the content for the pager row displayed when the paging feature is enabled (when the AllowPaging property is set to true). This template usually contains controls with which the user can navigate to another record.

NOTE: The FormView control has a built-in pager row user interface ( UI ). You need to create a pager template only if you want to create your own custom pager row.

To display the value of a field in an item template, use a data binding expression. For more information on data binding expressions, see DataBinding Expression Syntax.

Input controls in the edit item template and insert item template can be bound to the fields of a data source by using a two-way binding expression. This allows the FormView control to automatically extract the values of the input control for an update or insert operation. Two-way binding expressions also allow input controls in an edit item template to automatically display the original field values. .

Binding to Data

The FormView control can be bound to a data source control (such as SqlDataSource, AccessDataSource, ObjectDataSource and so on), or to any data source that implements the System.Collections.IEnumerable interface (such as System.Data.DataView, System.Collections.ArrayList, and System.Collections.Hashtable). Use one of the following methods to bind the FormView control to the appropriate data source type:

  • To bind to a data source control, set the DataSourceID property of the FormView control to the ID value of the data source control. The FormView control automatically binds to the specified data source control and can take advantage of the data source control's capabilities to perform inserting, updating, deleting, and paging functionality. This is the preferred method to bind to data.
  • To bind to a data source that implements the System.Collections.IEnumerable interface, programmatically set the DataSource property of the FormView control to the data source and then call the DataBind method. When using this method, the FormView control does not provide built-in inserting, updating, deleting, and paging functionality. You need to provide this functionality by using the appropriate event.

NOTE: This control can be used to display user input, which might include malicious client script. Check any information that is sent from a client for executable script, SQL statements, or other code before displaying it in your application. Whenever possible, it is strongly recommended that values are HTML-encoded before they are displayed in this control. ASP.NET provides an input request validation feature to block script and HTML in user input. Validation server controls are also provided to assess user input.

Data Operations

The FormView control provides many built-in capabilities that allow the user to update, delete, insert, and page through items in the control. When the FormView control is bound to a data source control, the FormView control can take advantage of the data source control's capabilities and provide automatic updating, deleting, inserting, and paging functionality.

NOTE: The FormView control can provide support for update, delete, insert, and paging operations with other types of data sources; however, you must provide an appropriate event handler with the implementation for these operations.

Because the FormView control uses templates, it does not provide a way to automatically generate command buttons to perform updating, deleting, or inserting operations. You must manually include these command buttons in the appropriate template. The FormView control recognizes certain buttons that have their CommandName properties set to specific values. The following table lists the command buttons that the FormView control recognizes.

Button Command name value Description
Cancel "Cancel" Used in updating or inserting operations to cancel the operation and to discard the values entered by the user. The FormView control then returns to the mode specified by the DefaultMode property.
Delete "Delete" Used in deleting operations to delete the displayed record from the data source. Raises the ItemDeleting and ItemDeleted events.
Edit "Edit" Used in updating operations to put the FormView control in edit mode. The content specified in the EditItemTemplate property is displayed for the data row.
Insert "Insert" Used in inserting operations to attempt to insert a new record in the data source using the values provided by the user. Raises the ItemInserting and ItemInserted events.
New "New" Used in inserting operations to put the FormView control in insert mode. The content specified in the InsertItemTemplate property is displayed for the data row.
Page "Page" Used in paging operations to represent a button in the pager row that performs paging. To specify the paging operation, set the CommandArgument property of the button to "Next", "Prev", "First", "Last", or the index of the page to which to navigate. Raises the PageIndexChanging and PageIndexChanged events.

NOTE: This type of button is typically used only in the pager template.

Update "Update" Used in updating operations to attempt to update the displayed record in the data source with the values provided by the user. Raises the ItemUpdating and ItemUpdated events.

Unlike the Delete button (which deletes the displayed record immediately), when the Edit or New button is clicked, the FormView control goes into edit or insert mode, respectively. In edit mode, the content contained in the EditItemTemplate property is displayed for the current data item. Typically, the edit item template is defined such that the Edit button is replaced with an Update and a Cancel button. Input controls that are appropriate for the field's data type (such as a TextBox or a CheckBox control) are also usually displayed with a field's value for the user to modify. Clicking the Update button updates the record in the data source, while clicking the Cancel button abandons any changes.

Likewise, the content contained in the InsertItemTemplate property is displayed for the data item when the control is in insert mode. The insert item template is typically defined such that the New button is replaced with an Insert and a Cancel button, and empty input controls are displayed for the user to enter the values for the new record. Clicking the Insert button inserts the record in the data source, while clicking the Cancel button abandons any changes.

The FormView control provides a paging feature, which allows the user to navigate to other records in the data source. When enabled, a pager row is displayed in the FormView control that contains the page navigation controls. To enable paging, set the AllowPaging property to true. You can customize the pager row by setting the properties of objects contained in the PagerStyle and the PagerSettings property. Instead of using the built-in pager row UI, you can create your own UI by using the PagerTemplate property.

Customizing the User Interface

You can customize the appearance of the FormView control by setting the style properties for the different parts of the control. The following table lists the different style properties.

Style property Description
EditRowStyle The style settings for the data row when the FormView control is in edit mode.
EmptyDataRowStyle The style settings for the empty data row displayed in the FormView control when the data source does not contain any records.
FooterStyle The style settings for the footer row of the FormView control.
HeaderStyle The style settings for the header row of the FormView control.
InsertRowStyle The style settings for the data row when the FormView control is in insert mode.
PagerStyle The style settings for the pager row displayed in the FormView control when the paging feature is enabled.
RowStyle The style settings for the data row when the FormView control is in read-only mode.

For examples illustrating use of this control, see the individual member types of this class. For syntax information, see FormView in ASP.NET Syntax for Web Controls.

See Also

FormView Web Server Control Skip Navigation Links




Home
Suggested Reading


Previous page Back to top Next page

© 2000-2010 Rey Nuñez All rights reserved.

If you have any question, comment or suggestion
about this site, please send us a note

You can help support asp.net.ph