昨天解決了這麼一個問題,是關於Mobile Control的。
用ASP.NET開發WAP程式,使用Mobile Control很方便,也比較容易上手,可是有時候想利用wml的一些元素,有不想很費勁的寫adapter。比如:想在頁面中添加一個wml的prev元素,如果用wml直接寫就可以寫成 <anchor title="Back">Back<prev /></anchor>
可是在一個mobile form中如何添加呢?我試了這麼幾種方法:
1、直接利用Response。就是在該MobileWebForm的Page_Load裡面填寫
string aa = "<card id=\"card1\" title=\"Title\">";
aa += "<P>";
aa += "<anchor title=\"back\">返回<prev/></anchor>";
aa += "</P>";
Response.Write(aa);
當然應該加一些裝置相容性的工作,這裡省略。
這樣做的問題是,這樣寫出來的東西肯定是在頁面的最頂端。
2、利用Application_EndRequest
要想將內容放到最後,就要在Response的內容Render完之後再添加,不過直接將上面的代碼放到這個事件中,在Openwave中卻顯示不出來,可能是因為openwave認為這個card已經在結束標誌之後了,所以這樣做了一下。
在Page_Load中寫
string aa = "<card id=\"card1\" title=\"Title\">";
Response.Write(aa);
然後再EndRequest中寫
if(this.Request.FilePath.IndexOf("yourmobileform.aspx") != -1)
{
string aa += "<P>";
aa += "<anchor title=\"back\">返回<prev/></anchor>";
aa += "</P>";
Response.Write(aa);
}
就把Openwave騙過去了
3、使用DeviceSpecific控制項。
這種方法屬於正統解決方案。在Form上拖一個Devicespecific控制項,設計為
<mobile:DeviceSpecific id="DeviceSpecific2" runat="server">
<Choice Filter="isWML11">
<FooterTemplate>
<P>
<anchor title="back">返回<prev /></anchor>
</P>
</FooterTemplate>
</Choice>
</mobile:DeviceSpecific>
可能是我不怎麼會用Openwave的緣故,openwave不理會我上面的東西。後來就自己加了一個裝置篩選條件,判斷isMobileDevice=true。這樣就可以了。
用<FooterTemplate>放在最後,用<HeaderTemplate>和<ScriptTemplate>都放在最上面了。
4、還有就是寫自己的Adapter,這個從略,我試過HtmlAdapter,沒問題,但是寫了一個Wml的Adapter, openwave不理會的,搞不大明白openwave究竟接收什麼類型的東西。
做一些補充,前面說的都是放在最前或者最後,要是放在中間怎麼辦。
可以在Form上放一個Panel,然後將這個DeviceSpecific放在這個panel中,然後在<ContentTemplate>中放wml內容
<P><anchor title="ddd">3eee<prev /></anchor></P>
這後面就可以放你要放的東西了
比如<mobile:Label id="Label1" runat="server">Label</mobile:Label>
可是這個Label不能被代碼訪問到,所以需要手動添加事件
<mobile:Label id="Label1" runat="server" OnLoad="SetText">Label</mobile:Label>
然後在.cs中加入這個函數實現
public void SetText(object sender,System.EventArgs e)
{
(sender as System.Web.UI.MobileControls.Label).Text = "dkkd";
}
繼續更新(2004.9.9)
要放在中間,不一定要用DeviceSpecific,我們可以使用自訂控制項來實現,比如我們可以從Label派生一個出來,然後重載OnRender
protected override void OnRender(System.Web.UI.HtmlTextWriter writer)
{
if(this.MobilePage.Device.PreferredRenderingType == "xhtml-mp" ||
this.MobilePage.Device.PreferredRenderingType == "wml11")
{
writer.WriteBeginTag("anchor");
writer.Write(" title=\"" + this.title + "\">" + this.Text);
writer.WriteBeginTag("prev");
writer.WriteEndTag("prev");
writer.WriteEndTag("anchor"); }
else
base.OnRender(writer);
}
然後將這個控制項拖到Form中就可以實現了