I have a simple CalendarExtender (from AjaxControlToolkit) attached to a textbox.
我有一个简单的CalendarExtender(来自AjaxControlToolkit)附加到文本框。
<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).
为了控制用户输入,我在文本框上将AutoPostBack设置为True,以及TextChanged事件上的函数(尽管TextChanged不是此处的问题)。
In Page_Load
, I have:
在Page_Load中,我有:
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.
在打开页面时,Page_Load设置日期,但AutoPostBack在Page_Load之后立即触发回发,并在IsPostBack设置为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.
我尝试将AutoPostBack属性设置为false,更改SelectedDate,并将其设置回true,但它会继续触发回发。
1 个解决方案
#1
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.
如何尝试在TextBox首先设置文本。
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.
也许你需要按照你想要的方式格式化DateTime。
#1
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.
如何尝试在TextBox首先设置文本。
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.
也许你需要按照你想要的方式格式化DateTime。