开发者

How to get variables from .ascx in another .ascx?

开发者 https://www.devze.com 2023-03-29 16:05 出处:网络
I have two .ascx files, one gets loaded into a placeholder within the other .ascx control. So I have: ParentControl

I have two .ascx files, one gets loaded into a placeholder within the other .ascx control.

So I have:

ParentControl

->ChildControl

I want to access the ParentControl from ChildControl to get variable data.

So far I have:

ParentControl.ascx

public UserList GetFunction
{
  get
  {
   return someVariable;
  }
}

And in the:

ChildControl.ascx

Protected void Page_Load(object sender, EventArgs e){

  ParentControl page = new ParentControl;
  string newVariable = page.GetFunction.someOtherVariable;
  }

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.

UPDATE ANSWER:

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

protected void Page_Load
{
 ControlIWantToGetInformationTo.SetInfo(info);
}

ChildClass.ascx

public void SetInfo(Info info)
{
 string someString = info.TheVariableWithin;
}

Several users have mentioned using the piece of code,

this.parent;

but because I am using sitecore cms the this.parent returns an unwanted value becau开发者_StackOverflowse 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.


I have manged 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

protected void Page_Load {  

   ControlIWantToGetInformationTo.SetInfo(info); 
} 

ChildClass.ascx

public void SetInfo(Info info) 
{  string someString = info.TheVariableWithin; } 

Several users have mentioned using the piece of code,

this.parent; 

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 :).


Creating a new parent control will not give you access to the current controls parent instance settings.

You could add a parent page property to your user control and populate it when you create your child control. Does your control not have a this.Parent ?

0

精彩评论

暂无评论...
验证码 换一张
取 消