2012-11-16 11 views
5

ここは私のカスタムコントロールです。WebControlクラスの[Height]プロパティを継承しています。他のプロパティを計算するためにコンストラクタでアクセスしたいのですが、その値は常に0です。WebControlsコンストラクタasp netのマーク可能なプロパティ

public class MyControl : WebControl, IScriptControl 
{ 

    public MyControl() 
    { 
     AnotherProperty = Calculate(Height); 
     ....... 
    } 

私のaspx

 <hp:MyControl Height = "31px" .... /> 

答えて

3

マークアップの値は、あなたのコントロールのコンストラクタでは利用できませんが、彼らはあなたのコントロールののOnInitイベント内から利用できます。 @andleerとして

protected override void OnInit(EventArgs e) 
{ 
    // has value even before the base OnInit() method in called 
    var height = base.Height; 

    base.OnInit(e); 
} 
+1

GetScriptDescriptors()メソッドでHeightにアクセスするにはどうすればよいですか? –

+0

他の変数に高さを保存しなければならないのですか? this.U = base.Height; –

+0

私はあなたがしようとしていることに完全に従っていないし、 'IScriptControl'インターフェースに精通していません。 – andleer

1

したがって、マークアップで指定されている任意のプロパティ値は、コンストラクタで利用できない、マークアップは、コントロールのコンストラクタで、まだ読み込まれていないと述べました。オンデマンドで使用する前に別のプロパティを計算し、OnInitの前に使用しないことを確認してください:

private int fAnotherPropertyCalculated = false; 
private int fAnotherProperty; 
public int AnotherProperty 
{ 
    get 
    { 
    if (!fAnotherPropertyCalculated) 
    { 
     fAnotherProperty = Calculate(Height); 
     fAnotherPropertyCalculated = true; 
    } 
    return fAnotherProperty; 
    } 
} 
関連する問題