Search code examples
asp.netuser-controls

User Control not getting declarative property value


I have a user control on a web form that is declared as follows:

<nnm:DeptDateFilter ID="deptDateFilter" runat="server" AllowAllDepartments="True" />

In the code-behind for this control, AllowAllDepartments is declared as follows:

internal bool AllowAllDepartments { get; set; }

Yet when I view the page, and set a breakpoint in the control's Page_Load event handler, my AllowAllDepartments property is always false. What are possible reasons for this?

BREAKING NEWS: Even setting the property programmatically has no effect on the property value when I hit my breakpoint in Page_Load of the control. Here is the Page_Load of the host page:

deptDateFilter.FilterChanged += deptDateFilter_FilterChanged;
if (!IsPostBack)
{
    deptDateFilter.AllowAllDepartments = true;
    PresentReport();
}

Solution

  • Try adding the property value to the ViewState:

    protected bool AllowAllDepartments 
    {
       get
       {
          if (ViewState["AllowAllDepartments"] != null)
             return bool.Parse(ViewState["AllowAllDepartments"]);
          else
             return false;
       }
       set
       {
          ViewState["AllowAllDepartments"] = value;
       }
    }
    

    EDIT Furthermore, you may want to handle the control's PreRender event, to see whether the the control's property has been correctly set there or not.