2010-06-05 2 views
0

아래 코드에서 Page_Load 이벤트 중에 groupId 값이 0으로 재설정되는 이유는 무엇입니까?왜 내 ASP.NET 사용자 정의 컨트롤의 필드 값이 0으로 재설정 되었습니까?

아마도 groupId 1로 만든 AccountGrid가 페이지에로드 된 것이 아닌 것일까 요? 내 페이지에서

public partial class AccountGrid : System.Web.UI.UserControl 
{ 
    int groupId = 0; 

    public AccountGrid() 
    { 
    } 

    // an aspx page creates AccountGrid with "new AccountGrid(1)" 
    public AccountGrid(int groupId) 
    { 
     this.groupId = groupId; 
    } 

    protected void Page_Load(object sender, EventArgs e) 
    { 
     DataAccessFacade facade = new DataAccessFacade(); 
     // groupId resets to 0 here... 
     grdAccount.DataSource = facade.GetAccountsByAccountGroupId(this.groupId); 
     grdAccount.DataBind(); 
    } 
} 

, 내가 대신 개인 필드를 사용

public partial class Default : System.Web.UI.Page 
{ 
    public Default() 
    { 
    } 

    public void Page_Load(object sender, EventArgs e) 
    { 
     ctlAccountGrid = new Views.Controls.Account.AccountGrid(1); 
     // should I do databind? 
     ctlAccountGrid.DataBind(); 
    } 
} 

답변

1

, 나는 공용 속성을 생성하고 그 대신 생성자를 통해 값을 설정하고 그것을 잘 작동합니다.

관련 문제