Why does my ASP.NET user control's field value reset to 0?

Posted by Innogetics on Stack Overflow See other posts from Stack Overflow or by Innogetics
Published on 2010-06-05T23:20:49Z Indexed on 2010/06/05 23:22 UTC
Read the original article Hit count: 195

Filed under:

In the code below, why does the groupId value reset to 0 during Page_Load event?

Maybe perhaps the AccountGrid created with groupId 1 is not the one that is loaded to the page?

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

In my page, I have

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

© Stack Overflow or respective owner

Related posts about ASP.NET