【问题标题】:How to Autofil the TextBox with Current date如何使用当前日期自动填充文本框
【发布时间】:2025-12-19 07:15:12
【问题描述】:

你好,亲爱的,希望你们一切都好,我是 .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>

在此先感谢它没有显示错误,但当前日期没有显示 请帮忙

【问题讨论】:

  • 以上现有代码的结果是什么?
  • empty textBox 表示它没有在 TextBoxStartDate @LeiYang 中填写当前日期
  • 尝试-> this.TextBoxStartDate.Text = DateTime.Now.ToString("yyyy-MM-dd"); *.com/questions/21952542/… 的可能重复项
  • 如何在服务器端获取价值,而不使用 AutoPostBack="true"
  • @rajeeshmenoth : runat="server" 足以在服务器端获得价值

标签: c# asp.net


【解决方案1】:

在 Aspx 文本框中添加 AutoPostBack="true"

以下代码运行良好:

代码隐藏:

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>    

输出:

【讨论】:

    【解决方案2】:

    您必须在文本框定义索引器中提供AutoPostBack="true" 才能触发 textchange 事件

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

    【讨论】:

      【解决方案3】:

      您可能已经覆盖或清除了客户端的输入框。您是否在客户端(使用 JavaScript)编写了任何代码来填充或清除加载时的输入框?

      【讨论】:

        【解决方案4】:

        如果您根本不想更改此 TextBox 的值,则可以执行以下操作:

        客户端(没有ontextchanged):

        如果您使用的是 .NET >= 4.5

        <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 是更好的方式

        【讨论】:

          【解决方案5】:

          使用此代码解决您的问题很简单`using System;

          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>
          

          【讨论】: