net example evento dropdownlist asp c# asp.net drop-down-menu autopostback selectedindexchanged

c# - example - Dropdown OnSelectedIndexChanged no disparando



dropdownlist selectedindexchanged vb net (1)

¿Está vinculando su lista desplegable de datos con cada viaje al servidor o solo a una devolución de datos? Si lo haces todo el tiempo, es posible que el servidor no crea que se haya seleccionado algo, por lo tanto, el evento no se activará.

Supongamos que está enlazando datos con el menú desplegable en el evento Page_Load. Quieres hacerlo así:

protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { // bind drop-down list here } }

El evento OnSelectedIndexChanged no se OnSelectedIndexChanged para mi cuadro desplegable. Todos los foros que he visto me dijeron que añadiera AutoPostBack="true" , pero eso no cambió los resultados.

HTML:

<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:Label ID="Label1" runat="server" Text="Current Time: " /><br /> <asp:Label ID="lblCurrent" runat="server" Text="Label" /><br /><br /> <asp:DropDownList ID="cboSelectedLocation" runat="server" AutoPostBack="true" OnSelectedIndexChanged="cboSelectedLocation_SelectedIndexChanged" /><br /><br /> <asp:Label ID="lblSelectedTime" runat="server" Text="Label" /> </div> </form> </body> </html>

Código detrás:

public partial class _Default : Page { string _sLocation = string.Empty; string _sCurrentLoc = string.Empty; TimeSpan _tsSelectedTime; protected void Page_Load(object sender, EventArgs e) { AddTimeZones(); cboSelectedLocation.Focus(); lblCurrent.Text = "Currently in " + _sCurrentLoc + Environment.NewLine + DateTime.Now; lblSelectedTime.Text = _sLocation + ":" + Environment.NewLine + DateTime.UtcNow.Add(_tsSelectedTime); } //adds all timezone displaynames to combobox //defaults combo location to seoul, South Korea //defaults current location to current location private void AddTimeZones() { foreach(TimeZoneInfo tz in System.TimeZoneInfo.GetSystemTimeZones()) { string s = tz.DisplayName; cboSelectedLocation.Items.Add(s); if (tz.StandardName == "Korea Standard Time") cboSelectedLocation.Text = s; if (tz.StandardName == System.TimeZone.CurrentTimeZone.StandardName) _sCurrentLoc = tz.StandardName; } } //changes timezone name and time depending on what is selected in the cbobox. protected void cboSelectedLocation_SelectedIndexChanged(object sender, EventArgs e) { foreach (TimeZoneInfo tz in System.TimeZoneInfo.GetSystemTimeZones()) { if (cboSelectedLocation.Text == tz.DisplayName) { _sLocation = tz.StandardName; _tsSelectedTime = tz.GetUtcOffset(DateTime.UtcNow); } } } }

¿Algún consejo sobre qué buscar para un codificador de rookie asp?

EDITAR : agregó más código detrás

Graham Clark tenía razón en la necesidad de !Page.IsPostBack , pero ahora es algo con las variables globales que establezco. Este código fue arrastrado y eliminado del proyecto ac #, por lo que supongo que hay algunos problemas con las variables globales y asp.net. Es hora de que investigue más sobre esto para comprender cómo las variables globales difieren en un programa independiente en comparación con un programa web.