3

CreateChildControls() によって作成されたコントロールに別のクラスからアクセスする必要があるため、ファイルを選択したときに参照する文字列のパスを取得できます。

動的に作成されたコントロールへのアクセス(c#)および動的に作成されたコントロールへのアクセスの問題で解決策を試しましたが 、喜びはありません

    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);

    }

別のクラスからテキストボックスにアクセスできるはずです

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

        string aaa = asspi.txtUrl.Text;



    }
4

2 に答える 2

5

I had to make the controls public to be accessible from another class. but it retuns null reference error. I have updated the initial post

If you expose your child controls publicly, you need to call EnsureChildControls in the getter for each publicly-exposed child control. This will force CreateChildControls to be executed, and hence your control tree to be built, ensuring the caller does not get a null reference.

E.g.:

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

...

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

Note that in order to do this, you need to expose your child controls as properties, not fields. I.e. in your sample code, you need to replace:

public TextBox txtUrl;  

by:

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

You should also inherit from CompositeControl, which does something similar for the Controls property:

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

If for some reason you are not inheriting from CompositeControl, then you'll need to add this Controls override to your class.

Incidentally, exposing child controls might be giving too much information to your callers, who probably shouldn't be concerned with such implementation details. Instead you could expose only the relevant properties of your child controls. For example, instead of exposing a child TextBox TxtUrl, you could expose a string property Url thus:

public string Url
{
    get
    {
        EnsureChildControls();
        return txtUrl.Text;            
    }
    set
    {
        EnsureChildControls();
        txtUrl.Text = value;
    }
}
于 2012-05-25T17:01:35.377 に答える
0

最後に、静的コントロールをページに追加するときに .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);
}
于 2012-05-25T15:49:46.267 に答える