This documentation is archived and is not being maintained.

Form Class

Provides the capability to group controls together.

Namespace:  System.Web.UI.MobileControls
Assembly:  System.Web.Mobile (in System.Web.Mobile.dll)

<DesignerAdapterAttribute(GetType(HtmlFormAdapter))> _
<AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level := AspNetHostingPermissionLevel.Minimal)> _
<AspNetHostingPermissionAttribute(SecurityAction.InheritanceDemand, Level := AspNetHostingPermissionLevel.Minimal)> _
Public Class Form _
	Inherits Panel _
	Implements ITemplateable, IPostBackEventHandler
Dim instance As Form
<mobile:Form />

A form represents the outermost grouping of controls within an ASP.NET mobile Web page. An individual mobile Web page can contain multiple forms at the outermost level. Forms cannot be nested; use Panel controls if you want to nest containers. For more information, see Introduction to the Form Control. To display a specific form, either set the ActiveForm property on the current page to the desired form, or set the NavigateUrl property in a Link control to the desired form. You can include literal text along with its accompanying markup tags in the text contents of the Form control. When using templates, it is important to remember that the Form control creates instances of templates in the OnInit method for the form. The OnInit method for the form is called before Page_Load and Page_Init. Also, the page constructor executes too early to set templates in the OnInit method because the form is not yet created. To correct this, hook the form's own OnInit method, and create an instance of the template there. For more information, see Implementing Templated Rendering.

The following code example shows how to create a page with two forms along with links between the two forms. One form has a check box list. When items are selected and the Submit button is clicked, the form presents a list of the selected items and their values. Notice that the Activate event methods prepare the respective forms for display


The following code example uses the single-file code model and may not work correctly if copied directly into a code-behind file. This code example must be copied into an empty text file that has an .aspx extension. For more information, see ASP.NET Web Page Syntax Overview

<%@ Page Language="VB"  
    Inherits="System.Web.UI.MobileControls.MobilePage" %>
<%@ Register TagPrefix="mobile"  
    Assembly="System.Web.Mobile" %>
<%@ Import Namespace="System.Web.Mobile" %>
<%@ Import Namespace="System.Web.UI.MobileControls" %>
<%@ Import Namespace="System.Drawing" %>

<script runat="server">
    ' When Form1 is activated 
    Private Sub Form1_Activate(ByVal sender As Object, _
        ByVal e As EventArgs)
        Dim viewText As String = "You have viewed this Form {0} times." 

        ' First viewing 
        If (count = 0) Then
            message2.Text = "Welcome to the Form Sample" 
        Else ' subsequent viewings
            message2.Text = String.Format(viewText, _
              (count + 1).ToString())
        End If 

        ' Format the form
        Form1.Alignment = Alignment.Center
        Form1.Wrapping = Wrapping.NoWrap
        Form1.BackColor = Color.LightBlue
        Form1.ForeColor = Color.Blue
        Form1.Paginate = True 

        ' Create an array and add the tasks to it. 
        Dim arr As ArrayList = New ArrayList()
        arr.Add(New Task("Verify transactions", "Done"))
        arr.Add(New Task("Check balance sheet", "Scheduled"))
        arr.Add(New Task("Send report", "Pending"))

        ' Bind the SelectionList to the array.
        SelectionList1.DataValueField = "Status"
        SelectionList1.DataTextField = "TaskName"
        SelectionList1.DataSource = arr
    End Sub 

    ' When Form1 is deactivated 
    Private Sub Form1_Deactivate(ByVal sender As Object, _
        ByVal e As EventArgs)

        count += 1
    End Sub 

    ' When Form2 is activated 
    Private Sub Form2_Activate(ByVal sender As Object, _
        ByVal e As EventArgs)

        Form2.BackColor = Color.DarkGray
        Form2.ForeColor = Color.White
        Form2.Font.Bold = BooleanOption.True 
    End Sub 

    ' The the Submit button is clicked 
    Protected Sub Command1_OnSubmit(ByVal sender As Object, _
        ByVal e As EventArgs)

        Dim i As Integer
        message2.Text = "FORM RESULTS:"
        message2.Font.Bold = BooleanOption.True 

        ' Create a string and a TextView control 
        Dim txtView As TextView = New TextView()
        Dim txt As String = "" 
        Dim spec As String = "{0} is {1}<br />" 

        ' Display a list of selected items with values 
        For i = 0 To SelectionList1.Items.Count - 1
            ' Get the ListItem 
            Dim itm As MobileListItem = SelectionList1.Items(i)

            ' List the selected items and values 
            If itm.Selected Then
                txt &= String.Format(spec, itm.Text, itm.Value)
            End If 

        ' Put the text into the TextView
        txtView.Text = txt
        ' Add the TextView to the form

        ' Hide unnecessary controls
        SelectionList1.Visible = False
        link1.Visible = False
        Command1.Visible = False 
    End Sub 

    ' Property to persist the count between postbacks 
    Private Property count() As Integer 
            Dim o As Object = ViewState("FormCount")
            If IsNothing(o) Then 
                Return 0
                Return CType(o, Integer)
            End If 
        End Get 
        Set(ByVal value As Integer)
            ViewState("FormCount") = value
        End Set 
    End Property 

    ' A custom class for the task array 
    Private Class Task
        Private _TaskName As String 
        Private _Status As String 

        Public Sub New(ByVal TaskName As String, ByVal Status As String)
            _TaskName = TaskName
            _Status = Status
        End Sub 

        Public ReadOnly Property TaskName() As String 
                Return _TaskName
            End Get 
        End Property 
        Public ReadOnly Property Status() As String 
                Return _Status
            End Get 
        End Property 
    End Class


<html xmlns="http:'" >
    <!-- The first form: Form1 -->
    <mobile:Form ID="Form1" Runat="server"
        <mobile:Label ID="message1" Runat="server">
            Welcome to ASP.NET

        <mobile:Label ID="message2" Runat="server" />
        <mobile:SelectionList Runat="server" 
            ForeColor="red" SelectType="CheckBox" />
        <mobile:Link ID="link1" Runat="server" 
            Text="Next Form" /><br />
        <mobile:Command ID="Command1" Runat="server" 
            Text="Submit" OnClick="Command1_OnSubmit" />

    <!-- The second form: Form2 -->
    <mobile:Form ID="Form2" Runat="server" 
        <mobile:Label ID="message4" Runat="server">
           Welcome to ASP.NET
        <mobile:Link ID="Link2" Runat="server" 
            NavigateUrl="#Form1" Text="Back" />

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1