2015-12-10 96 views
-2

您好親愛的所有人都希望你們都很好,做得很好,我是.net開發新手,我正在創建一個項目,該項目將獲得有關用戶日常基礎的信息,但問題在於我想用自動填寫當前日期的日期文本框, 這是我背後的Page_Load如何使用當前日期自動填充文本框

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Data; 
using System.Data.SqlClient; 
using System.Drawing; 

namespace StackOver 
{ 
public partial class _Default : System.Web.UI.Page 
{ 
    protected void Page_Load(object sender,EventArgs e) 
    { 
    TextBoxStartDate.Text = DateTime.Now.ToString(); 

    if (!Page.IsPostBack) 
    { 
    LoadOptionsCardCodeTable(); 
    LoadOptionsStageSlpNameTable(); 
    } 
    } 

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

代碼,同時也這是我的aspx.cs該文本框代碼

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

在此先感謝它不顯示錯誤但當前日期不顯示 好心幫

+2

上面現有的代碼的結果是什麼? –

+0

空文本框表示它不填充TextBoxStartDate中的當前日期@LeiYang – Ismail

+0

嘗試 - > this.TextBoxStartDate.Text = DateTime.Now.ToString(「yyyy-MM-dd」); 可能的重複 http://stackoverflow.com/questions/21952542/how-to-set-value-from-server-side-code-to-an-asp-net-textbox-with-textmode-date – Novice

回答

0

添加的AutoPostBack = 「真」在.aspx的文本框中。

下面的代碼工作正常:

代碼背後:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 

public partial class _Default : System.Web.UI.Page 
{ 
protected void Page_Load(object sender, EventArgs e) 
{ 
    TextBoxStartDate.Text = DateTime.Now.ToString(); 

    if (!Page.IsPostBack) 
    { 


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

.aspx的:

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

輸出:

enter image description here

0

你必須給AutoPostBack="true"在你的文本定義inoder觸發textchange事件

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

這是可能的,就可能改寫或客戶端清除輸入框。你有沒有在客戶端編寫任何代碼(使用JavaScript)來填寫或清除負載上的輸入框?

0

如果你不想改變這個文本框的價值可言,你可以做的下一件事:如果你使用的是.NET> = 4.5

客戶端(不ontextchanged

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

其他

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

,並在服務器端:

,如果你使用的是.NET> = 4.5

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

其他

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

如果你想控制文本框的輸入,然後服務器端事件是相當昂貴的一步。在這種情況下,js是更好的方法

0

使用此代碼來解決您的問題其簡單的使用系統;

using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 

public partial class demo : System.Web.UI.Page 
{ 
    protected void Page_Load(object sender, EventArgs e) 
    { 
     currentDateTime_textbox.Text = DateTime.Now.ToString(); 
    } 
} 

客戶端:

<asp:TextBox ID="currentDateTime_textbox" runat="server"></asp:TextBox> 
+0

他給出的代碼是Text Changed。 – RajeeshMenoth

相關問題