为什么我的ASP.NET用户控件的字段值重置为0?

时间:2010-06-05 23:20:50

标签: asp.net

在下面的代码中,为什么在Page_Load事件期间groupId值重置为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 个答案:

答案 0 :(得分:1)

我没有使用私有字段,而是创建了一个公共属性,并通过它而不是构造函数设置了值,它运行正常。