Access property of parent webform from UC

Discussion in 'ASP .Net' started by Ben Amada, Oct 30, 2005.

  1. Ben Amada

    Ben Amada Guest

    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, Oct 30, 2005
    #1
    1. Advertising

  2. Ben Amada

    Siva M Guest

    Hi,

    Try this:

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

    "Ben Amada" <> wrote in message
    news:...
    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, Oct 30, 2005
    #2
    1. Advertising

  3. Ben Amada

    Ben Amada Guest

    Siva M wrote:

    > Hi,
    >
    > Try this:
    >
    > if (UserControl.Parent is <desired page-class name>)
    > {
    > //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, Oct 31, 2005
    #3
  4. Ben Amada

    Siva M Guest

    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.

    "Ben Amada" <> wrote in message
    news:...
    Siva M wrote:

    > Hi,
    >
    > Try this:
    >
    > if (UserControl.Parent is <desired page-class name>)
    > {
    > //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, Oct 31, 2005
    #4
  5. Ben Amada

    Ben Amada Guest

    Siva M wrote:

    > 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.


    Works great .. thanks again!

    Ben
    Ben Amada, Oct 31, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. titof
    Replies:
    0
    Views:
    497
    titof
    Jul 24, 2003
  2. timmso
    Replies:
    1
    Views:
    478
    Alex Papadimoulis
    Dec 12, 2003
  3. =?Utf-8?B?bXN1aw==?=

    Using one webform to set a value on another webform

    =?Utf-8?B?bXN1aw==?=, Jul 12, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    456
    =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=
    Jul 12, 2006
  4. =?Utf-8?B?Vk1J?=

    Moving value from popup Webform to main Webform?

    =?Utf-8?B?Vk1J?=, Oct 9, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    570
    GroupReader
    Oct 9, 2006
  5. Bitswapper
    Replies:
    5
    Views:
    133
    Prasad, Ramit
    Aug 27, 2013
Loading...

Share This Page