3

내 드롭 다운 상자에서 OnSelectedIndexChanged 이벤트가 발생하지 않습니다. 내가 본 모든 포럼은 AutoPostBack="true"을 추가하라고 말했지만 그 결과는 바뀌지 않았습니다.드롭 다운 OnSelectedIndexChanged가 실행되지 않음

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> 

코드 숨김

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

신인 ASP 코더에 대한보고 무엇에 어떤 조언을?

편집이 : 그레이엄 클라크는 !Page.IsPostBack를 필요에 정확했다


뒤에 더 많은 코드를 추가했지만 지금은 내가 설정 한 전역 변수로 무언가이다. 이 코드는 C# 프로젝트에서 끌어서 놓기 때문에 전역 변수와 asp.net에 몇 가지 문제가 있다고 가정합니다. 필자가 웹 프로그램과 달리 독립 변수에서 전역 변수가 어떻게 다른지 이해하기 위해 더 많은 연구를 할 시간입니다.

+2

정말 해고되지 않습니까? foreach 밖에서 중단 점을 설정 했습니까? 그것은 발화가 아니라고 믿게하는 무관 한 문제 일 수 있습니다. – ctorx

+2

데이터를 드롭 다운에 어떻게 바인딩 하시겠습니까? 마크 업에 빈 드롭 다운이 표시됩니다. 코드 뒤에 값을 바인딩하고 있습니까? –

+0

@matthew : 예, foreach 루프 외부에서 실행되지 않습니다. @KP : 코드 뒤에 타임 존 정보를 설정하고 있습니다. – Jim

답변

9

당신은 드롭 다운 목록을 서버로 돌아갈 때마다 또는 다시 게시 할 때마다 데이터 바인딩합니까? 매번이 작업을 수행하는 경우 서버에서 아무 것도 선택되지 않았다고 생각할 수 있으므로 이벤트가 실행되지 않습니다.

Page_Load 이벤트의 드롭 다운을 데이터 바인딩한다고 가정 해보십시오. 다음과 같이하고 싶습니다.

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