Form Class
Assembly: System.Web.Mobile (in system.web.mobile.dll)
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
Note: |
|---|
| 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" 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>
- AspNetHostingPermission for operating in a hosted environment. Demand value: LinkDemand; Permission value: Minimal.
- AspNetHostingPermission for operating in a hosted environment. Demand value: InheritanceDemand; Permission value: Minimal.
System.Web.UI.Control
System.Web.UI.MobileControls.MobileControl
System.Web.UI.MobileControls.Panel
System.Web.UI.MobileControls.Form
Windows 98, Windows Server 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.
Note: