6

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

3 Answers 3

7

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

Sign up to request clarification or add additional context in comments.

1 Comment

Date format "yyyy-MM-dd" is the answer key here, This worked for me.
1

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");
}

2 Comments

Yes, yes. I put that in a event handler/ method. I thought this is obvious so I did not wrap that around the code Page_Load in the question. But with even/ function wrapper - it does not work.
I added the wrapper now in the question for clarity. No success yet.
1

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;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.