2012-03-20 126 views
0

我有一个控件:复合控件定义了一个按钮。在使控件可见之前,该按钮将调用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,因为它将应用程序的其他部分搞砸了。然而,我可以将这个级别的财产的价值添加到一个州袋,但我觉得这是一个非常糟糕的做法。

回答

0

我并没有真正地解决这个问题,但我有一个NoSQL解决方案,并且存储这些控件之间的值并在应用程序关闭时处理它。