Form 类
窗体表示的是 ASP.NET 移动网页中最外面的控件组。单个移动网页可以在最外层包含多个窗体。窗体不能嵌套;如果要嵌套容器,请使用 Panel 控件。有关更多信息,请参见 Form 控件介绍。若要显示特定窗体,请将当前页的 ActiveForm 属性设置为所需的窗体,或者将 Link 控件中的 NavigateUrl 属性设置为所需的窗体。可以在 Form 控件的文本内容中包含文本及其伴随的标记。使用模板时,请记住 Form 控件在 OnInit 方法中为窗体创建模板的实例,这一点很重要。窗体的 OnInit 方法在 Page_Load 和 Page_Init 之前调用。另外,由于窗体尚未创建,而页构造函数执行得太早,以致无法在 OnInit 方法中设置模板。若要更正这一点,请挂钩窗体自身的 OnInit 方法,并在该方法中创建模板的实例。有关更多信息,请参见 实现模板呈现。
示例 下面的代码示例演示如何创建一个带有两个窗体的页,并在两个窗体间建立链接。其中一个窗体具有一个复选框列表。当选择项并单击“提交”按钮时,该窗体便会显示一个包含选定项及其值的列表。请注意,Activate 事件方法会准备要显示的相应窗体
<%@ 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>

浙公网安备 33010602011771号