Access to controls created by CreateChildControls ()

I need to access the controls created by CreateChildControls () from another class, so when I select the file, I have a path to the line for the link.

I tried the solutions in Accessing dynamically created controls (C #) and Problem while accessing dynamically created controls But thanks, no thanks

    publicTextBox txtUrl; 

    protected override void CreateChildControls()
    {
        Label lblUrl = new Label();
        lblUrl.ID = "lblUrl";
        lblUrl.Text = "Url: ";
        Controls.Add(lblUrl);

        TextBox txtUrl = new TextBox();
        txtUrl.ID = "txtUrl";
        Controls.Add(txtUrl);

        AssetUrlSelector picker = new AssetUrlSelector();
        picker.ID = "ausUrl";

        picker.DefaultOpenLocationUrl =  OpenUrl;
        picker.AssetUrlClientID = txtUrl.ClientID;
        picker.AssetUrlTextBoxVisible = false;
        Controls.Add(picker);

        Control control = Page.LoadControl(_ascxPath);
        Controls.Add(control);

    }

From another class I should have access to the text box

   protected void Button1_Click(object sender, EventArgs e)
    {
        AssetPicker asspi = new AssetPicker();

        string aaa = asspi.txtUrl.Text;



    }
+3
source share
2 answers

I had to make public controls accessible from another class. but it returns a zero reference error. I updated the initial entry

, EnsureChildControls . CreateChildControls , , , , , .

:.

public Button MyChildButton
{
    get
    {
        EnsureChildControls();
        return _myChildButton;
    }
}
private Button _myChildButton;

...

protected override void CreateChildControls()  
{
    ...
    _myChildButton = new Button();
    ... 
}

, , . :

public TextBox txtUrl;  

:

public TextBox TxtUrl
{
    get
    {
        EnsureChildControls();
        return txtUrl;
    }
}
private TextBox txtUrl;

CompositeControl, - Controls:

public override ControlCollection Controls
{
    get
    {
        EnsureChildControls();
        return base.Controls;
    }
}

- CompositeControl, .

, , , , . . , , TextBox TxtUrl, Url, :

public string Url
{
    get
    {
        EnsureChildControls();
        return txtUrl.Text;            
    }
    set
    {
        EnsureChildControls();
        txtUrl.Text = value;
    }
}
+5

, .NET, , ( .designer). , :

private Label lblUrl;
private TextBox txtUrl;
private AssetUrlSelector picker;
private Control control;

protected override void CreateChildControls()
{
    lblUrl = new Label();
    lblUrl.ID = "lblUrl";
    lblUrl.Text = "Url: ";
    Controls.Add(lblUrl);

    txtUrl = new TextBox();
    txtUrl.ID = "txtUrl";
    Controls.Add(txtUrl);

    picker = new AssetUrlSelector();
    picker.ID = "ausUrl";

    picker.DefaultOpenLocationUrl =  OpenUrl;
    picker.AssetUrlClientID = txtUrl.ClientID;
    picker.AssetUrlTextBoxVisible = false;
    Controls.Add(picker);

    control = Page.LoadControl(_ascxPath);
    Controls.Add(control);
}
0

All Articles