简体   繁体   中英

How to access the Public property of aspx page in the custom Control

I have created a Custom control and embedded in the aspx page. Now i want to access a public property in the aspx webpage in the Custom Control (eg: Test.ascx.cs) How i can do that?

TIA

It would not make much sense to add dependency from the custom control to the outside.

Create an interface that contains the property you specified :

public interface IMyPageInterface 
{
   string MyProperty {get;set;}
}

implement interface on your page :

//your page code behind
public partial class WebForm1: System.Web.UI.Page,IMyPageInterface
{
    public string MyProperty 
    {
       get;
       set;
    }
}

you can get your page as IMyPageInterface in your custom control

IMyPageInterface myPage = this.Page as IMyPageInterface;
myPage.MyProp...

A property of type IMyPageInterface can also be defined

public class MyControl :Control
{
    public IMyPageInterface MyPage
    {
        get
        {
            return this.Page as IMyPageInterface;
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM