Html 如何强制 ASP:TextBox 为电子邮件类型?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16914226/
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 do I force ASP:TextBox to be of type email?
提问by RealSollyM
I have an ASP.NET 3.5
application and I'd like to add the type="email"
according to HTML5
standards but if I add the type on the ASP:TextBox
control I get the below output:
我有一个ASP.NET 3.5
应用程序,我想type="email"
根据HTML5
标准添加,但是如果我在ASP:TextBox
控件上添加类型,我会得到以下输出:
<input name="ctl00$content$txtEmailAddress" type="text" type="email" value=""
id="ctl00_content_txtEmailAddress" class="input-block-level" required="required"/>
How can I replace the type attribute from "text" to "email"?
如何将类型属性从“文本”替换为“电子邮件”?
回答by Sachin
If you using .Net 3.5 then no matter what type you have specified with <asp:TextBox>
, it will always render with <input type="text" />
. So in that case you can use simple html input with type="email"
如果您使用 .Net 3.5,那么无论您<asp:TextBox>
使用<input type="text" />
. 因此,在这种情况下,您可以使用 type="email" 的简单 html 输入
<input type="email" />
For 4.5 version you can use TextMode="Email"
对于 4.5 版本,您可以使用 TextMode="Email"
<asp:TextBox ID="txtmyBox" runat="server" TextMode="Email"></asp:TextBox>
回答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.
抱歉,我参加聚会有点晚了,但我认为其他人可以从我所做的事情中受益。我有一个 HTML 5 页面,但我们仍然有 .NET 3.5。我们希望保留 .NET 元素,但将类型更改为电子邮件。
I've tried several methods, 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...)
我已经尝试了几种方法,但对我有用的方法如下:我向元素本身内联添加了一个 JavaScript 属性(当我将它放在脚本标记中时,由于某种原因它不会被选中......)
Here is what your tag would look like if you use my changes:
如果您使用我的更改,您的标签将如下所示:
<asp:TextBox runat="server" type="email" onfocus="this.type='email'"/>
回答by JimiSweden
My solution (not updating to .Net 4.5) ended up like this with an inline script, the element ID is visible if you inspect the rendered html input element.
我的解决方案(不更新到 .Net 4.5)以一个内联脚本结束,如果您检查呈现的 html 输入元素,则元素 ID 是可见的。
<label class="input text">
<asp:TextBox runat="server" CssClass="text" ID="EmailTextBox"></asp:TextBox>
<script>
document.getElementById("ctl00_ContentPlaceHolder_EmailTextBox").type = "email";
</script>
</label>
回答by Vishnu Vikraman
You can use like this..
你可以这样使用..
<asp:TextBox runat="server" type="email" />
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" />
You can refer this link http://support.microsoft.com/kb/2468871and go to feature 3.
您可以参考此链接http://support.microsoft.com/kb/2468871并转到功能 3。
回答by faester
In older versions of .NET you could extend the TextBox
class with the possibility to set the input type:
在旧版本的 .NET 中,您可以扩展TextBox
类并设置输入类型:
A completely basic - but working - solution could be the one shown below, which also adds a placeholder to the textbox.
一个完全基本但有效的解决方案可能是如下所示的解决方案,它还向文本框添加了一个占位符。
namespace Ux.Example{
public class TextBox : System.Web.UI.WebControls.TextBox
{
public string PlaceHolder { get; set; }
public string InputType { get; set; }
protected override void OnInit(EventArgs e)
{
if (!string.IsNullOrWhiteSpace(PlaceHolder))
Attributes.Add("placeholder", PlaceHolder);
if (!string.IsNullOrWhiteSpace(InputType))
{
Attributes.Add("type", InputType);
}
base.OnLoad(e);
}
}
}
You would also have to include the file in the aspx file using @Register
:
您还必须使用@Register
以下命令将该文件包含在 aspx 文件中:
<%@ Register TagPrefix="ux" Namespace="Ux.Example" Assembly="CHANGE_TO_CORRECT_NAME" %>
And the actual usage of the control would then be:
然后控件的实际使用将是:
<ux:TextBox ID="Email" Visible="false" runat="server" PlaceHolder="enter e-mail address" InputType="email" />
I have not tested what happens when the TextMode
attribute is in use. It may cause some havoc.
我尚未测试TextMode
使用该属性时会发生什么。它可能会造成一些破坏。
回答by mohsen solhnia
if you want to show a preview of email list depend of your characters that you are typing and your history of email names in browser, you should set name of element ID just"email". but all method mostly work on chrome:
如果您想根据您输入的字符和浏览器中电子邮件名称的历史来显示电子邮件列表的预览,您应该将元素 ID 的名称设置为“电子邮件”。但所有方法主要适用于 chrome:
<asp:TextBox ID="email" runat="server" CssClass="YourEmailCss"></asp:TextBox>
or, if you use HTML standard elements:
或者,如果您使用 HTML 标准元素:
<input id="email" type="email" name="email" />