Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 3.5
Form Class
Collapse All/Expand All Collapse All
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
.NET Framework Class Library
Form Class

Provides the capability to group controls together.

Namespace:  System.Web.UI.MobileControls
Assembly:  System.Web.Mobile (in System.Web.Mobile.dll)
Visual Basic (Declaration)
<DesignerAdapterAttribute(GetType(HtmlFormAdapter))> _
<AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level := AspNetHostingPermissionLevel.Minimal)> _
<AspNetHostingPermissionAttribute(SecurityAction.InheritanceDemand, Level := AspNetHostingPermissionLevel.Minimal)> _
Public Class Form _
    Inherits Panel _
    Implements ITemplateable, IPostBackEventHandler
Visual Basic (Usage)
Dim instance As Form
C#
[DesignerAdapterAttribute(typeof(HtmlFormAdapter))]
[AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
[AspNetHostingPermissionAttribute(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
public class Form : Panel, ITemplateable, IPostBackEventHandler
Visual C++
[DesignerAdapterAttribute(typeof(HtmlFormAdapter))]
[AspNetHostingPermissionAttribute(SecurityAction::LinkDemand, Level = AspNetHostingPermissionLevel::Minimal)]
[AspNetHostingPermissionAttribute(SecurityAction::InheritanceDemand, Level = AspNetHostingPermissionLevel::Minimal)]
public ref class Form : public Panel, 
    ITemplateable, IPostBackEventHandler
JScript
public class Form extends Panel implements ITemplateable, IPostBackEventHandler
ASP.NET
<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

NoteNote:

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

Visual Basic
<%@ Page Language="VB" 
    Inherits="System.Web.UI.MobileControls.MobilePage" %>
<%@ Register TagPrefix="mobile" 
    Namespace="System.Web.UI.MobileControls" 
    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
        SelectionList1.DataBind()
    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
        Next

        ' Put the text into the TextView
        txtView.Text = txt
        ' Add the TextView to the form
        Form1.Controls.Add(txtView)

        ' 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
        Get
            Dim o As Object = ViewState("FormCount")
            If IsNothing(o) Then
                Return 0
            Else
                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
            Get
                Return _TaskName
            End Get
        End Property
        Public ReadOnly Property Status() As String
            Get
                Return _Status
            End Get
        End Property
    End Class

</script>

<html xmlns="http:'www.w3.org/1999/xhtml" >
<body>
    <!-- The first form: Form1 -->
    <mobile:Form ID="Form1" Runat="server"
        OnDeactivate="Form1_Deactivate" 
        OnActivate="Form1_Activate">
        <mobile:Label ID="message1" Runat="server">
            Welcome to ASP.NET
        </mobile:Label>

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

    <!-- The second form: Form2 -->
    <mobile:Form ID="Form2" Runat="server" 
        OnActivate="Form2_Activate">
        <mobile:Label ID="message4" Runat="server">
           Welcome to ASP.NET
        </mobile:Label> 
        <mobile:Link ID="Link2" Runat="server" 
            NavigateUrl="#Form1" Text="Back" />
    </mobile:Form>
</body>
</html>
C#
<%@ Page Language="C#" 
    Inherits="System.Web.UI.MobileControls.MobilePage" %>
<%@ Register TagPrefix="mobile" 
    Namespace="System.Web.UI.MobileControls" 
    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 void Form1_Activate(object sender, EventArgs e)
    {
        string viewText = "You have viewed this Form {0} times.";

        if (count == 0) // First viewing
            message2.Text = "Welcome to the Form Sample";
        else // subsequent viewings
            message2.Text = String.Format(viewText,
              (count + 1).ToString());

        // 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.
        ArrayList arr = 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;
        SelectionList1.DataBind();
    }

    // When Form1 is deactivated
    private void Form1_Deactivate(object sender, EventArgs e)
    {
        count++;
    }

    // When Form2 is activated
    private void Form2_Activate(object sender, EventArgs e)
    {
        Form2.BackColor = Color.DarkGray;
        Form2.ForeColor = Color.White;
        Form2.Font.Bold = BooleanOption.True;
    }

    // The the Submit button is clicked
    protected void Command1_OnSubmit(object sender, EventArgs e)
    {
        message2.Text = "FORM RESULTS:";
        message2.Font.Bold = BooleanOption.True;

        // Display a list of selected items with values
        for (int i = 0; i < SelectionList1.Items.Count; i++)
        {
            // Create a string and a TextView control
            TextView txtView = new TextView();
            string txt = "";
            string spec = "{0} is {1}<br />";

            // Display a list of selected items with values
            // Get the list item
            MobileListItem itm = SelectionList1.Items[i];

            // List the selected items and values
            if (itm.Selected)
            {
                txt += String.Format(spec, itm.Text, itm.Value);
            }

            // Put the text into the TextView
            txtView.Text = txt;
            // Add txtView to the form
            Form1.Controls.Add(txtView);
        }

        // Hide unnecessary controls
        SelectionList1.Visible = false;
        link1.Visible = false;
        Command1.Visible = false;
    }

    // Property to persist the count between postbacks
    private int count
    {
        get
        {
            object o = ViewState["FormCount"];
            return o == null ? 0 : (int)o;
        }
        set { ViewState["FormCount"] = value; }
    }


    // A custom class for the task array
    private class Task
    {
        private String _TaskName;
        private String _Status;

        public Task(String TaskName, String Status)
        {
            _TaskName = TaskName;
            _Status = Status;
        }

        public String TaskName
        {
            get { return _TaskName; }
        }
        public String Status
        {
            get { return _Status; }
        }
    }
</script>

<html  >
<body>
    <!-- The first form: Form1 -->
    <mobile:Form ID="Form1" Runat="server"
        OnDeactivate="Form1_Deactivate" 
        OnActivate="Form1_Activate">
        <mobile:Label ID="message1" Runat="server">
            Welcome to ASP.NET
        </mobile:Label>

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

    <!-- The second form: Form2 -->
    <mobile:Form ID="Form2" Runat="server" 
        OnActivate="Form2_Activate">
        <mobile:Label ID="message4" Runat="server">
           Welcome to ASP.NET
        </mobile:Label> 
        <mobile:Link ID="Link2" Runat="server" 
            NavigateUrl="#Form1" Text="Back" />
    </mobile:Form>
</body>
</html>
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
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks | Privacy Statement
Page view tracker