Two types of postback events

强颜欢笑 提交于 2020-01-23 03:15:05

问题



1) I found two articles, each categorizing a bit differently the two types of postback events:

One resource says the two types of postback events are Changed event ( where controls implement IPostbackDataHandler ), which fires when data is changed between postbacks, and then are Raised events ( where controls implement IPostbackEventHandler ), which are raised by server controls for whatever reason the control sees fit

Other article says the two types are Immediate response events and Change events. According to this article, Immediate response events are ones that actually trigger a postback


a) Which categorization is correct?

b) If second article was correct, then if TextBox control had AutoPostBack=”true”, shouldn’t then TextChanged also be considered Immediate response event?


2) When page is submitted back to the server due to some user action, then at Event handling stage, ASP.NET raises events of all controls that got their data changed since the last postback. The event that actually triggered a postback ( such as Click event ) is raised last


a) But what if user selecting a row in GridView caused a postback? When GridView causes a postback due to Row selection, then unlike simpler controls ( like TextBox or Button ), that postback causes GridView to fire not one, but several server-side events during Event handling stage ( SelectedIndexChaning and SelectedIndexChanged).

Which of these GridView’s events is considered by ASP.NET to be the one that caused a postback?


thank you


回答1:


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



回答2:


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.




回答3:


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



来源:https://stackoverflow.com/questions/918313/two-types-of-postback-events

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