Backwards Inheritence In Web User Controls

  • Thread starter Thread starter john bailo
  • Start date Start date
J

john bailo

For a c# web application, I created a user control that includes a form in
control.

The idea is, on the main Page, when the user clicks Submit from
the master form, that makes the user control visible and gives
it new functionality, but retains the look of the top half of
the master page. I then hide the original submit button.

Ok, now, after pressing the Submit button in the user control, I want
to change the look of the master page.

The questions are:

(0) how do I gain access to the properties of the Page that
instantiates the user control, from the user control itself?

(1) Is some kind of upwards/backwards inheritence is necessary?

(2) Or do I pass the whole Page as an object to the click event?
 
0) There is a Page property of the UserControl class (which you are
inheriting from) that gives you a reference to the instantiating page.
1) Huh? What does this mean?
2) I take it this is related to the first question? In which case that's the
answer.
 
0) There is a Page property of the UserControl class (which you are
inheriting from) that gives you a reference to the instantiating page.

When I enter Page, intellisense seems to show a
generic Page object -- but I want to control the
actual calling object along with its specific
properties ( such as text boxes, tables ) and so
on.

Can I just assume "Page" will take on those properties
at run time? Is Page like window.opener() ( the Parent )
in javascript?
 
john bailo said:
When I enter Page, intellisense seems to show a
generic Page object -- but I want to control the
actual calling object along with its specific
properties ( such as text boxes, tables ) and so
on.

Can I just assume "Page" will take on those properties
at run time? Is Page like window.opener() ( the Parent )
in javascript?

Say your code-behind class file for the Page is called 'MyPage', then
you should be able to cast the Page property from the usercontrol like
so:

MyPage myPage = this.Page as MyPage;

If you wanted to access the properties of the page, you will need to
change them to public as opposed to private or protected, then they
will show up in intellisense on the myPage object.

Alternatively, you can use Page.FindControl. Say you have a textbox
called 'MyTextBox' on the page, you can access it like so:

TextBox myTextBox = Page.FindControl("MyTextBox") as TextBox;

Regards,
Paddo
 
Genius!

Thanks, Paddo....


Paddo said:
"john bailo" <[email protected]> wrote in message

Say your code-behind class file for the Page is called 'MyPage', then
you should be able to cast the Page property from the usercontrol like
so:

MyPage myPage = this.Page as MyPage;

If you wanted to access the properties of the page, you will need to
change them to public as opposed to private or protected, then they
will show up in intellisense on the myPage object.

Alternatively, you can use Page.FindControl. Say you have a textbox
called 'MyTextBox' on the page, you can access it like so:

TextBox myTextBox = Page.FindControl("MyTextBox") as TextBox;

Regards,
Paddo
 
MyPage myPage = this.Page as MyPage;

If you wanted to access the properties of the page, you will need to
change them to public as opposed to private or protected, then they
will show up in intellisense on the myPage object.

Alternatively, you can use Page.FindControl. Say you have a textbox
called 'MyTextBox' on the page, you can access it like so:

TextBox myTextBox = Page.FindControl("MyTextBox") as TextBox;

Paddo:

I used your method with great success; and I
don't mean to look a gift horse in the mouth;
however, from an OOP standpoint, it still doesn't
make any sense to me.

I think of a User Control as being instantiated
by the Page to begin with. To then, instantiate
a new this.Page to me would mean instantiating
a different object from the object that called
the User Control.

How can it be, that instantiating a new 'copy'
of the MyPage object allows me to gain access
to the properties and methods of the original
myPage, the one that called the User Control ?
 
Back
Top