这是我的自定义control.It从WebControl类继承的高度属性。我想在构造函数中访问它来计算其他properties.But,它的值总是0。
public class MyControl : WebControl, IScriptControl
{
public MyControl()
{
AnotherProperty = Calculate(Height);
.......
}我的aspx
<hp:MyControl Height = "31px" .... /> 发布于 2012-11-17 00:39:04
标记值在控件的构造函数中不可用,但在控件的OnInit事件中可用。
protected override void OnInit(EventArgs e)
{
// has value even before the base OnInit() method in called
var height = base.Height;
base.OnInit(e);
}发布于 2012-11-29 10:38:09
正如@andleer所说,在控件的构造函数中还没有读取标记,因此在标记中指定的任何属性值在构造函数中都不可用。在即将使用时计算另一个按需属性,并确保在OnInit之前未使用:
private int fAnotherPropertyCalculated = false;
private int fAnotherProperty;
public int AnotherProperty
{
get
{
if (!fAnotherPropertyCalculated)
{
fAnotherProperty = Calculate(Height);
fAnotherPropertyCalculated = true;
}
return fAnotherProperty;
}
}https://stackoverflow.com/questions/13420320
复制相似问题