Reputation: 151
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
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
Upvotes: 0
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
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
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