2015-12-10 103 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

相关问题