在服务器端属性更改prevent回传 [英] Prevent postback on server-side property change

查看:93
本文介绍了在服务器端属性更改prevent回传的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单CalendarExtender(从AjaxControlToolkit)连接到一个文本框。

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" />

为了控制用户的输入,我有的AutoPostBack 设置为的文本框,以及在框TextChanged 事件的函数(虽然框TextChanged 不是这里的问题)。

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).

的Page_Load ,我有:

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

在打开的页面,的Page_Load 设置日期,但将AutoPostBack后立即的Page_Load 触发回传,称与的IsPostBack 再次设置为true。

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.

是否有一个服务器端的方式来prevent这回发?

我试过的AutoPostBack 属性设置为false,改变了 SelectedDate ,并设置回真实的,但它不断发射回传。

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

推荐答案

的原因是因为你给的延长日期,然后扩展它添加到文本框,然后在文本框中触发回发。

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;
    }
}

也许你需要格式化的DateTime你想要的方式。

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

这篇关于在服务器端属性更改prevent回传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆