Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

any suggestions or comments are welcome, thanks

like image 499
Jhonny D. Cano -Leftware- Avatar asked May 04 '10 19:05

Jhonny D. Cano -Leftware-


People also ask

How can use date TextBox in asp net?

A BoundField of a GridView always uses a plain TextBox. To use a HTML 5 date input type we need to add a TemplateField in the GridView. We need to modify the EditItemTemplate of the TemplateField with the following markup: <asp:TemplateField HeaderText="Date of Birth">


2 Answers

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

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

See feature 3 way down the page

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" />
like image 112
Chris Diver Avatar answered Sep 22 '22 18:09

Chris Diver


If you don't mind subclassing, you can do this by overidding 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);
    }
}
like image 34
Colin Avatar answered Sep 20 '22 18:09

Colin