Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set value from server side code to an asp.net TextBox with TextMode="Date"?

Tags:

html

c#

asp.net

I am doing a demo application for learning the use of new HTML5 input types (example- Date, email etc as TextMode) introduced in asp.net textbox control.

In my sample page I want to display server side date field data using asp:TextBox with TextMode="Date".

The asp.net code goes as:

<asp:TextBox ID="txtExpenseDate" TextMode="Date" runat="server"></asp:TextBox>

The C# backend code goes as

protected void Page_Load(object sender, EventArgs e)
{
    txtExpenseDate.Text = DateTime.Now.ToString("MM/dd/yyyy");
}

But while the page loads, the date value does not get displayed in the textbox.

What am I doing wrong?

addendum: Just realized that since this is HTML5, I must mention browser version. I am on latest Google Chrome Version 33.0.1750.117 m. This displays the field as a calendar to pick the date, thus supports the HTML5 equivalant of TextMode="Date" attribute .

Regards,

Sumit

like image 404
dhalsumit Avatar asked Feb 22 '14 09:02

dhalsumit


3 Answers

Try the below code

 protected void Page_Load(object sender, EventArgs e)
        {
            this.txtExpenseDate.Text = DateTime.Now.ToString("yyyy-MM-dd");
        }

http://forums.asp.net/t/1856516.aspx?Problem+with+date+textmode+for+textbox+in+vs2012+net+4+5

like image 112
Sridhar Narasimhan Avatar answered Nov 14 '22 22:11

Sridhar Narasimhan


But while the page loads, the date value does not get displayed in the textbox.

You need to set the value for the TextBox control in Page_Load event of the Webpage

Try This:

protected void Page_Load(object sender, EventArgs e)
{
    txtExpenseDate.Text = DateTime.Now.ToString("MM/dd/yyyy");
}
like image 36
Sudhakar Tillapudi Avatar answered Nov 15 '22 00:11

Sudhakar Tillapudi


Your TextMode set as Date So you need to convert at exact as date format optionally you can set textmode as date. like below.

this.txtExpenseDate.Text = DateTime.Now.ToString("yyyy-MM-dd");
this.txtExpenseDate.TextMode=TextBoxMode.Date;
like image 30
Sajidur Rahman Avatar answered Nov 15 '22 00:11

Sajidur Rahman