Ajax CalendarExtender,如何在单击按钮后获取日期?
我想添加一个 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
,它将重置为 null 并抛出 null 异常。
我该如何解决这个问题?
非常感谢您的帮助。 我希望我说清楚了
I want to add add an Ajax CalendarExtender to my page. And then after selecting a date and clicking on a button I get the selected Day in a label.
I have a text box which is the target of the 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" />
In the Code Behind:
On the first page load I set the date to Today.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Calendar1.SelectedDate = DateTime.Today;
}
}
In the Button1_Click Event
protected void Button1_Click(object sender, EventArgs e)
{
Label1.Text = Calendar1.SelectedDate.Value.Day.ToString();
}
The problem is when I click the button (or after any post backs) the value selected get reset to Today's date.
And if I don't set it to DateTime.Today
in the Page_Load
It get reset to null and throw a null exception.
How can I solve this?
Thank you very much for any help.
I hope i was clear
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
也许这会起作用:
Maybe this will work:
该问题的解决方案是使用 Request.Form 集合。由于此集合具有回发到服务器的所有字段的值,并且还具有使用客户端脚本(如 JavaScript)设置的值。
因此,我们需要对服务器端获取值的方式进行一些小的改变。
C#
The solution to the issue is making use of Request.Form collections. As this collection has values of all fields that are posted back to the server and also it has the values that are set using client side scripts like JavaScript.
Thus we need to do a small change in the way we are fetching the value server side.
C#