Building Microsoft ASP.NET Applications for Mobile Devices, Second Edition [Electronic resources] نسخه متنی

اینجــــا یک کتابخانه دیجیتالی است

با بیش از 100000 منبع الکترونیکی رایگان به زبان فارسی ، عربی و انگلیسی

Building Microsoft ASP.NET Applications for Mobile Devices, Second Edition [Electronic resources] - نسخه متنی

Andy Wigley; Peter Roxburgh

| نمايش فراداده ، افزودن یک نقد و بررسی
افزودن به کتابخانه شخصی
ارسال به دوستان
جستجو در متن کتاب
بیشتر
تنظیمات قلم

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

روز نیمروز شب
جستجو در لغت نامه
بیشتر
توضیحات
افزودن یادداشت جدید






Hidden Variables

Sometimes you might want to pass small amounts of information between Web pages without using session state. For example, suppose you need to collect information from a multipart form that the user fills in. In HTML, you'd pass the information from one page to another using input tags with a type value of hidden. But in WML, you'd set variables in the browser's cache and then post all their values to the server when the user completed the forms. The MobilePage class's HiddenVariables property provides this type of functionality. This property allows you to store name-value pairs, which the runtime then passes back and forth between the server and the client as hidden fields.

You need to know when to use the Session object to store information and when to use the HiddenVariables property. In fact, you don't need to use the HiddenVariables property to store information. This property just offers an alternative way to retain information between requests and responses; you can easily use the Session object in its place. Furthermore, you should use HiddenVariables only for small amounts of information. There are a number of reasons for this, including the following:



Many mobile devices have limited bandwidth. You don't need to use this bandwidth when you can easily store data on the server in the Session object.



WAP devices support compiled deck sizes of only up to approximately 1.4 KB. (A deck is the outermost element of a file of WML content. Each WML file must support exactly one deck.)



Listings 12-4 and 12-5 show code that uses the HiddenVariables property to pass information between forms in a multipart form. The user accesses the first form and enters his or her name. The runtime then adds this information to the HiddenVariables collection, and Form2 activates. The user then enters his or her e-mail address, which the runtime also adds to the HiddenVariables collection. Finally, Form3 activates, causing data contained within the HiddenVariables collection to populate the TextView control and be displayed to the user.





Note

The WAP gateway compiles WML files before they arrive at the WML browser. The WML browser uses these compiled files instead of the raw WML files stored on the Web server. When you use ASP.NET, the runtime generates uncompiled WML code, which it then sends to the WAP gateway. The WAP gateway in turn compiles the WML and forwards it to the WML browser on the client.


Listing 12-4: MobileWebForm1.aspx from the HiddenVariablesExample project






<%@ Register TagPrefix="mobile" 
Namespace="System.Web.UI.MobileControls"
Assembly="System.Web.Mobile" %>
<%@ Page language="c#" Codebehind="MobileWebForm1.aspx.cs"
Inherits="MSPress.MobWeb.HidVarEx.MobileWebForm1"
AutoEventWireup="false" %>
<mobile:Form id="Form1" runat="server">
<mobile:Label id="Label2" runat="server">Your name:</mobile:Label>
<mobile:TextBox id="TextBoxName" runat="server"></mobile:TextBox>
<mobile:Command id="Command1" runat="server">
Submit
</mobile:Command>
</mobile:Form>
<mobile:Form id="Form2" runat="server">
<mobile:Label id="Label1" runat="server">
Your e-mail:
</mobile:Label>
<mobile:TextBox id="TextBoxEmail" runat="server"/>
<mobile:Command id="Command2" runat="server">
Submit
</mobile:Command>
</mobile:Form>
<mobile:Form id="Form3" runat="server">
<mobile:TextView id="TextView1" runat="server">
TextView
</mobile:TextView>
</mobile:Form>











Listing 12-5: The code-behind file MobileWebForm1.aspx.cs from the HiddenVariablesExample project






using System;
using System.Collections;
using System.Web;
using System.Web.Mobile;
using System.Web.SessionState;
namespace MSPress.MobWeb.HidVarEx
{
public class MobileWebForm1 : System.Web.UI.MobileControls.MobilePage
{
protected System.Web.UI.MobileControls.Form Form2;
protected System.Web.UI.MobileControls.Command Command1;
protected System.Web.UI.MobileControls.Command Command2;
protected System.Web.UI.MobileControls.Form Form3;
protected System.Web.UI.MobileControls.TextView TextView1;
protected System.Web.UI.MobileControls.TextBox TextBoxName;
protected System.Web.UI.MobileControls.TextBox TextBoxEmail;
protected System.Web.UI.MobileControls.Form Form1;
public MobileWebForm1()
{
Page.Init += new System.EventHandler(Page_Init);
}
private void Page_Init(object sender, EventArgs e)
{
InitializeComponent();
}
private void InitializeComponent()
{
this.Command1.Click +=
new System.EventHandler(this.Command1_Click);
this.Command2.Click +=
new System.EventHandler(this.Command2_Click);
this.Form3.Activate +=
new System.EventHandler(this.Form3_Activate);
}
private void Command1_Click(object sender, System.EventArgs e)
{
HiddenVariables.Add(TextBoxName.ID,TextBoxName.Text);
this.ActiveForm=Form2;
}
private void Command2_Click(object sender, System.EventArgs e)
{
HiddenVariables.Add(TextBoxEmail.ID,TextBoxEmail.Text);
this.ActiveForm=Form3;
}
private void Form3_Activate(object sender, System.EventArgs e)
{
String FormData=";
foreach (Object o in HiddenVariables.Keys)
{
FormData+=o.ToString()+" "+HiddenVariables[o]+"<br>";
}
TextView1.Text=FormData;
}
}
}











Figure 12-1 shows how this code's output looks when viewed in the Nokia simulator.


Figure 12-1: Output of the HiddenVariablesExample project displayed in the Nokia simulator

/ 145