在用户控制的用户控制和母版页值访问控制 [英] Access a control in user control and master page value in User control

查看:89
本文介绍了在用户控制的用户控制和母版页值访问控制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎么能在用户控件访问母版页和母版页的控制值的用户控制的控制值?

How can i access a control value in user control in master page and master page control value in user control?

感谢和放大器;问候

推荐答案

您是最好的办法是让以类似的方式如下面的用户控件的公共属性:

You're best bet is to make public properties in the user control in a similar fashion as below:

public TextBox getMyTextBox()
{
    return this.myTextBox;
}

这将给以控制为一体的母版页的访问。如果你只在得到一个值或两个兴趣再做到这一点:

This will give the master page access to the control as a whole. If you are only interested in getting a single value or two then do this:

public String getMyTextBoxValue()
{
    return this.myTextBox.Text;
}

根据您希望能够从母版页用户控件访问什么,你可以到该对象的引用,创建用户控件时(如在构造函数的参数)传递

Depending on what you want to be able to access in your user control from the master page, you can pass in a reference to that object when creating the user control (as a parameter in the constructor)

private object needObject;

public MyUserControl(object objectToReference)
{
     this.needObject = objectToReference;
}

这样,您必须将对象的引用。 飞 - 动态如果你正在创建的控制这将是容易的。如果你希望他们从你的表单设计一开始,然后做一个单独的方法来设置你的参考。

This way you will have a reference to the object. This will be easy if you're creating the controls 'on the fly - dynamically'. If you want them in your form design from the very start then make a separate method to set the reference for you.

public void setParentReference(object objectToReference)
{
    this.needObject = objectToReference;
}

在主页面,您可以简单地调用这个方法你给用户控件传递您所需要的任何对象(只要它不是一个原始的,如果它是一个原始值,然后使用 REF 来传递作为参考,因为原语默认值发送)作为参数,并从该用户控件点可以访问该对象。

In the master page you can simply call this method on your given usercontrol passing in whatever object you need (as long as it's not a primitive, if it is a primitive value then use ref to pass as a reference because primitives send value by default) as a parameter and from that point on the usercontrol will have access to the object.

当然这仅仅是存储引用一个简单对象的例子(你可以在你想分享或任何其他值一些类通过。)

Of course this is just an example of storing reference to a simple object (you can pass in some class which you want to share or any other value.)

如果您要访问您的用户控件里的家长,那么你应该记住, Control.Parent ,您可以访问到当前控件的父。

If you want to access the parent inside your usercontrol then you should keep in mind that Control.Parent gives you access to the parent of the current control.

由于用户控件类从 ContentControl中继承反过来从控制你可以简单地做到这一点。

Since UserControl class inherits from ContentControl which in turn inherits from Control you can simply do this.

object parentReference = this.Parent;

这是一个更好的做法是使用对象这里,然后检查它是否是一个的typeof 的形式,因为如果它在一个容器中,然后在容器将被保存,它可能会导致异常,如果变量类型是表格

It is a better practice to use object here and then check if it's is a typeof form because if it is in a container then the container will be stored and it could cause exception if the variable type was Form.

您也可以使用此来获得ParentForm。

You can also use this to get the ParentForm.

Form parentReference = this.ParentForm;

但要记住的事情是,如果你刚才创建这个动态控制,并没有将其添加到任何形式或控制儿童列出父和ParentForm将所以一定要使用它,或者试图将它转换为东西之前验证为

But a thing to keep in mind is that if you have just created this dynamic control and haven't yet added it to any form or control childrens list the Parent and ParentForm will be null so make sure you validate for null before using it or trying to cast it as something.

然后你就可以这样使用从表单中的任何财产。我假设你的主人形式被称为Form1上。 (公共部分Form1类:表格

Then you can use any property from the form by doing this. I assume your master form is called Form1. (public partial class Form1 : Form)

((Form1)parentReference).somePublicProperty = someValue;

希望它可以清除它给你。

Hope it clears it up for you.

这篇关于在用户控制的用户控制和母版页值访问控制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆