私はコントロールを持っています:ボタンを定義する複合コントロール。ボタンは Command イベントを呼び出し、別のコントロールのプロパティの値を設定してから、コントロールを表示します。
これらのコントロールは両方とも、事前にコンテナー コントロール内でインスタンス化されます。CreateChildControls() メソッド内の 2 番目のフォームでプロパティの値を取得する必要がありますが、これができないのはなぜですか?
シナリオ:
public class Main : CompositeControl
{
#region fields
private StepPersonal _stepPersonal;
private StepFinancial _stepFinancial;
#endregion
protected override CreateChildControls()
{
this._stepPersonal = new StepPersonal { ID = "StepPersonal1", Visible = true };
this.Controls.Add(this._stepPersonal);
this._stepFinancial = new StepFinancial { ID = "StepFinancial1", Visible = false };
this.Controls.Add(this._stepFinancial);
}
protected override Render(HtmlTextWriter writer)
{
this._stepPersonal.RenderControl(writer);
this._stepFinancial.RenderControl(writer);
}
}
StepPersonal:
public class StepPersonal : CompositeControl
{
#region fields
private Button _checkDetails;
#endregion
protected override CreateChildControls()
{
this._checkDetails = new Button { ID = "CheckDetailsButton", Text = "CheckDetails", CommandName = "DetailsConfirmation" }
this._checkDetails.Command += new CommandEventHandler(this.OnCheckDetails);
this.Controls.Add(this._checkDetails);
}
protected override Render(HtmlTextWriter writer)
{
this._checkDetail.RenderControl(writer);
}
protected void OnCheckDetails(object sender, CommandEventArgs args)
{
string argument = args.CommandArgs.ToString();
this.Visible = false;
Main owner = (sender as Button).FindParent<Main>(); // custom extension method
StepFinancial sf = owner.FindControl<StepFinancial>("StepFinancial1");
sf.Argument = argument;
sf.Visible = false;
}
}
最後に、これが私の問題があるところです。StepFinancial コントロール
public class StepFinancial : CompositeControl
{
#region fields
private TextBox _argumentTextBox;
#endregion
#region properties
public string Argument { get; set; }
#endregion
protected override CreateChildControls()
{
string argument = this.Argument; // this is always null
// I have buttons in here (not being displayed) who's events are not wiring up
// if I move this into a method, and call the method in Render(), as it seems
// to allow me access to the properties there, but I need the value here?
}
protected override Render(HtmlTextWriter writer)
{
string argument = this.Argument; // this contains the value of the property set previously
this._argumentTextBox.RenderControl(writer);
}
}
StateBag に値を追加しようとしましたが、何もありません。CommandEventHanlder に QueryString を追加しようとしましたが、コレクションがロックされているというエラー メッセージが表示されます。私はキャッシュとセッションを試しました(これはSharePointに展開されるため、セッションは機能しません)ので、それは問題外です。グーグル、ビング、Yahoo! も試してみました。これは運が悪い。
言及しなかった更新 、OnPreRender の下のコレクションにコントロールを追加することはできません。これは、イベントを関連付けないためです。アプリケーションの他の部分を台無しにするため、EnsureChildControlsも使用できません。ただし、このレベルのプロパティの値をステートバッグに追加することはできますが、これは非常に悪い方法だと思います。