1

I have a simple CalendarExtender (from AjaxControlToolkit) attached to a textbox.

<asp:TextBox ID="StartDateText" runat="server" MaxLength="10" Width="70px" AutoPostBack="True" OnTextChanged="StartDateText_TextChanged" />
<asp:ImageButton ID="ImageCalendarStartDate" runat="server" ImageUrl="~/images/Calendar_scheduleHS.png" AlternateText="Click to show calendar" />
<asp:CalendarExtender ID="StartDateCalendarExtender" runat="server" TargetControlID="StartDateText" PopupButtonID="ImageCalendarStartDate" />

In order to control user input, I have the AutoPostBack set to True on the textbox, as well as a function on the TextChanged event (although TextChanged isn't the issue here).

In Page_Load, I have:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        StartDateCalendarExtender.SelectedDate = DateTime.Now.AddDays(-1);
    }
}

On opening the page, Page_Load sets the date, but the AutoPostBack triggers a postback right after Page_Load, calling it again with IsPostBack set to true.

Is there a server-side way to prevent this postback?

I tried setting the AutoPostBack property to false, changing the SelectedDate, and setting it back to true, but it keeps firing a postback.

MPelletier
  • 16,256
  • 15
  • 86
  • 137

1 Answers1

1

The reason is that because you give the date on the extender, then the extender add it to the text box, then the text box trigger the post back.

How about try to set the text at the TextBox at the first place.

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        // remove that
        // StartDateCalendarExtender.SelectedDate = DateTime.Now;
        // and direct set it to the text box.
        StartDateText.Text = DateTime.Now;
    }
}

Maybe you need to format the DateTime the way you want it.

Aristos
  • 66,005
  • 16
  • 114
  • 150
  • Very astute, sir. However, my CalendarExtender then no longer has the date, and when I open it, some other date is selected (CalendarExtender actually defaults to `DateTime.Now`. Suppose I want to set it to `DateTime.Now.AddDays(-1)`. – MPelletier Jun 05 '13 at 20:58
  • @MPelletier To me is working, the control is read and show the date time from the text box... – Aristos Jun 05 '13 at 21:01
  • Yes, you are correct. I did something else incorrectly. Thanks! – MPelletier Jun 05 '13 at 21:08
  • Drats, that leaves `CalendarExtender.SelectedDate` empty, unset. – MPelletier Jun 06 '13 at 18:19
  • Ah! `SelectedDate` is a bit of a misnommer. You gotta use the textbox only. – MPelletier Jun 06 '13 at 20:56