views:

265

answers:

4

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

strong text

A: 

Make the property bindable like:

[Bindable(true), Category("Appearance"), DefaultValue(false)]
internal bool AllowAllDepartments { get; set; }
Fiona Holder
+1  A: 

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.

darasd
That may work, and I'll try it as a last resort, but I shouldn't have to explicitly add control properties to the viewstate. I don't even need it in viewstate, as it only has an effect on the first page load.
ProfK
Worked for me as a last resort. Still don't know why it won't work otherwise...
Code Sherpa
A: 

Just out of curiosity... does it work OK if you don't use the get;set; shortcut?

private bool _allowAllDepartments;
public bool AllowAllDepartments
{
    get { return _allowAllDepartments; }
    set { _allowAllDepartments = value;}
}
Bryan
A: 

Have you tried making the property public?

ScottE