簡體   English   中英

如何使用Asp.net在文本框中顯示當前日期

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

誰能告訴我如何以這種格式(dd-mm-yyyy)打印current data

我正在使用此命令,但不起作用:

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

我想用current date autofill文本框

我如何以這種格式(dd-mm-yyyy)打印當前數據

較低的毫米表示分鍾.. !!

您可以使用以下格式:

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

像這樣更改代碼:

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

把這個TextBoxStartDate.Text = DateTime.Now.ToString(); PageLoad

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

這是我的ASP.cs代碼:

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

您可以在頁面上加載事件。

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

您可以將其寫在Page_Load事件上以獲取當前日期時間

該守則將是:

您還必須在代碼中包含名稱空間,

aspx和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();
 }

同樣根據您的代碼,您需要在代碼隱藏中添加onTextChanged事件

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

UPDATE

加載頁面時,您會在文本框中看到類似以下的內容

日期文本框

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM