Ismail
Ismail

Reputation: 151

How to show the Current date in textBox using Asp.net

Can any one tell me how can I print the current data in this format (dd-mm-yyyy) ?

I am using this command, but it is not working:

protected void TextBoxStartDate_TextChanged(object sender, EventArgs e)
        {
              TextBoxStartDate.Text = DateTime.Now.ToString();
           // TextBoxStartDate.Text = DateTime.Now();
        }

I want autofill textbox with a current date

Upvotes: 0

Views: 38072

Answers (4)

HEEN
HEEN

Reputation: 4727

You can write it on Page_Load event to get the Current Date Time

The Code will be:-

You must also have namespace included in your code,

aspx and cs:-

<asp:TextBox ID="TextBoxStartDate" runat="server" OnTextChanged="TextBoxStartDate_TextChanged" Width="150px" Height="16px" ></asp:TextBox>

protected void Page_Load(object sender, EventArgs e) 
 {
     TextBoxStartDate.Text = DateTime.Now.ToString();
 }

Also as per your code, you need to add onTextChanged event in your code-behind

protected void TextBoxStartDate_TextChanged(object sender, EventArgs e)
{
    //do something here
}

UPDATE

You will get something like below in your textbox when you load your page

Date Textbox

Upvotes: 0

rjps12
rjps12

Reputation: 605

Put this TextBoxStartDate.Text = DateTime.Now.ToString(); on PageLoad

protected void Page_Load(object sender, System.EventArgs e)
{
   TextBoxStartDate.Text = DateTime.Now.ToString();
}

here is my ASP.cs code:

<asp:TextBox ID="TextBoxStartDate" runat="server" ontextchanged="TextBoxStartDate_TextChanged" Width="150px" Height="16px"  ></asp:TextBox>

Upvotes: 1

Rajeesh Menoth
Rajeesh Menoth

Reputation: 1750

how can i print the current data in this format (dd-mm-yyyy)

Lower mm means minutes..!!

You can use this format:

DateTime.Now.ToString("dd-MM-yyyy");

Change your code like this:

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

Upvotes: 6

Vijay Kumbhoje
Vijay Kumbhoje

Reputation: 1441

You can do it on you page Load event.

   protected void Page_Load(object sender, EventArgs e)
        {
            TextBox1.Text = System.DateTime.Now.ToShortDateString();
        }

Upvotes: 1

Related Questions