我有一个 ASP.NET 页面,它有两个输入元素:
- 只读的文本框。此文本框是 CalendarExtender 的 TargetControl
- AutoPostBack=true 的 DropDownList
这是代码:
<table border="0" cellpadding="0" cellspacing="0">
<tr><td colspan="2">Date:</td></tr>
<tr><td colspan="2">
<asp:TextBox ID="dateTextBox" runat="server" ReadOnly="true" />
<ajax:CalendarExtender ID="datePicker" runat="server" Format="MM/dd/yyyy" OnLoad="datePicker_Load" TargetControlID="dateTextBox" />
</td></tr>
<tr><td colspan="2">Select an Option:</td></tr>
<tr>
<td>Name: </td>
<td><asp:DropDownList ID="optionsDropDownList" runat="server" AutoPostBack="true"
OnLoad="optionsDropDownList_Load"
OnSelectedIndexChanged="optionsDropDownList_SelectedIndexChanged"
DataTextField="Name" DataValueField="ID" />
</td></tr>
<tr><td><asp:Button ID="saveButton" runat="server" Text="Save" OnClick="saveButton_Click" /></td></tr>
</table>
当 DropDownList 回发时,用户使用 datePicker 选择的日期将重置为当前日期。另外,如果我查看 dateTextBox 的 Text 属性,它等于 string.Empty。
如何保留用户在回发中选择的日期?
最佳答案
当然,您必须按照其他人的建议进行操作:动态设置 readonly
字段,而不是在标记中,并确保您不会意外重置 Page_Load()
中的值在回发期间...
...但您还必须在 Page_Load()
内执行以下操作,因为 CalendarExtender
对象具有必须强制更改的日期的内部副本:
if (IsPostBack) // do this ONLY during postbacks
{
if (Request[txtDate.UniqueID] != null)
{
if (Request[txtDate.UniqueID].Length > 0)
{
txtDate.Text = Request[txtDate.UniqueID];
txtDateExtender.SelectedDate = DateTime.Parse(Request[txtDate.UniqueID]);
}
}
}
关于ASP.NET AJAX 工具包 - CalendarExtender 在回发时重置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/957129/