如何在服务器端.NET中使用HTML5电子邮件输入类型

时间:2022-03-06 15:34:47

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.

据我了解,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?

我想在项目中使用它,但我的输入字段是 控件。我如何使用HTML5元素,但仍像其他字段一样访问其数据服务器端? :textbox>

6 个解决方案

#1


24  

There is an update for .NET framework 4 which allows you to specify the type attribute

.NET framework 4有一个更新,允许您指定type属性

http://support.microsoft.com/kb/2468871.

http://support.microsoft.com/kb/2468871。

See feature 3 way down the page

请参阅页面下方的功能3

Feature 3

特色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:

新语法允许您定义与HTML5兼容的TextBox控件。例如,以下代码定义了与HTML5兼容的TextBox控件:

<asp:TextBox runat="server" type="some-HTML5-type" />

#2


16  

you can try adding the attributes manually, like:

您可以尝试手动添加属性,例如:

TextBox1.Attributes["type"] = "email"; 
TextBox1.Attributes["type"] = "url"; 
TextBox1.Attributes["type"] = "number"; 

#3


4  

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'"/>

Eli

#4


2  

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集合并检索该值。无论浏览器对标签做什么,它都必须向服务器提交一个字符串。

#5


2  

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

这为我工作

#6


0  

You need to create your own custom control and override the Render routines. Feel free to use either the source code or DLLs

您需要创建自己的自定义控件并覆盖Render例程。随意使用源代码或DLL

#1


24  

There is an update for .NET framework 4 which allows you to specify the type attribute

.NET framework 4有一个更新,允许您指定type属性

http://support.microsoft.com/kb/2468871.

http://support.microsoft.com/kb/2468871。

See feature 3 way down the page

请参阅页面下方的功能3

Feature 3

特色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:

新语法允许您定义与HTML5兼容的TextBox控件。例如,以下代码定义了与HTML5兼容的TextBox控件:

<asp:TextBox runat="server" type="some-HTML5-type" />

#2


16  

you can try adding the attributes manually, like:

您可以尝试手动添加属性,例如:

TextBox1.Attributes["type"] = "email"; 
TextBox1.Attributes["type"] = "url"; 
TextBox1.Attributes["type"] = "number"; 

#3


4  

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'"/>

Eli

#4


2  

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集合并检索该值。无论浏览器对标签做什么,它都必须向服务器提交一个字符串。

#5


2  

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

这为我工作

#6


0  

You need to create your own custom control and override the Render routines. Feel free to use either the source code or DLLs

您需要创建自己的自定义控件并覆盖Render例程。随意使用源代码或DLL