reference web control in dynamically added user control, ASP.NET 2

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Samuel

    reference web control in dynamically added user control, ASP.NET 2

    Hi,

    I am building a page that makes use of user control as a templating
    technique. The following is that I have in mind and it is actually working:

    Root/
    -- login.aspx
    -- login.aspx.vb
    -- UC/
    ----- Classic/
    --------- skin_login.ascx
    ----- Blue/
    --------- skin_login.ascx
    ----- skin_login.ascx .vb

    as you can see, login.aspx has a partial class defined in login.aspx.vb. The
    folder UC/ contains 2 templates for login.aspx, Classic and Blue.
    skin_login.ascx are in the two folders Classic/ and Blue/ that inherit the
    same partial class skin_login.ascx .vb in a level above (right under UC/).

    I am dynamically loading either "Blue/skin_login.ascx " or
    "Classic/skin_login.ascx " based on user preference into login.aspx and I want
    to be able to reference the web controls defined in skin_login.ascx in
    login.aspx. The following is the logic:

    In login.aspx.vb
    ===========
    Private Sub Page_Init(ByVal sender As Object, ByVal e As
    System.EventArg s) Handles Me.Init
    Dim loginURControl As Control = LoadControl("~/UC/" + Page.Theme +
    "/skin_login.ascx ")
    loginFormSkinHo lder.Controls.A dd(loginURContr ol)
    End Sub
    ===========

    loginFormSkinHo lder is a placeholder control in login.aspx, and I am loading
    the user control based on the Page.Theme of the page. I create a same name
    folder under UC/ for each theme I have under APP_themes.

    Inside the user control there is a dropdown list with ID SetTheme. I can
    reference it in login.aspx by using:

    loginURControl. FindControl("Se tTheme")

    but what I wanted to do is I want to reference it by using

    loginURControl. SetTheme

    and I am not sure how I can do that. Any suggestion is deeply appreciated. I
    have tried casting loginURControl from Control to something else like the
    following:

    ==========
    Dim loginURControl As Control = LoadControl("~/UC/" + Page.Theme +
    "/skin_login.ascx ")
    Dim typedLoginURCon trol as ? = Ctype(loginURCo ntrol , ?)
    ==========

    but I don't know what I should specify as the type. I have tried the
    filename of the User control (skin_login) but it didn't work (intelliSense
    can't recognize it and the page can't compile)

    I hope I have explained my situation very clearly. Any help is deeply
    appreciated.
  • Steven Cheng[MSFT]

    #2
    RE: reference web control in dynamically added user control, ASP.NET 2

    Hi,

    Thanks for your posts. As for this Usercontrol problem, I'll perform some
    tests on my local side first and will let you know if I got any progress.
    Thanks,

    Steven Cheng
    Microsoft Online Support

    Get Secure! www.microsoft.com/security
    (This posting is provided "AS IS", with no warranties, and confers no
    rights.)
    --------------------
    | Thread-Topic: reference web control in dynamically added user control,
    ASP.NET 2
    | thread-index: AcWb5u+qBz7cDJa ZSd2h+YOtipRuuA ==
    | X-WBNR-Posting-Host: 64.180.16.145
    | From: =?Utf-8?B?U2FtdWVs?= <programmer@nos pam.nospam>
    | Subject: reference web control in dynamically added user control, ASP.NET
    2
    | Date: Mon, 8 Aug 2005 00:01:01 -0700
    | Lines: 64
    | Message-ID: <7DF30F1D-1D79-4A82-B8C1-4AC3E33710F9@mi crosoft.com>
    | MIME-Version: 1.0
    | Content-Type: text/plain;
    | charset="Utf-8"
    | Content-Transfer-Encoding: 7bit
    | X-Newsreader: Microsoft CDO for Windows 2000
    | Content-Class: urn:content-classes:message
    | Importance: normal
    | Priority: normal
    | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.0
    | Newsgroups: microsoft.publi c.dotnet.framew ork.aspnet
    | NNTP-Posting-Host: TK2MSFTNGXA03.p hx.gbl 10.40.2.250
    | Path: TK2MSFTNGXA01.p hx.gbl!TK2MSFTN GXA03.phx.gbl
    | Xref: TK2MSFTNGXA01.p hx.gbl
    microsoft.publi c.dotnet.framew ork.aspnet:1166 24
    | X-Tomcat-NG: microsoft.publi c.dotnet.framew ork.aspnet
    |
    | Hi,
    |
    | I am building a page that makes use of user control as a templating
    | technique. The following is that I have in mind and it is actually
    working:
    |
    | Root/
    | -- login.aspx
    | -- login.aspx.vb
    | -- UC/
    | ----- Classic/
    | --------- skin_login.ascx
    | ----- Blue/
    | --------- skin_login.ascx
    | ----- skin_login.ascx .vb
    |
    | as you can see, login.aspx has a partial class defined in login.aspx.vb.
    The
    | folder UC/ contains 2 templates for login.aspx, Classic and Blue.
    | skin_login.ascx are in the two folders Classic/ and Blue/ that inherit
    the
    | same partial class skin_login.ascx .vb in a level above (right under UC/).
    |
    | I am dynamically loading either "Blue/skin_login.ascx " or
    | "Classic/skin_login.ascx " based on user preference into login.aspx and I
    want
    | to be able to reference the web controls defined in skin_login.ascx in
    | login.aspx. The following is the logic:
    |
    | In login.aspx.vb
    | ===========
    | Private Sub Page_Init(ByVal sender As Object, ByVal e As
    | System.EventArg s) Handles Me.Init
    | Dim loginURControl As Control = LoadControl("~/UC/" + Page.Theme
    +
    | "/skin_login.ascx ")
    | loginFormSkinHo lder.Controls.A dd(loginURContr ol)
    | End Sub
    | ===========
    |
    | loginFormSkinHo lder is a placeholder control in login.aspx, and I am
    loading
    | the user control based on the Page.Theme of the page. I create a same
    name
    | folder under UC/ for each theme I have under APP_themes.
    |
    | Inside the user control there is a dropdown list with ID SetTheme. I can
    | reference it in login.aspx by using:
    |
    | loginURControl. FindControl("Se tTheme")
    |
    | but what I wanted to do is I want to reference it by using
    |
    | loginURControl. SetTheme
    |
    | and I am not sure how I can do that. Any suggestion is deeply
    appreciated. I
    | have tried casting loginURControl from Control to something else like the
    | following:
    |
    | ==========
    | Dim loginURControl As Control = LoadControl("~/UC/" + Page.Theme +
    | "/skin_login.ascx ")
    | Dim typedLoginURCon trol as ? = Ctype(loginURCo ntrol , ?)
    | ==========
    |
    | but I don't know what I should specify as the type. I have tried the
    | filename of the User control (skin_login) but it didn't work
    (intelliSense
    | can't recognize it and the page can't compile)
    |
    | I hope I have explained my situation very clearly. Any help is deeply
    | appreciated.
    |

    Comment

    • Scott Allen

      #3
      Re: reference web control in dynamically added user control, ASP.NET 2

      Hi Samuel:

      You'll need to use an @ Reference directive to import a user control
      type into an aspx. See my post
      "One More On ASP.NET 2.0 Compilation"
      http://odetocode.com/Blogs/scott/arc...6/30/1889.aspx for more
      info.

      The problem is the user control code will most likely exist in a
      different assembly than your ASPX page, and the only way to reference
      the assembly is with the @ Reference directive.

      What I'd suggest is having all of your user controls implement an
      interface you define, or use a class derived from UserControl as the
      base for both skins. This will allow you to cast to the type you've
      defined outside of an ascx, aspx page. This is similar to the
      technique I'm showing here:


      --
      Scott


      On Mon, 8 Aug 2005 00:01:01 -0700, Samuel <programmer@nos pam.nospam>
      wrote:
      [color=blue]
      >Hi,
      >
      >I am building a page that makes use of user control as a templating
      >technique. The following is that I have in mind and it is actually working:
      >
      >Root/
      >-- login.aspx
      >-- login.aspx.vb
      >-- UC/
      >----- Classic/
      >--------- skin_login.ascx
      >----- Blue/
      >--------- skin_login.ascx
      >----- skin_login.ascx .vb
      >
      >as you can see, login.aspx has a partial class defined in login.aspx.vb. The
      >folder UC/ contains 2 templates for login.aspx, Classic and Blue.
      >skin_login.asc x are in the two folders Classic/ and Blue/ that inherit the
      >same partial class skin_login.ascx .vb in a level above (right under UC/).
      >
      >I am dynamically loading either "Blue/skin_login.ascx " or
      >"Classic/skin_login.ascx " based on user preference into login.aspx and I want
      >to be able to reference the web controls defined in skin_login.ascx in
      >login.aspx. The following is the logic:
      >
      >In login.aspx.vb
      >===========
      > Private Sub Page_Init(ByVal sender As Object, ByVal e As
      >System.EventAr gs) Handles Me.Init
      > Dim loginURControl As Control = LoadControl("~/UC/" + Page.Theme +
      >"/skin_login.ascx ")
      > loginFormSkinHo lder.Controls.A dd(loginURContr ol)
      > End Sub
      >===========
      >
      >loginFormSkinH older is a placeholder control in login.aspx, and I am loading
      >the user control based on the Page.Theme of the page. I create a same name
      >folder under UC/ for each theme I have under APP_themes.
      >
      >Inside the user control there is a dropdown list with ID SetTheme. I can
      >reference it in login.aspx by using:
      >
      >loginURControl .FindControl("S etTheme")
      >
      >but what I wanted to do is I want to reference it by using
      >
      >loginURControl .SetTheme
      >
      >and I am not sure how I can do that. Any suggestion is deeply appreciated. I
      >have tried casting loginURControl from Control to something else like the
      >following:
      >
      >==========
      >Dim loginURControl As Control = LoadControl("~/UC/" + Page.Theme +
      >"/skin_login.ascx ")
      >Dim typedLoginURCon trol as ? = Ctype(loginURCo ntrol , ?)
      >==========
      >
      >but I don't know what I should specify as the type. I have tried the
      >filename of the User control (skin_login) but it didn't work (intelliSense
      >can't recognize it and the page can't compile)
      >
      >I hope I have explained my situation very clearly. Any help is deeply
      >appreciated.[/color]

      Comment

      • Samuel

        #4
        Re: reference web control in dynamically added user control, ASP.N

        hi Scott,

        I defined an abstract class with an abstract method in APP_code that is
        inherited and overridden in the skin's partial class file. This works fine
        for me and I am just wondering if this is the correct way to do it. Can you
        provide me some insight as to what advantage I get by using an abstract class
        v.s. using the findControl method and cast it. The abstract method returns a
        dropdownlist, and the control found by findcontrol can also be casted to a
        dropdownlist too.

        Another question: as I can now successfully access members in the user
        control, I wonder how I can do the same but this time, the other way around -
        access members in the page within the user control without using the
        parent.findcont rol method, where parent is the placeholder for the user
        control.

        Thanks in advance!

        Comment

        • Steven Cheng[MSFT]

          #5
          Re: reference web control in dynamically added user control, ASP.N

          Thanks for Scott's informative inputs.

          Hi Samuel,

          Using abstract class or interface is mainly to hidden your actual
          implemenation. Though using abstract class, we still need to cast the
          control instance (through findControl) into the abstract class reference,
          however, this is all the same when we add new type of usercontrols (still
          use the same casting and referencing code).

          As for the further question on accessing Page property from usercontrol,
          it's just the same as in asp.net 1.x, we're limited to the UserControl/
          Page model. In UserControl we can only get a "Page" reference and we need
          to do explicit cast (to the concrete page class) if we need to explicitly
          access page's certain property. Also, we can use reflection api to
          investage page properties, but this has high performance costs.

          Thanks,

          Steven Cheng
          Microsoft Online Support

          Get Secure! www.microsoft.com/security
          (This posting is provided "AS IS", with no warranties, and confers no
          rights.)




          --------------------
          | Thread-Topic: reference web control in dynamically added user control,
          ASP.N
          | thread-index: AcWcatRYhbGZL0m uQ7q9v8dS7ULiIw ==
          | X-WBNR-Posting-Host: 64.180.16.145
          | From: =?Utf-8?B?U2FtdWVs?= <programmer@nos pam.nospam>
          | References: <7DF30F1D-1D79-4A82-B8C1-4AC3E33710F9@mi crosoft.com>
          <k5qef19n1d01ar 0jdipq2r98c3nau pj7mr@4ax.com>
          | Subject: Re: reference web control in dynamically added user control,
          ASP.N
          | Date: Mon, 8 Aug 2005 15:45:09 -0700
          | Lines: 17
          | Message-ID: <00C1F371-03B4-4CBF-825F-E31449EF09FF@mi crosoft.com>
          | MIME-Version: 1.0
          | Content-Type: text/plain;
          | charset="Utf-8"
          | Content-Transfer-Encoding: 7bit
          | X-Newsreader: Microsoft CDO for Windows 2000
          | Content-Class: urn:content-classes:message
          | Importance: normal
          | Priority: normal
          | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.0
          | Newsgroups: microsoft.publi c.dotnet.framew ork.aspnet
          | NNTP-Posting-Host: TK2MSFTNGXA03.p hx.gbl 10.40.2.250
          | Path: TK2MSFTNGXA01.p hx.gbl!TK2MSFTN GXA03.phx.gbl
          | Xref: TK2MSFTNGXA01.p hx.gbl
          microsoft.publi c.dotnet.framew ork.aspnet:1168 12
          | X-Tomcat-NG: microsoft.publi c.dotnet.framew ork.aspnet
          |
          | hi Scott,
          |
          | I defined an abstract class with an abstract method in APP_code that is
          | inherited and overridden in the skin's partial class file. This works
          fine
          | for me and I am just wondering if this is the correct way to do it. Can
          you
          | provide me some insight as to what advantage I get by using an abstract
          class
          | v.s. using the findControl method and cast it. The abstract method
          returns a
          | dropdownlist, and the control found by findcontrol can also be casted to
          a
          | dropdownlist too.
          |
          | Another question: as I can now successfully access members in the user
          | control, I wonder how I can do the same but this time, the other way
          around -
          | access members in the page within the user control without using the
          | parent.findcont rol method, where parent is the placeholder for the user
          | control.
          |
          | Thanks in advance!
          |

          Comment

          • Samuel

            #6
            Re: reference web control in dynamically added user control, ASP.N

            Thanks Steven for your reply. Let me clarify a few things:

            are you saying that when casting type to the abstract class type, we are
            using the findcontrol method internally? So, does it mean that there is no
            difference whatsoever performance-wise that is more advantageous to use
            abstract class than calling findcontrol myself to access controls defined in
            a user control?

            Also, can you give me a simple example to what you meant in the second
            paragraph?
            I don't quite understand because I thought when we access controls defined
            in the main page from a dynamically added user control, we have to use
            parent.findcont rol("the_contro l_name_defined_ in_main_page")?

            Thanks in advance!

            Comment

            • Steven Cheng[MSFT]

              #7
              Re: reference web control in dynamically added user control, ASP.N

              Thanks for the response Samuel,

              As for "abstract class", of course, if you expose some public
              property/field to represent the Controls in the page(concretae derived
              page), it'll be more efficient than you manually using findControl to get
              it on the fly. But since using abstract base class add the page's
              inheritance which also has performance cost.

              For the second paragraph, you mentioned that
              ========
              access controls defined
              in the main page from a dynamically added user control, we have to use
              parent.findcont rol("the_contro l_name_defined_ in_main_page")?
              =========

              of course , this is ok. In fact, what I mentioned is that in case when we
              would like to access some public property or field of the container Page,
              then, we still need to use down casting or reflection to access them
              thorugh the Control.Page property.

              Thanks & Regards,

              Steven Cheng
              Microsoft Online Support

              Get Secure! www.microsoft.com/security
              (This posting is provided "AS IS", with no warranties, and confers no
              rights.)




              --------------------
              | Thread-Topic: reference web control in dynamically added user control,
              ASP.N
              | thread-index: AcWcm24WABe9OJp +TwCcL/lVSVez/Q==
              | X-WBNR-Posting-Host: 64.180.16.145
              | From: =?Utf-8?B?U2FtdWVs?= <programmer@nos pam.nospam>
              | References: <7DF30F1D-1D79-4A82-B8C1-4AC3E33710F9@mi crosoft.com>
              <k5qef19n1d01ar 0jdipq2r98c3nau pj7mr@4ax.com>
              <00C1F371-03B4-4CBF-825F-E31449EF09FF@mi crosoft.com>
              <ARWadrInFHA.34 72@TK2MSFTNGXA0 1.phx.gbl>
              | Subject: Re: reference web control in dynamically added user control,
              ASP.N
              | Date: Mon, 8 Aug 2005 21:33:03 -0700
              | Lines: 15
              | Message-ID: <DCEE4BEA-34CB-4E5D-972E-585CBFB88960@mi crosoft.com>
              | MIME-Version: 1.0
              | Content-Type: text/plain;
              | charset="Utf-8"
              | Content-Transfer-Encoding: 7bit
              | X-Newsreader: Microsoft CDO for Windows 2000
              | Content-Class: urn:content-classes:message
              | Importance: normal
              | Priority: normal
              | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.0
              | Newsgroups: microsoft.publi c.dotnet.framew ork.aspnet
              | NNTP-Posting-Host: TK2MSFTNGXA03.p hx.gbl 10.40.2.250
              | Path: TK2MSFTNGXA01.p hx.gbl!TK2MSFTN GXA03.phx.gbl
              | Xref: TK2MSFTNGXA01.p hx.gbl
              microsoft.publi c.dotnet.framew ork.aspnet:1168 36
              | X-Tomcat-NG: microsoft.publi c.dotnet.framew ork.aspnet
              |
              | Thanks Steven for your reply. Let me clarify a few things:
              |
              | are you saying that when casting type to the abstract class type, we are
              | using the findcontrol method internally? So, does it mean that there is
              no
              | difference whatsoever performance-wise that is more advantageous to use
              | abstract class than calling findcontrol myself to access controls defined
              in
              | a user control?
              |
              | Also, can you give me a simple example to what you meant in the second
              | paragraph?
              | I don't quite understand because I thought when we access controls
              defined
              | in the main page from a dynamically added user control, we have to use
              | parent.findcont rol("the_contro l_name_defined_ in_main_page")?
              |
              | Thanks in advance!
              |

              Comment

              • Scott Allen

                #8
                Re: reference web control in dynamically added user control, ASP.N

                Sorry I missed your follow up question, Samual. Thanks to Steven for
                following up. Let me know if I can answer anything else.

                --
                Scott


                On Mon, 8 Aug 2005 21:33:03 -0700, Samuel <programmer@nos pam.nospam>
                wrote:
                [color=blue]
                >Thanks Steven for your reply. Let me clarify a few things:
                >
                >are you saying that when casting type to the abstract class type, we are
                >using the findcontrol method internally? So, does it mean that there is no
                >difference whatsoever performance-wise that is more advantageous to use
                >abstract class than calling findcontrol myself to access controls defined in
                >a user control?
                >
                >Also, can you give me a simple example to what you meant in the second
                >paragraph?
                >I don't quite understand because I thought when we access controls defined
                >in the main page from a dynamically added user control, we have to use
                >parent.findcon trol("the_contr ol_name_defined _in_main_page") ?
                >
                >Thanks in advance![/color]

                Comment

                Working...