在复合控件之间传递值

时间:2012-03-20 16:15:22

标签: c# composite-controls

我有一个控件:定义按钮的复合控件。在使控件可见之前,该按钮调用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),所以这是不可能的。我试过谷歌,Bing'ing甚至雅虎!这个,但没有运气。

更新 我没有提到,我无法将控件添加到OnPreRender下的集合中,因为它没有连接任何事件。我不能使用EnsureChildControls,因为它搞砸了应用程序的其他部分。但是,我可以将此级别的属性值添加到状态包中,但我觉得这是一种非常糟糕的方法。

1 个答案:

答案 0 :(得分:0)

我并没有真正解决这个问题,我觉得很舒服,但我有一个NoSQL解决方案,并在那里的控件之间存储值,并在应用程序关闭时处理它。