1

我想在我的页面中添加一个 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();
}

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

如果我没有将它设置为它,DateTime.TodayPage_Load会被重置为 null 并抛出一个 null 异常。

我该如何解决这个问题?

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

4

2 回答 2

0

也许这会起作用:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        if(!Calendar1.SelectedDate)
            Calendar1.SelectedDate = DateTime.Today;
    }
}
于 2011-09-22T18:00:44.213 回答
0

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

因此,我们需要在获取价值服务器端的方式上做一个小的改变。

C#

protected void Submit(object sender, EventArgs e)
{
   string date = Request.Form[txtDate.UniqueID];
} 
于 2011-11-17T01:15:01.337 回答