我有一個控件:複合控件定義了一個按鈕。在使控件可見之前,該按鈕將調用Command事件並設置另一個控件屬性的值。在複合控件之間傳遞值
這兩個控件都是事先在容器控件中實例化的。我需要在CreateChildControls()方法的第二種形式中獲取屬性的值,但是,這是不可能的,爲什麼?
場景:
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),所以這是不可能的。我嘗試了谷歌搜索,Binging甚至雅虎!這但沒有運氣。
更新 我沒有提及,我無法將控件添加到OnPreRender下的集合中,因爲它沒有連接任何事件。我無法使用EnsureChildControls,因爲它將應用程序的其他部分搞砸了。然而,我可以將這個級別的財產的價值添加到一個州袋,但我覺得這是一個非常糟糕的做法。