English 中文(简体)
访问 CreateCreate Controls () 创建儿童控制() 建立的控制
原标题:accessing controls created by CreateChildControls()

我需要从另一类中访问由CreateChild Controls()创建的控件, 这样当我选择文件时, 我就会在字符串上找到要引用的路径 。

我尝试了“href=”的解决方案 https://stackoverflow.com/ questions/2982198/ access-control-controls- controls-created-hirdal-c” > Accessing controls(c#) 和

从另一个班级,我应该可以访问文本框

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

        string aaa = asspi.txtUrl.Text;



    }
最佳回答

我不得不让控制控制程序公开, 才能从另一类上进入。 但是它反亮了无效引用错误 。 我已经更新了最初的文章 。

如果您公开暴露了对孩子的控制,您需要在每次公开曝光的孩子们的控制中,在获取器中调用 Sure child Controls 。这将强制执行 CreateChild Controls ,因此您要建造控制树,确保拨打者不会获得无效引用。

例如:

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;

您还应该继承 Compiosite Controlation 的遗产,因为 control controls 属性有类似之处:

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

如果您出于某种原因没有继承 Compiosite control control , 那么您需要将此控件添加到您的类中 。

顺便提一下, 曝光儿童控制可能会给您的打电话者太多信息, 他们可能不应该关注此执行细节。 相反, 您只能披露您控制孩子的相关属性 。 例如, 您可能暴露一个孩子 TextBox TxtUrl , 而不是孩子 TextBox < code> TxtUrl , 您可能暴露一个字符串属性 < code> Url :

public string Url
{
    get
    {
        EnsureChildControls();
        return txtUrl.Text;            
    }
    set
    {
        EnsureChildControls();
        txtUrl.Text = value;
    }
}
问题回答

结尾处,. NET 在页面中添加静态控件时会做什么. NET 会将控件作为字段的引用( 它们通常会转到. 设计者文件) 。 所以, 只需将控件与字段相同 :

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




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签