Вопрос

ASP.net 2.0, Visual Studio 2005, and standard controls such as <asp:DropDownList>, etc require that I put in AutoPostBack="true" for there to be a call to the code behind. But why? Shouldn't that be happening anyway? Thanks

Это было полезно?

Решение

Code behind runs on the server. HTML code and Javascript run on the browser.

A dropdownlist is an HTML element that runs on the browser. It can't do anything on the server without sending data back to the server. The means by which ASPX web forms send data to the server is via postback.

Sometime you don't want a dropdownlist to send data to the server. It really slows down the user experience to have to wait for something to cross the wire. To speed things up, you can disable the postback on the list control; the server will only get contacted when the user posts the whole form. At that point, the server can inspect the list control to see if its value has changed and take action.

Другие советы

No, this should not be happening anyway.

An asp:DropDownList is a control that generates a single drop-down list. This is rendered as a select tag to the client. A select tag is typically there to collect input from a user, not to submit a form.

Forcing AutoPostBack="true" is necessary because then you are causing a post-back whenever the selected index changes. While this can be useful, this is not the expected behavior of a select tag, and most developers are not going to want this action... for performance reasons... or if it is part of a larger form, it will interrupt user flow... etc.

The alternative would be they only call javascript (on the client) as opposed to making a round trip to the server (postback).

Javascript is faster since it is on the client and doesn't require a round trip to the server, but it can be disabled or changed by the user.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top