如何从在其他的.ascx的.ascx GET变量? [英] How to get variables from .ascx in another .ascx?
问题描述
我有两个.ascx文件,一是被加载到其他的.ascx控件中的占位符。
I have two .ascx files, one gets loaded into a placeholder within the other .ascx control.
所以我有:
ParentControl
- > ChildControl
我要访问从ChildControl的ParentControl得到可变数据。
I want to access the ParentControl from ChildControl to get variable data.
到目前为止,我有:
ParentControl.ascx
public UserList GetFunction
{
get
{
return someVariable;
}
}
而在
ChildControl.ascx
Protected void Page_Load(object sender, EventArgs e){
ParentControl page = new ParentControl;
string newVariable = page.GetFunction.someOtherVariable;
}
我在哪里走错了,当我在父类返回someVariable它有什么,我需要它,但是当我设法得到它在ChildControl page.GetFunction返回null。
Where am I going wrong, when I return someVariable in parent class it has what I need in it, but when I try to get it in the ChildControl page.GetFunction returns null.
让我知道如果你需要更多的信息。
Let me know if you need more information.
更新应答:
我已经maanged解决此问题由周围的工作:
I have maanged to resolve this issue by working around it:
在父类叫我在子类中定义的函数,通过分析我所需要的值。
In parentclass I call a function defined in the child class and parse the values I need through.
ParentClass.ascx
ParentClass.ascx
protected void Page_Load
{
ControlIWantToGetInformationTo.SetInfo(info);
}
ChildClass.ascx
ChildClass.ascx
public void SetInfo(Info info)
{
string someString = info.TheVariableWithin;
}
有用户使用的那块code提到,
Several users have mentioned using the piece of code,
this.parent;
但因为我使用Sitecore的CMS的this.parent返回不必要的值,因为该页面不是身体一旦通过Sitecore的运行。
but because I am using sitecore cms the this.parent returns an unwanted value because the page is not physical once run through sitecore.
感谢所有庆幸协助我设法通过问题得到:)
Thanks for all the assistance glad I managed to get through the issue :).
推荐答案
要你的子类中访问父控件,使用的财产父
你的子类:
To access the parent control within your child class, use the property "Parent" of your child class:
protected void Page_Load(object sender, EventArgs e)
{
ParentControl page = (ParentControl)this.Parent;
if(page != null)
string newVariable = page.GetFunction();
}
希望,这有助于。
Hope, this helps.
这篇关于如何从在其他的.ascx的.ascx GET变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!