| zhixin 的个人资料MYspace.com照片日志列表 | 帮助 |
|
4月28日 5/1又要5/1了,长假中要加班3天,感觉没有了假期的味道,不过还是要提起精神来!好好安排一下,准备利用这时间把vb6.0和asp.net控件开发的东西好好看一边,补习不懂的也准备将来的。5/1还有重要的事就是带gf见家长。好了,为学习,工作,爱情三不忘而继续努力 4月19日 mobile textbox numeric 问题解决方法(原创)使用.net mobile开发WAP,对于textbox ,手机无法自动切换到数字输入,numeric属性似乎不起作用?
使用强制输出wml,数字切换可以使了,用openwave查看原文件,textbox 为<input id="textbox1" format="*N">
原来在wml中input标记的format属性为控制文字格式,N为数字,A为大写字母,a为小写字母依次等等。 那既然wml输出能有format格式可用,那对于xhtml会有什么那?抱着疑问用.NET Reflector打开 system.web.mobile.dll, 找到xhtmltextboxAdapter类,果然,在render方法中没有对numeric有相应的处理。这就是不能切换数字的真实原因了。那么xhtml中就没有类似format属性吗?
继续google,翻到以前保存的一篇老外的文章,证实之前的估计。但后来看到可以用wap css 的来代替处理。眼前一亮,可用Adapter来重写xhtml输出,(此处的依据是,.NET mobile control使用adapter模式呈现出不同手持设备支持的标记语言,比如使用htmlAdapter针对支持html微浏览器输出html,用wmlAdapter对仅支持wml的设备输出wml,对支持最新wap2.0的设备输出xhtml,因此对应与Textbox有htmlTextboxAdapter,wmlTextboxAdapter,xhtmlTextboxAdapter,cHtmlTextboxAdapter 四种不同的适配器类)
1.新添项目->类库命名为MobileClassLibrary->修改类名为NumericTextBoxAdapter
Namespace CustomMobileControls
Public Class NumericTextBoxAdapter
Inherits System.Web.UI.MobileControls.Adapters.XhtmlAdapters.XhtmlTextBoxAdapter Public Overrides Sub Render(ByVal writer As XhtmlMobileTextWriter) Me.ConditionalClearPendingBreak(writer) Me.ConditionalEnterStyle(writer, MyBase.Style) Me.ConditionalRenderOpeningSpanElement(writer) 'If (Me.Device.Item(XhtmlConstants.RequiresOnEnterForward) Is "true") Then ' writer.AddOnEnterForwardSetVar(Me.Control.UniqueID, Me.Control.Text) 'End If writer.WriteBeginTag("input") writer.WriteAttribute("name", Me.Control.UniqueID) 'Me.ConditionalRenderCustomAttribute(writer, XhtmlConstants.AccessKeyCustomAttribute) Dim text As String = Me.Control.Text If (((Not [text] Is Nothing) AndAlso ([text].Length > 0)) AndAlso Not Me.Control.Password) Then writer.Write(" value=""") writer.WriteEncodedText([text]) writer.Write("""") End If If (Me.Control.Size > 0) Then writer.WriteAttribute("size", Me.Control.Size.ToString(CultureInfo.InvariantCulture)) End If If (Me.Control.MaxLength > 0) Then writer.WriteAttribute("maxlength", Me.Control.MaxLength.ToString(CultureInfo.InvariantCulture)) End If '此处增加输出style属性加上wap css If (Me.Control.Numeric = True) Then writer.WriteAttribute("style", "-wap-input-format: '8N'") End If Dim a As String = Me.Device.Item("requiresInputTypeAttribute") If Me.Control.Password Then writer.WriteAttribute("type", "password") ElseIf ((Not a Is Nothing) AndAlso String.Equals(a, "true", StringComparison.OrdinalIgnoreCase)) Then writer.WriteAttribute("type", "text") End If writer.Write("/>") Me.ConditionalSetPendingBreakAfterInline(writer) Me.ConditionalRenderClosingSpanElement(writer) Me.ConditionalExitStyle(writer, MyBase.Style) End Sub End Class
End Namespace
以上代码继承XhtmlTextBoxAdapter,覆盖了render方法,只是简单的增加了style属性,"-wap-input-format: '8N'" 实现和fomat="8N"相同的效果。注销XhtmlConstants是未找到定义。
2.在web.config中增加:
<mobileControls cookielessDataDictionaryType="System.Web.Mobile.CookielessData">
<device name="NumericTextboxAdapters" inheritsFrom="XhtmlDeviceAdapters"> <control name="System.Web.UI.MobileControls.TextBox" adapter="MobileClassLibrary.CustomMobileControls.NumericTextBoxAdapter, MobileClassLibrary"/> </device> </mobileControls> 使用手机再访问网站,果然所有numeric设为true的textbox自动切换到数字输入方式。新适配器成功!
这里还要明白的是原来IIS 对于不能识别的手机型号自动输出为xhtml,而非wml,那是因为IIS被用来做服务器,默认生成更标准的xhtml了。
|
|
|