【问题标题】:Ajax CalendarExtender, How to get the date after i click on a button?Ajax CalendarExtender,单击按钮后如何获取日期?
【发布时间】:2011-11-22 21:34:41
【问题描述】:

我想在我的页面中添加一个 Ajax CalendarExtender。然后在选择日期并单击按钮后,我会在标签中获得选定的日期。

我有一个文本框,它是 CalendarExtender 的目标

<asp:TextBox ID="DateText" runat="server" ReadOnly="true" ></asp:TextBox>

<ajaxToolkit:CalendarExtender 
    ID="Calendar1"
    runat="server"
    TargetControlID="DateText"
    Format="MMMM d, yyyy"
    PopupPosition="Right"
    />

<asp:Button runat="server" ID="Button1" onclick="Button1_Click" />

代码背后

在第一页加载时,我将日期设置为今天。

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        Calendar1.SelectedDate = DateTime.Today;
    }
}

在 Button1_Click 事件中

protected void Button1_Click(object sender, EventArgs e)
{
    Label1.Text = Calendar1.SelectedDate.Value.Day.ToString();
}

问题是当我单击按钮时(或在任何回帖后),选择的值会重置为今天的日期。

如果我没有在Page_Load 中将它设置为DateTime.Today,它会被重置为空并抛出空异常。

我该如何解决这个问题?

非常感谢您的帮助。 我希望我很清楚

【问题讨论】:

    标签: asp.net ajax ajaxcontroltoolkit calendarextender


    【解决方案1】:

    该问题的解决方案是使用 Request.Form 集合。由于此集合具有回发到服务器的所有字段的值,并且还具有使用 JavaScript 等客户端脚本设置的值。

    因此,我们需要对获取值服务器端的方式进行一些小改动。

    C#

    protected void Submit(object sender, EventArgs e)
    {
       string date = Request.Form[txtDate.UniqueID];
    } 
    

    【讨论】:

      【解决方案2】:

      也许这会奏效:

      protected void Page_Load(object sender, EventArgs e)
      {
          if (!IsPostBack)
          {
              if(!Calendar1.SelectedDate)
                  Calendar1.SelectedDate = DateTime.Today;
          }
      }
      

      【讨论】:

      • 感谢您的回复,但这不起作用。 SelectedDate 甚至在进入 Page_Load 事件之前就重置为今天的日期
      猜你喜欢
      • 2021-10-12
      • 2016-03-03
      • 2021-12-08
      • 1970-01-01
      • 1970-01-01
      • 2021-09-05
      • 2021-10-12
      • 2011-09-10
      相关资源
      最近更新 更多