【发布时间】:2011-03-24 05:20:24
【问题描述】:
您知道 iDevices 将如何根据 type 属性的 HTML 5 值显示不同的键盘布局吗?因此,<input type="email"... 将显示 iPad 的电子邮件键盘布局。我正在使用 .net 文本框,但希望 iDevices 为该字段显示适当的键盘布局。但是当 TextBox 控件呈现时,type 属性会被覆盖为“text”。有什么想法吗?
【问题讨论】:
您知道 iDevices 将如何根据 type 属性的 HTML 5 值显示不同的键盘布局吗?因此,<input type="email"... 将显示 iPad 的电子邮件键盘布局。我正在使用 .net 文本框,但希望 iDevices 为该字段显示适当的键盘布局。但是当 TextBox 控件呈现时,type 属性会被覆盖为“text”。有什么想法吗?
【问题讨论】:
还有另一种解决方案。不幸的是,以上所有内容都需要大量编码、新类、KB 安装,而 iPhone os 3+ 不会更新。
如果您希望手机在 ASP.NET 中为文本框控件显示数字小键盘,请使用此选项: 模式="[0-9]*"
<asp:textbox runat="server" id="txtNumber" pattern="[0-9]*" />
【讨论】:
我刚刚用
public class TextBoxControlAdapter : ControlAdapter
{
protected override void Render(HtmlTextWriter writer) {
var textBox = Control as WebControl;
if (textBox != null) {
var type = textBox.Attributes["type"];
if (!String.IsNullOrEmpty(type)) {
writer.AddAttribute("type", type);
}
}
base.Render(writer);
}
}
在 App_Browsers\AdapterMappings.browser(例如)中,添加:
<adapter controlType="System.Web.UI.WebControls.TextBox" adapterType="MyProject.ControlAdapters.TextBoxControlAdapter" />
【讨论】:
type="email" 添加到渲染输出中,但还有另一个type 属性,其值为"text"。
.NET 框架 4 的更新允许您指定类型属性。
http://support.microsoft.com/kb/2468871.
在页面下方查看功能 3
Feature 3
New syntax lets you define a TextBox control that is HTML5 compatible. For example, the following code defines a TextBox control that is HTML5 compatible:
<asp:TextBox runat="server" type="some-HTML5-type" />
【讨论】:
也可以实现替换为“”
但是,当您使用 Visual 在 [...].aspx 文件中定义 type='email' id='txtEmail' /> 时,会出现一个花絮问题工作室 2010。
在使用 type='email' 时,Visual Studio 将删除一个实例变量定义 - “protected global::System.Web.UI.HtmlControls.HtmlInputText txtEmail;”在 [...].aspx.designer.cs 文件中。
因此,您只需在 [...].aspx 中定义“”。 除了在 [...].aspx 中添加 type='email' 属性外,您还可以在代码隐藏文件中提供一个属性,如下所示,
txtEmail.Attributes.Add("type", "email");
【讨论】:
不容易解决,但您可以创建一些新控件,例如:
public EnhancedTextBox : TextBox {
public Html5Type Html5Type { get; set; }
override AddAttributesToRender(HtmlTextWriter writer) {
// add attribute according to the selected type in the property
// something like
writer.AddAttribute("type", "email");
base.AddAttributesToRender(writer);
}
}
并使用它而不是普通的文本框
【讨论】:
<%@ Register Assembly="ServerControl" TagPrefix="mytag" Namespace="ServerControl"%>引用顶部的程序集,然后通过<mytag:EnhancedTextbox runat="server" ... />使用它