Question

Before I begin, I have already seen this question about a very similar topic (as well as this one and this one), none of which answer my question completely. I already understand the concepts presented in these questions/answers, but I have more questions.

A) What happens if you have multiple controls with AutoPostBack="false" and you change a number of them before a postback? Take the following brief example (assume that everything else needed for the page is written correctly and trivially; e.g., Page_Load):

Default.aspx:

<asp:DropDownList ID="ddlFoo" runat="server" 
    OnSelectedIndexChanged="ddlFoo_Changed" AutoPostBack="false" >
    <asp:ListItem Text="a" />
    <asp:ListItem Text="b" />
    <asp:ListItem Text="c" />
</asp:DropDownList>
<asp:DropDownList ID="ddlBar" runat="server" 
    OnSelectedIndexChanged="ddlBar_Changed" AutoPostBack="false" >
    <asp:ListItem Text="1" />
    <asp:ListItem Text="2" />
    <asp:ListItem Text="3" />
</asp:DropDownList>
<asp:Button ID="btnQux" runat="sever" Text="Click for PostBack" OnClick="btnQux_Click"

Default.aspx.cs:

protected void ddlFoo_Changed(object sender, EventArgs e)
{
    Response.Write("ddlFoo changed to " + ddlFoo.Text + ". ");
}
protected void ddlBar_Changed(object sender, EventArgs e)
{
    Response.Write("ddlBar changed to " + ddlBar.Text + ". ");
}
protected void btnQux_Changed(object sender, EventArgs e) { }

Now, say you change ddlFoo to 3 and then ddlBar to b. Then, you click btnQux. You get the following output from Response.Write after clicking:

ddlBar changed to b. ddlFoo changed to 3. 

Why does this happen? Do the OnSelectedIndexChanged methods get put into a stack to be called once a postback happens?

B) Why does my webpage load much more quickly when I use this approach and set AutoPostBack="false" for most of my controls? To be specific, I did this for a CheckBox, a DropDownList, and a TextBox in a GridView, which retrieved ~1200 rows and 27 columns of data and took 10s in VS2008 debug mode versus 310s before. Why would the load/refresh time be so much faster?

EDIT: I released the code earlier this afternoon, and there was no significant difference between the load time of the old (AutoPostBack="true") and new (AutoPostBack="false") versions. I think that perhaps the debugger was doing something extra, which caused the large jump in load time. A better way to rephrase question B) might be this then: What could the debugger have been doing to cause this large jump in load time?

Was it helpful?

Solution

Warning: I'm no ASP.NET expert... If this turns out to be garbage, I'll delete it :)

A) I believe you will see the new values of all the controls, whenever the postback ends up happening, including all the change events, just as you described. The values have changed, after all - the AutoPostBack just affects the timing (and whether the postback occurs at all, of course).

B) There's more Javascript in the HTML delivered with AutoPostBack = True on all the controls, but not enough to make that enormous difference. As noted in your edit, it looks like that was a transient issue anyway - we can't really explain transient issues without more diagnostics.

OTHER TIPS

You can use Fiddler to see what data is moving between client and server.

A. With fiddler you can easily see what data is sent to the server.

For example:

If you have DropDownList ddlFoo, when you click on button, you actually post this information:

POST http:// [server]:[port]/[resource.aspx] HTTP/1.1 Host: [server]:[port] [Headers...]

_VIEWSTATE[viewstate data stored in html as hidden field value]&_EVENTVALIDATION=[event validaion data]&ddlFoo=selecteItem&button1=ButtonText

When ASP.NET receives request, it compares ddlFoo's value and invokes it's event.

B. When you set AutoPostBack to true, then this javascript function will be generated:

function __doPostBack(eventTarget, eventArgument) {
    if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
        theForm.__EVENTTARGET.value = eventTarget;
        theForm.__EVENTARGUMENT.value = eventArgument;
        theForm.submit();
    }
}

And onchange attribute will be added to ddlFoo. So whenever you change DropdownList item, onchange event will be fired and __doPostBack function will be called, which will auto post back to the server.

Both answers you have gotten so far are correct. The simplified version of it is this:

A) When the form is finally POST'ed to the server, the server compares the form's current state with the ViewState and responds accordingly.

B) Enabling AutoPostBack causes javascript to be generated, and this javascript submits the form (which then triggers the postback).

Why does this happen? Do the OnSelectedIndexChanged methods get put into a stack to be called once a postback happens?

Events that do not immediately post back (in your case ddlFoo_Changed and ddlBar_Changed) are cached.

Then those cached/pending events are raised along with btnQux's click event, when a page is posted back (by btnQux's click event).

You can read more here - ASP.NET Server Control Event Model

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top