简体   繁体   English

在复合控件之间传递值

[英]passing values between composite controls

I have a control : composite control that defines a button. 我有一个控件:定义一个按钮的复合控件。 The button calls a Command event and sets the value of another control's property, before making the control visible. 在使控件可见之前,该按钮调用Command事件并设置另一个控件的属性的值。

Both of these controls are instantiated within a container control before hand. 这两个控件都是在容器控件中实例化的。 I need to grab the value of the property in the second form within the CreateChildControls() method, however, this is not possible, why? 我需要在CreateChildControls()方法中以第二种形式获取属性的值,但是,这是不可能的,为什么?

Scenario: 场景:

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: 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;
    }
}

Then lastly, and this is where my problem lies, the StepFinancial control 最后,这就是我的问题所在,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);
    }
}

I've tried adding the value to a StateBag, nothing. 我试过将值添加到StateBag中,什么也没有。 I tried adding a QueryString in the CommandEventHanlder, but get a collection is locked error message. 我尝试在CommandEventHanlder中添加QueryString,但得到一个集合被锁定的错误消息。 I tried cache and Sessions (sessions won't work cause this will be deployed to SharePoint) so that's out of the question. 我尝试了缓存和会话(会话将无法工作,因为它将被部署到SharePoint),所以这是不可能的。 I've tried Googling, Bing'ing even Yahoo! 我尝试了谷歌搜索,甚至还对Yahoo! this but no luck. 这可是没有运气。

Update I failed to mention, I can't add the controls to the collection under OnPreRender as it does not wire up any of the events. 更新我没有提到,我无法将控件添加到OnPreRender下的集合中,因为它不会连接任何事件。 I can't use EnsureChildControls either as it screws up other parts of the application. 我不能使用确保孩子控制,因为它会弄乱应用程序的其他部分。 I can, however, add the value from the property at this level, to a statebag, but I feel this is a very bad way of doing it. 但是,我可以将这个级别的属性的值添加到状态包中,但是我觉得这样做是非常糟糕的方法。

我并没有真正解决此问题,但是我有一个NoSQL解决方案作为基础,并在其中存储了控件之间的值,并在应用程序关闭时进行处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM