Html 如何在服务器端 .NET 中使用 HTML5 电子邮件输入类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3232079/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I use HTML5 email input type with server-side .NET
提问by Michael
As I understand it, the <input type=email>
element in HTML5 will render as a simple text field in browsers that do not support the tag. On other browsers it will render properly, like on the iPhone it will bring up the e-mail keyboard layout.
据我了解,<input type=email>
HTML5 中的元素将在不支持标签的浏览器中呈现为一个简单的文本字段。在其他浏览器上它会正确呈现,就像在 iPhone 上一样,它会显示电子邮件键盘布局。
I'd like to use this in a project but my input fields are <asp:TextBox>
controls. How can I use the HTML5 element but still access its data server-side like the rest of my fields?
我想在项目中使用它,但我的输入字段是<asp:TextBox>
控件。如何使用 HTML5 元素但仍像其他字段一样访问其数据服务器端?
回答by Chris Diver
There is an update for .NET framework 4 which allows you to specify the type attribute
.NET framework 4 有一个更新,它允许您指定类型属性
http://support.microsoft.com/kb/2468871.
http://support.microsoft.com/kb/2468871。
See feature 3
way down the page
查看feature 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" />
特点 3
新语法允许您定义与 HTML5 兼容的 TextBox 控件。例如,以下代码定义了一个与 HTML5 兼容的 TextBox 控件:
<asp:TextBox runat="server" type="some-HTML5-type" />
回答by Milox
you can try adding the attributes manually, like:
您可以尝试手动添加属性,例如:
TextBox1.Attributes["type"] = "email";
TextBox1.Attributes["type"] = "url";
TextBox1.Attributes["type"] = "number";
回答by Eli
Sorry I'm a bit late to the party, though I think that others can benefit from what I did. I have a page which is HTML 5 though we still have .NET 3.5. We wanted to keep the .NET element, though have the type change to email. I've tried several methods (including Milox above) to no avail, though the one which worked for me was the following: I added a JavaScript property to the element itself inline (when I put it in a script tag it wouldn't pick up for some reason...) Here is what your tag would look like if you use my changes:
抱歉,我参加聚会有点晚了,但我认为其他人可以从我所做的事情中受益。我有一个 HTML 5 页面,但我们仍然有 .NET 3.5。我们希望保留 .NET 元素,但将类型更改为电子邮件。我尝试了几种方法(包括上面的 Milox)都无济于事,但对我有用的方法如下:我向元素本身内联添加了一个 JavaScript 属性(当我将它放在脚本标签中时,它不会选择出于某种原因...)如果您使用我的更改,您的标签将是什么样子:
<asp:TextBox runat="server" type="email" onfocus="this.type='email'"/>
<asp:TextBox runat="server" type="email" onfocus="this.type='email'"/>
Eli
伊莱
回答by John Bledsoe
Whether or not it is accessible as a server control, you should be able to access the HttpRequest.Form
collection and retrieve the value. No matter what the browser does with the tag, it has to submit a string to the server.
无论它是否可作为服务器控件访问,您都应该能够访问HttpRequest.Form
集合并检索值。无论浏览器如何处理标签,它都必须向服务器提交一个字符串。
回答by Shelly
in your .aspx file add
在您的 .aspx 文件中添加
<input type="text" required autofocus placeholder="Email Address"
class="txt-input txt-input-username" ID="myTextBox" runat="server"/>
in your Code Behind .cs
在 .cs 背后的代码中
myTextBox.Attributes["type"] = "email";
This Worked For Me
这对我有用
回答by ja928
You need to create your own custom control and override the Render routines. Feel free to use either the source code or DLLs
您需要创建自己的自定义控件并覆盖渲染例程。随意使用源代码或 DLL