locked
Referencing to usercontrol from another aspx-file RRS feed

  • Question

  • User1024180000 posted

    I have an application which is structured like this:

    Page1.aspx

        UserControl1

        UserControl2

    Page2.aspx

        UserControl1

        UserControl2

    Both pages are using same Master Page. However I'm unable to access User Control1 in Page1.aspx from UserControl1 in Page2.aspx. In Page2.aspx.cs I have tried this:

              ContentPlaceHolder mpContentPlaceHolder = (ContentPlaceHolder)(Page.Master.FindControl("myContentPlaceHolder"));
              Page1UserControl userCtrl = (Page1UserControl)(mpContentPlaceHolder.FindControl("controlName"));
             

    I have already declared properties in User Controls so if I wanted to access MyProperty (Page1.aspx, UserControl1) value from User Control1 (Page2.aspx) I would like to do it like this:

          userCtrl.MyProperty = "123";

    I get an error message and userCtrl has a value of null. Actually userCtrl points to Page2.aspx instead of Page1.aspx. How can I change this ? 

    Thursday, May 29, 2008 3:57 AM

Answers

  • User-16411453 posted

     I don't think it works that way.  Each page is independent, and only objects on the displaying page exists at the moment. It's not like a windows application where several pages can exist at the same time, and you can access those pages objects. It's a web program, not a windows program!.

    You can place both pages on the same page, and separate them with div tags, or panels, and toggle visibility. 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, May 31, 2008 7:21 PM

All replies

  • User-1399914977 posted
    By default controls access modifiers are protected. You can make it public to access outside
    Thursday, May 29, 2008 4:13 AM
  • User-16411453 posted

     I don't think it works that way.  Each page is independent, and only objects on the displaying page exists at the moment. It's not like a windows application where several pages can exist at the same time, and you can access those pages objects. It's a web program, not a windows program!.

    You can place both pages on the same page, and separate them with div tags, or panels, and toggle visibility. 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, May 31, 2008 7:21 PM