Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / October 2005

Tip: Looking for answers? Try searching our database.

Access property of parent webform from UC

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Ben Amada - 30 Oct 2005 08:17 GMT
Hello,

I have a user control which will be dynamically loaded from one of two
parent ASPX webforms.  Within the user control I'd like to be able to access
a property from the parent webform.  One problem I'm having is that I don't
know the type of the parent webform until run-time.  Is this possible?  And
if so, how can I do this?

Thank you,
Ben
Siva M - 30 Oct 2005 11:41 GMT
Hi,

Try this:

if (UserControl.Parent is <desired page-class name>)
{
   //type cast Parent property and access the public property
}

Hello,

I have a user control which will be dynamically loaded from one of two
parent ASPX webforms.  Within the user control I'd like to be able to access
a property from the parent webform.  One problem I'm having is that I don't
know the type of the parent webform until run-time.  Is this possible?  And
if so, how can I do this?

Thank you,
Ben
Ben Amada - 31 Oct 2005 10:24 GMT
> Hi,
>
[quoted text clipped - 4 lines]
>    //type cast Parent property and access the public property
> }

Hi Siva,

Thank you for the good idea.  I'm getting a couple of errors however while
trying to implement your idea in VB.  Here's my code:

If Me.Parent Is WebForm6 Then
 CType(Me.Parent, WebForm6).TestProp = "test"
End If

First error: The VS IDE is complaining about WebForm6 during design time.
It says:
"WebForm6 is a type and cannot be used as an expression."

Second error: If I eliminate the If-EndIf so I just have CType(...), then
during run-time, I get the following error:
"System.InvalidCastException: Specified cast is not valid."

WebForm6 is the parent ASPX page of this usercontrol, so I don't see why I'm
getting these errors.

Do you (or anyone else) have any idea what I'm doing wrong?

Thanks!
Ben
Siva M - 31 Oct 2005 15:40 GMT
Ben,

Try it this way:

If TypeOf Me.Page Is WebForm6 Then
   CType (Me.Page, WebForm6).TestProp = "text"
End If

One correction: It is Me.Page, not Me.Parent. Sorry about it.

Siva M wrote:

> Hi,
>
[quoted text clipped - 4 lines]
>    //type cast Parent property and access the public property
> }

Hi Siva,

Thank you for the good idea.  I'm getting a couple of errors however while
trying to implement your idea in VB.  Here's my code:

If Me.Parent Is WebForm6 Then
 CType(Me.Parent, WebForm6).TestProp = "test"
End If

First error: The VS IDE is complaining about WebForm6 during design time.
It says:
"WebForm6 is a type and cannot be used as an expression."

Second error: If I eliminate the If-EndIf so I just have CType(...), then
during run-time, I get the following error:
"System.InvalidCastException: Specified cast is not valid."

WebForm6 is the parent ASPX page of this usercontrol, so I don't see why I'm
getting these errors.

Do you (or anyone else) have any idea what I'm doing wrong?

Thanks!
Ben
Ben Amada - 31 Oct 2005 23:13 GMT
> Ben,
>
[quoted text clipped - 5 lines]
>
> One correction: It is Me.Page, not Me.Parent. Sorry about it.

Works great .. thanks again!

Ben

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.