Html 为 html5 input type="date" 渲染 asp.TextBox

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2768515/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 02:57:32  来源:igfitidea点击:

Render asp.TextBox for html5 input type="date"

htmlasp.netwebformstextbox

提问by Jhonny D. Cano -Leftware-

I don't know if it has been asked before, couldn't find it either.

不知道以前有没有问过,也没找到。

Is it possible to control the type of the input text that is rendered by an asp:TextBox? I would like to change it to <input type="date">

是否可以控制由 asp:TextBox 呈现的输入文本的类型?我想把它改成<input type="date">

any suggestions or comments are welcome, thanks

欢迎任何建议或意见,谢谢

回答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 3way 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 Colin

If you don't mind subclassing, you can do this by overidding AddAttributesToRender

如果你不介意子类化,你可以通过覆盖 AddAttributesToRender 来做到这一点

public class DateTextbox : System.Web.UI.WebControls.TextBox
{
    protected override void AddAttributesToRender(System.Web.UI.HtmlTextWriter writer)
    {
        writer.AddAttribute("type", "date");
        base.AddAttributesToRender(writer);
    }
}

回答by Freaky JellyFish

Here is how I did it... hope it helps...

这是我是怎么做的...希望它有帮助...

Add a new item to your project of the type "JScript File", then paste this code in:

向“JScript 文件”类型的项目添加一个新项目,然后将此代码粘贴到:

var setNewType;
if (!setNewType) {
setNewType = window.onload = function() {
    var a = document.getElementsByTagName('input');
    for (var i = 0; i < a.length; i++) {
        if (a[i].getAttribute('xtype')) {
            a[i].setAttribute('type', a[i].getAttribute('xtype'));
            a[i].removeAttribute('xtype');
        };
    }
}

Now add this line into your aspx page after the bodytag (change the file name to whatever you called it above!):
<script type="text/javascript" src="setNewType.js"></script>

现在将此行添加到您的 aspx 页面中的body标记之后(将文件名更改为您在上面调用的任何名称!):
<script type="text/javascript" src="setNewType.js"></script>

Finally, add something like the following to your code behind PageLoad ( I used VB here):

最后,在 PageLoad 后面的代码中添加如下内容(我在这里使用了 VB):

aspTxtBxId.Attributes("xtype") = "tel" ' or whatever you want it to be

The important part above is the Attributes.("xtype"), as it places the attribute XTYPE in the rendered html for the "textbox", which the javascript then finds and uses to replace the original "type" attribute.

上面的重要部分是Attributes.("xtype"),因为它将属性 XTYPE 放置在为“文本框”呈现的 html 中,然后 javascript 找到并使用它来替换原始的“类型”属性。

Good Luck!
FJF

祝你好运!
FJF

回答by maxandcoffee

I know this question is old, but I was having the same issue in a Web Forms application. You need to use TextMode

我知道这个问题很老,但我在 Web 窗体应用程序中遇到了同样的问题。您需要使用 TextMode

While the documentation states that

虽然文档指出

Use the TextMode property to specify how a TextBox control is displayed. Three common options are single-line, multiline, or password text box.

使用 TextMode 属性指定 TextBox 控件的显示方式。三个常用选项是单行、多行或密码文本框。

You can also use html5, date, time, number, etc built in Visual Studio 2012/2013.

您还可以使用 Visual Studio 2012/2013 中内置的 html5、日期、时间、数字等。

https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.textbox.textmode(v=vs.110).aspx

https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.textbox.textmode(v=vs.110).aspx

回答by ja928

I went the route of building my own set of html5 inputsby building custom controls. I get the custom keyboards on iPad and iPhone plus the postback coding of true asp.net controls. It worked for my inhouse project, so I decided to license the whole suite to save other people the time and trouble of doing it from scratch.

我通过构建自定义控件来构建自己的一组html5 输入。我得到了 iPad 和 iPhone 上的自定义键盘以及真正的 asp.net 控件的回发编码。它适用于我的内部项目,所以我决定授权整个套件,以节省其他人从头开始做的时间和麻烦。

Hope this helps!

希望这可以帮助!

回答by BALKANGraph

Actually there is no easy way to override the type attribute in standart asp:TextBox. You can simly use an input element

实际上没有简单的方法可以覆盖标准 asp:TextBox 中的 type 属性。您可以简单地使用输入元素

Here is an example

这是一个例子

<input type="date" id="Input1" runat="server" />

Let me know if it helps...

让我知道它是否有帮助...