Sunday, May 1, 2011

Access data on Page from Web Control

I created a web control, and it needs some data from its parent page.
How can it access that data?

Update: Thank you for the quick solutions, however they don't work for me. Visual Studio doesn't recognize the name of the page as a class. I took the name from where the class is defind:

public partial class Apps_Site_Templates_CollegesMain : cUICommonFeatures

(cUICommonFeatures inherits from System.Web.UI.Page)

But in the control, when I define

        protected System.Web.UI.Page parentPage;
        parentPage = (Apps_Site_Templates_CollegesMain)Page;

I get a compliation error:

The type or namespace name 'Apps_Site_Templates_CollegesMain' could not be found (are you missing a using directive or an assembly reference?)

I feel like I'm missing something really basic here, and I'll probably be very embarrassed when I get an answer, but I do need help....

From stackoverflow
  • If the parent page class is named lets say ParentPage, you can do this within the control:

    ParentPage page = (ParentPage)this.Page;
    

    Then you can access the properties and methods on ParentPage. If you have more pages using the same control, you should use an interface on the parent page to access the properties on the page.

    IParentPage page = (IParentPage)this.Page;
    

    Does that answer your question?

    Lea Cohen : My problem is that Visual Studio doesn't recognize the parent page name as a class... I updated my question
    Paul Suart : +1 - interfaces are definitely the way to go here.
    asgerhallas : @Lea Cohen: Do you have the "using namespace.where.page.class.reside" in the top of your usercontrol file? If you do, what namespaces are the page and the usercontrol in, respectively?
  • There are many ways to do that. I think the best one is adding a property for the data control needs and in your page set this property.

    Also you can access to your page from your control like that :

    string dataYouWant = ((YourParentPageName)Page).GetData();
    

    Or you can add the data to viewstate, and read it from the child controls.

    But as I said, I would choose the first one.

  • All Controls (server controls, usercontrols and custom controls) expose a property Page which allows you to access the containing Page instance from the code of the control.

    Therefore, you could simply do:

    // In Usercontrol code:
    MyParentPage parentPage = this.Page as MyParentPage;
    if (parentPage != null)
    {
      // Access the properties of the Parent page.
      string t = parentPage.Title;
    }
    

    The article "Mastering Page-UserControl Communication" offers a good beginners introduction to control-Page interactions.

  • Controls should be written to be independant of what page they're on. If the control needs a piece of data, then it should expose a public property which is of the type of the data that it needs. The page it is on wo uld then set that property to the data that the control needs. This permits the control to be used on another page, or even made part of a UserControl and that UserControl then used on the parent page.

0 comments:

Post a Comment