Two types of postback events

被刻印的时光 ゝ 提交于 2019-12-04 15:32:41

The SelectedIndexChanged event (on a listbox) and the TextChanged event (on a Textbox) will fire on a postback even if the Autopostback property is false.

Try putting this on a form:

   <asp:ListBox runat="server" ID="test" 
        onselectedindexchanged="test_SelectedIndexChanged">
<asp:ListItem >number1</asp:ListItem>
 <asp:ListItem >number2</asp:ListItem>     
</asp:ListBox>

<asp:TextBox runat="server" ID="text" ontextchanged="text_TextChanged" />
<asp:Button runat="server" Text="Click Me" />

and this in the code behind:

 protected void test_SelectedIndexChanged(object sender, EventArgs e)
    {
        Response.Write("SelectedIndexChanged");
    }

    protected void text_TextChanged(object sender, EventArgs e)
    {
        Response.Write("TextChanged");
    }

Then run it, change the selected item, type some text, and hit the button - both events fire. You can see the sequence of events using Reflector. For the Textbox, the RaisePostDataChangedEvent method is:

 protected virtual void RaisePostDataChangedEvent()
{
    if (this.AutoPostBack && !this.Page.IsPostBackEventControlRegistered)
    {
        this.Page.AutoPostBackControl = this;
        if (this.CausesValidation)
        {
            this.Page.Validate(this.ValidationGroup);
        }
    }
    this.OnTextChanged(EventArgs.Empty);
}

SelectedIndexChanging is called first and is typically where I make any sort of updates. SelectedIndexChanged happens AFTER SelectedIndexChanging, so technically the page sees the SelectedIndexChanging as the event that caused the postback. Remember that a postback also calls the entire lifecycle of the page from Init to Page_Load to your event.

the autopostback property just lets the site know to do a postback after whatever change occurs, be it textchanged, selectedindexchanged, checkedchanged, etc. if the autopostback property is not set to true, these events will not occur.

First, it sounds to me like both categorizations are compatible, just a different way of putting it. I suppose you could say that Raised events are equivalent to Immediate Response events, which are only possible in controls that implement IPostbackEventHandler.

So regarding 1b, when AutoPostBack = true, I believe it just sets up a javascript onchange event to make a postback call. Once the postback fires, the page detects that the textbox has changed and runs the changed event. Thus this still isn't a Raised Event. (I could be wrong, but that's my understanding.)

A Raise Event would be one triggered in a control's RaisePostBack method (required by IPostBackEventHandler)--which happens after the page load cycle and triggering each change event.

Under the hood, a postback can occur in one of two ways: by submitting the form (asp:button with submit behavior) or by a javascript _doPostBack call (occurs in controls where AutoPostBack=true or when you make the call directly using Page.ClientScript).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!