Casting to parent page from user control - how??

Discussion in 'ASP .Net' started by Eric, Nov 22, 2006.

  1. Eric

    Eric Guest

    I got a particular problem in visual studio 2005
    There's a user control on page and I want to meka a cast like this

    MyPage mp=(MyPage)this.Page;

    Error is : cannot cast from ASP.mypage_blalala_aspx to MyPage.
    Stupid namespace ASP doesn't have that class name.
    I can't even put any base classes to that crazy App_Code folder
    to get the class reference. Am I doing something wrong?
    All I want to do is during click to access a mainpage method from user control
    that is on a gridview. Or how to simply tell a parent page that one of
    user controls raised some event. There can be many user controls on the grid.
    What's the point of blocking the page classes?
    How to get rid of this terrible App_code folder and make things work?
    I beg for help. Thanks.

    Eric
    Eric, Nov 22, 2006
    #1
    1. Advertising

  2. Eric

    Karl Seguin Guest

    I agree it's annoying, but there are good solutions available...

    1 -
    In the user control you can use the @reference directive to make the type
    available, something like:

    <%@ Reference Page="~/about.aspx" %>

    which should make the cast work...

    problem with this is that it ties your user control to that particular
    page - which defeats part of the purpose of using a user control...

    2 -
    Using an interface or a base class in the app_code is the right way to do it
    (unless you go with #3). This is the ASP.NET team's attempt to force
    developers to code better (which in theory I agree with). I'm not sure why
    you are having problems with this...but I myself find it a little annoying
    and generally prefer #3.


    3 -
    Using the Web Application Project which you can (freely) download from:
    http://msdn2.microsoft.com/en-us/asp.net/aa336618.aspx

    Once installed, you can create new web applications projects which behave A
    LOT more like everything worked in 2003. I use this almost exclusively for
    anything that's midly complicated (as far as structure goes). You'll have
    to restart your project though..copy and pasting should mostly work :)

    Karl

    --
    http://www.openmymind.net/
    http://www.codebetter.com/


    "Eric" <> wrote in message
    news:...
    >I got a particular problem in visual studio 2005
    > There's a user control on page and I want to meka a cast like this
    >
    > MyPage mp=(MyPage)this.Page;
    >
    > Error is : cannot cast from ASP.mypage_blalala_aspx to MyPage.
    > Stupid namespace ASP doesn't have that class name.
    > I can't even put any base classes to that crazy App_Code folder
    > to get the class reference. Am I doing something wrong?
    > All I want to do is during click to access a mainpage method from user
    > control
    > that is on a gridview. Or how to simply tell a parent page that one of
    > user controls raised some event. There can be many user controls on the
    > grid.
    > What's the point of blocking the page classes?
    > How to get rid of this terrible App_code folder and make things work?
    > I beg for help. Thanks.
    >
    > Eric
    Karl Seguin, Nov 22, 2006
    #2
    1. Advertising

  3. Eric

    Eric Guest

    Thanks Karl.
    Do you know if there are any particular advantages using
    Web Site Project model versus Web Application Project model?
    The first one enforces many limits for coding which I'd say
    doesn't make it a better coding. OOP concept is all about
    managing variety of many objects which now are removed.
    User controls sometimes if not mostly have purpose
    of eliminating large number of identical objects that
    perform different functions in the base class that is our page class.
    Accessing methods is easy, event objects get destroyed on the roundtrips
    so it's hard to say what is really better.
    BTW any reference to page from user.c. causes circular reference error
    an in fact there is a namespace to page but hidden and disabled in VS2005)
    Thanks for help.

    Eric

    "Karl Seguin" <> wrote in message news:%...
    >I agree it's annoying, but there are good solutions available...
    >
    > 1 -
    > In the user control you can use the @reference directive to make the type
    > available, something like:
    >
    > <%@ Reference Page="~/about.aspx" %>
    >
    > which should make the cast work...
    >
    > problem with this is that it ties your user control to that particular
    > page - which defeats part of the purpose of using a user control...
    >
    > 2 -
    > Using an interface or a base class in the app_code is the right way to do it
    > (unless you go with #3). This is the ASP.NET team's attempt to force
    > developers to code better (which in theory I agree with). I'm not sure why
    > you are having problems with this...but I myself find it a little annoying
    > and generally prefer #3.
    >
    >
    > 3 -
    > Using the Web Application Project which you can (freely) download from:
    > http://msdn2.microsoft.com/en-us/asp.net/aa336618.aspx
    >
    > Once installed, you can create new web applications projects which behave A
    > LOT more like everything worked in 2003. I use this almost exclusively for
    > anything that's midly complicated (as far as structure goes). You'll have
    > to restart your project though..copy and pasting should mostly work :)
    >
    > Karl
    >
    > --
    > http://www.openmymind.net/
    > http://www.codebetter.com/
    >
    >
    > "Eric" <> wrote in message
    > news:...
    >>I got a particular problem in visual studio 2005
    >> There's a user control on page and I want to meka a cast like this
    >>
    >> MyPage mp=(MyPage)this.Page;
    >>
    >> Error is : cannot cast from ASP.mypage_blalala_aspx to MyPage.
    >> Stupid namespace ASP doesn't have that class name.
    >> I can't even put any base classes to that crazy App_Code folder
    >> to get the class reference. Am I doing something wrong?
    >> All I want to do is during click to access a mainpage method from user
    >> control
    >> that is on a gridview. Or how to simply tell a parent page that one of
    >> user controls raised some event. There can be many user controls on the
    >> grid.
    >> What's the point of blocking the page classes?
    >> How to get rid of this terrible App_code folder and make things work?
    >> I beg for help. Thanks.
    >>
    >> Eric

    >
    Eric, Nov 22, 2006
    #3
  4. Eric

    Karl Seguin Guest

    Yes, there are some advantages. The Web site project is much more flexible
    when it comes to deployment...you can fully pre-compile your site (you can
    do that with the other one too actually), you can compile none of your site
    or you can just compile your source code. For some people, this is a key
    benefit.

    There's also no .proj file, which in some cases is great, and in some cases
    isn't...

    It's really give and take..but like I said, for my apps I tend to prefer the
    WAP..

    Karl

    --
    http://www.openmymind.net/
    http://www.codebetter.com/


    "Eric" <> wrote in message
    news:...
    > Thanks Karl.
    > Do you know if there are any particular advantages using Web Site Project
    > model versus Web Application Project model?
    > The first one enforces many limits for coding which I'd say doesn't make
    > it a better coding. OOP concept is all about
    > managing variety of many objects which now are removed.
    > User controls sometimes if not mostly have purpose
    > of eliminating large number of identical objects that perform different
    > functions in the base class that is our page class.
    > Accessing methods is easy, event objects get destroyed on the roundtrips
    > so it's hard to say what is really better.
    > BTW any reference to page from user.c. causes circular reference error
    > an in fact there is a namespace to page but hidden and disabled in VS2005)
    > Thanks for help.
    >
    > Eric
    >
    > "Karl Seguin" <> wrote in
    > message news:%...
    >>I agree it's annoying, but there are good solutions available...
    >>
    >> 1 -
    >> In the user control you can use the @reference directive to make the type
    >> available, something like:
    >>
    >> <%@ Reference Page="~/about.aspx" %>
    >>
    >> which should make the cast work...
    >>
    >> problem with this is that it ties your user control to that particular
    >> page - which defeats part of the purpose of using a user control...
    >>
    >> 2 -
    >> Using an interface or a base class in the app_code is the right way to do
    >> it (unless you go with #3). This is the ASP.NET team's attempt to force
    >> developers to code better (which in theory I agree with). I'm not sure
    >> why you are having problems with this...but I myself find it a little
    >> annoying and generally prefer #3.
    >>
    >>
    >> 3 -
    >> Using the Web Application Project which you can (freely) download from:
    >> http://msdn2.microsoft.com/en-us/asp.net/aa336618.aspx
    >>
    >> Once installed, you can create new web applications projects which behave
    >> A LOT more like everything worked in 2003. I use this almost exclusively
    >> for anything that's midly complicated (as far as structure goes). You'll
    >> have to restart your project though..copy and pasting should mostly work
    >> :)
    >>
    >> Karl
    >>
    >> --
    >> http://www.openmymind.net/
    >> http://www.codebetter.com/
    >>
    >>
    >> "Eric" <> wrote in message
    >> news:...
    >>>I got a particular problem in visual studio 2005
    >>> There's a user control on page and I want to meka a cast like this
    >>>
    >>> MyPage mp=(MyPage)this.Page;
    >>>
    >>> Error is : cannot cast from ASP.mypage_blalala_aspx to MyPage.
    >>> Stupid namespace ASP doesn't have that class name.
    >>> I can't even put any base classes to that crazy App_Code folder
    >>> to get the class reference. Am I doing something wrong?
    >>> All I want to do is during click to access a mainpage method from user
    >>> control
    >>> that is on a gridview. Or how to simply tell a parent page that one of
    >>> user controls raised some event. There can be many user controls on the
    >>> grid.
    >>> What's the point of blocking the page classes?
    >>> How to get rid of this terrible App_code folder and make things work?
    >>> I beg for help. Thanks.
    >>>
    >>> Eric

    >>
    Karl Seguin, Nov 23, 2006
    #4
  5. Eric,
    As Karl "kinda" alluded, I personally don't even use website projects
    anymore. I find the new WAP model to be far preferable. I'm talking about
    production applications for public websites here.
    Peter

    --
    Co-founder, Eggheadcafe.com developer portal:
    http://www.eggheadcafe.com
    UnBlog:
    http://petesbloggerama.blogspot.com




    "Eric" wrote:

    > Thanks Karl.
    > Do you know if there are any particular advantages using
    > Web Site Project model versus Web Application Project model?
    > The first one enforces many limits for coding which I'd say
    > doesn't make it a better coding. OOP concept is all about
    > managing variety of many objects which now are removed.
    > User controls sometimes if not mostly have purpose
    > of eliminating large number of identical objects that
    > perform different functions in the base class that is our page class.
    > Accessing methods is easy, event objects get destroyed on the roundtrips
    > so it's hard to say what is really better.
    > BTW any reference to page from user.c. causes circular reference error
    > an in fact there is a namespace to page but hidden and disabled in VS2005)
    > Thanks for help.
    >
    > Eric
    >
    > "Karl Seguin" <> wrote in message news:%...
    > >I agree it's annoying, but there are good solutions available...
    > >
    > > 1 -
    > > In the user control you can use the @reference directive to make the type
    > > available, something like:
    > >
    > > <%@ Reference Page="~/about.aspx" %>
    > >
    > > which should make the cast work...
    > >
    > > problem with this is that it ties your user control to that particular
    > > page - which defeats part of the purpose of using a user control...
    > >
    > > 2 -
    > > Using an interface or a base class in the app_code is the right way to do it
    > > (unless you go with #3). This is the ASP.NET team's attempt to force
    > > developers to code better (which in theory I agree with). I'm not sure why
    > > you are having problems with this...but I myself find it a little annoying
    > > and generally prefer #3.
    > >
    > >
    > > 3 -
    > > Using the Web Application Project which you can (freely) download from:
    > > http://msdn2.microsoft.com/en-us/asp.net/aa336618.aspx
    > >
    > > Once installed, you can create new web applications projects which behave A
    > > LOT more like everything worked in 2003. I use this almost exclusively for
    > > anything that's midly complicated (as far as structure goes). You'll have
    > > to restart your project though..copy and pasting should mostly work :)
    > >
    > > Karl
    > >
    > > --
    > > http://www.openmymind.net/
    > > http://www.codebetter.com/
    > >
    > >
    > > "Eric" <> wrote in message
    > > news:...
    > >>I got a particular problem in visual studio 2005
    > >> There's a user control on page and I want to meka a cast like this
    > >>
    > >> MyPage mp=(MyPage)this.Page;
    > >>
    > >> Error is : cannot cast from ASP.mypage_blalala_aspx to MyPage.
    > >> Stupid namespace ASP doesn't have that class name.
    > >> I can't even put any base classes to that crazy App_Code folder
    > >> to get the class reference. Am I doing something wrong?
    > >> All I want to do is during click to access a mainpage method from user
    > >> control
    > >> that is on a gridview. Or how to simply tell a parent page that one of
    > >> user controls raised some event. There can be many user controls on the
    > >> grid.
    > >> What's the point of blocking the page classes?
    > >> How to get rid of this terrible App_code folder and make things work?
    > >> I beg for help. Thanks.
    > >>
    > >> Eric

    > >

    >
    =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=, Nov 23, 2006
    #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. kevin
    Replies:
    11
    Views:
    5,803
    Andrew McDonagh
    Jan 8, 2005
  2. HP
    Replies:
    3
    Views:
    571
  3. Wally Barnes
    Replies:
    3
    Views:
    523
    Wally Barnes
    Nov 20, 2008
  4. Sosuke

    Up casting and down casting

    Sosuke, Dec 20, 2009, in forum: C++
    Replies:
    2
    Views:
    561
    James Kanze
    Dec 20, 2009
  5. Bill S.
    Replies:
    2
    Views:
    211
    Bill S.
    May 8, 2004
Loading...

Share This Page