简体   繁体   中英

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.

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?

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:

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

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. I tried adding a QueryString in the CommandEventHanlder, but get a collection is locked error message. I tried cache and Sessions (sessions won't work cause this will be deployed to SharePoint) so that's out of the question. I've tried Googling, Bing'ing even 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. 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解决方案作为基础,并在其中存储了控件之间的值,并在应用程序关闭时进行处理。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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