ASP.NET AJAX Toolkit - CalendarExtender is reset o

2019-06-16 01:09发布

I have an ASP.NET page that has two input elements:

  1. A TextBox that is ReadOnly. This TextBox is the TargetControl of a CalendarExtender
  2. A DropDownList with AutoPostBack=true

Here is the code:

<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:&nbsp;</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>

When the DropDownList posts back, the date selected by the user with the datePicker is reset to the current date. In addition, if I look at the Text property of dateTextBox, it is equal to string.Empty.

How do I preserve the date that the user selected on a PostBack?

9条回答
SAY GOODBYE
2楼-- · 2019-06-16 01:43

Certainly you must do as others have already suggested: set readonly field dynamically rather than in markup, and make sure you are not accidentally resetting the value in Page_Load() during postbacks...

...but you must also do the following inside Page_Load(), because the CalendarExtender object has an internal copy of the date that must be forcibly changed:

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]);
        }
    }
}
查看更多
\"骚年 ilove
3楼-- · 2019-06-16 01:45
protected void Page_Load(object sender, EventArgs e)
{
    txt_sdate.Text = Request[txt_sdate.UniqueID];
}
查看更多
手持菜刀,她持情操
4楼-- · 2019-06-16 01:51

I suspect your datePicker_Load is setting something and not checking if it's in a postback. That would make it happen every time and look like it was 'resetting'.

查看更多
登录 后发表回答