用户控件未获得声明性属性值 [英] User Control not getting declarative property value
问题描述
我在 Web 表单上有一个用户控件,声明如下:
I have a user control on a web form that is declared as follows:
<nnm:DeptDateFilter ID="deptDateFilter" runat="server" AllowAllDepartments="True" />
在此控件的代码隐藏中,AllowAllDepartments
声明如下:
In the code-behind for this control, AllowAllDepartments
is declared as follows:
internal bool AllowAllDepartments { get; set; }
然而,当我查看页面并在控件的 Page_Load
事件处理程序中设置断点时,我的 AllowAllDepartments
属性始终为 false.造成这种情况的可能原因是什么?
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?
突发新闻:当我在控件的 Page_Load 中遇到断点时,即使以编程方式设置属性也不会影响属性值.这是主机页面的 Page_Load:
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();
}
推荐答案
尝试将属性值添加到 ViewState:
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;
}
}
编辑此外,您可能希望处理控件的 PreRender 事件,以查看控件的属性是否已正确设置.
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.
这篇关于用户控件未获得声明性属性值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!