我有一个控件:定义按钮的复合控件。在使控件可见之前,该按钮调用一个 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);
}
}
步骤个人:
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),所以这是不可能的。我试过谷歌搜索,Bing'ing 甚至 Yahoo! 这但没有运气。
更新 我没有提到,我无法将控件添加到 OnPreRender 下的集合中,因为它不会连接任何事件。我也不能使用 EnsureChildControls,因为它搞砸了应用程序的其他部分。但是,我可以将这个级别的属性值添加到状态包中,但我觉得这是一种非常糟糕的方式。