Re: Accessing UserControls in ASPX code-behind pages

Discussion in 'ASP .Net' started by Mark Fitzpatrick, Apr 23, 2004.

  1. Alex,
    You use the register directive as normal to register it in the page
    itself. Now, when you call if from code-behind (or from any other class file
    in the assembly) you create it as a variable so that you can manipulate it.
    Just like putting a label control called myLabel on the page. In aspx you
    add it like <asp:label id="myLabel" runat="server" />. To access it from the
    code behind you create declare the variable protected
    System.Web.UI.WebControls.Label myLabel; You can then access any of the
    public properties associated with it.

    Forget about the .cs file not being available, it's not supposed to be.
    The register directive is accessing the .ascx file, which has information as
    to the class to inherit from, not just the location of the .cs file so when
    the control is called, it knows what compiled class to use.

    Hope this helps,
    Mark Fitzpatrick
    Microsoft MVP - FrontPage

    "Alex Maghen" <> wrote in message
    > I am confused about the approach to creating and using a UserControl on an

    ..ASPX page which has a code-behind .CS file. Here's the scenario [AND PLEASE
    > I have a UserControl, "SomeUserControl." It works fine. It consists of two

    > SomeUserControl.ascx
    > SomeUserControl.cs
    > I've been using this control from .ASPX pages that do not have a

    code-behind .CS file. In those .ASPX pages, I've been using a Register
    directive that looks like:
    > <%@ Register
    > tagprefix="MyStuff"
    > Tagname="SomeUserControl"
    > Src="/UserControls/SomeUserControl.ascx" %>
    > To actually place one of the controls on the page, I do:
    > <MyStuff:SomeUserControl id="SUC1" runat="server" />
    > Okay. So far, all of this works just fine!
    > Now here's the problem: Let's say, instead of having a simple .ASPX page

    which has <SCRIPT> C# inside it, I want to have a Code-Behing .CS file. So
    now I have two files that make up my page:
    > AnyPage.aspx
    > AnyPage.cs
    > The AnyPage.aspx files has the following directive at the top:
    > <%@ Page
    > Language="C#"
    > Inherits="MyStuff.AnyPage"
    > Src="AnyPage.cs" %>
    > This page works fine too. BUT, if I want to put a SomeUserControl on my

    AnyPage.aspx AND I want to be able to manipulate it from my AnyPage.cs, I
    get confused. In order to access it from the .CS code-behind file, it looks
    like I have to declare it as a member of the AnyPage Class. To do that, I
    need to be able to include a "using" directive that defines the user control
    SomeUserControl. To do *THAT*, I have to pre-compile the SomeUserControl
    into an Assembly. But if I do *THAT*, I don't understand the syntax for
    Registering the SomeUserControl on my .ASPX page.
    > So my question is, when a UserCOntrol's code-behind .CS file has been

    pre-compiled into the assembly AND THE SOURCE .CS FILES IS NO LONGER
    AVAILABLE, what is the "<% @ Register..." syntax to use on the .ASPX pages
    to use that user control?
    > Thanks as always for your help!
    > Alex
    Mark Fitzpatrick, Apr 23, 2004
    1. Advertisements

  2. Thanks for Mark's informative suggestions.

    Hi Alex,

    As for the latest question
    "Does this mean that even though you pre-compiled the .CS files for the
    User Control, you still MUST use the varioation of the @ Register directive
    that includes 'Src="xxxxx.ascx"'? "

    Here are my understanding on this:

    In fact, the Real class of the UserControl is not the precompiled class in
    the codebehind cs file(or vb file), the precompiled classes are all
    comipled into the web app's main assembly. As for the UserControl's actual
    class, it is dynamically compiled at runtime when the certain UserControl
    is first time requested. The ASP.NET runtime will parse the ".ascx file"
    and generate the UserControl's class( it is derived from the precompiled
    class which is precompiled into assembly at compile time). That's why the
    ascs file is necessary rather than the precompiled page classes. If the
    UserControl doesn't have a codebehind class, its actual class( generated
    and compiled at runtime ) will derived from the System.Web.UI.UserControl

    Well, in fact, this behavior is the same with the 's aspx page,
    whose actual class is also compiled at runtime, the codebehind's class is
    the parent class from which the actual runtime page class is derived from.
    For detailed description, you may have a look at the following tech article:

    #The ASP.NET Page Object Model

    Also, you can search some further reference on the ASP.NET page mechanism
    in the MSDN library.
    If you have anything unclear, please feel free to post here. Thanks.


    Steven Cheng
    Microsoft Online Support

    Get Secure!
    (This posting is provided "AS IS", with no warranties, and confers no

    Get Preview at ASP.NET whidbey
    Steven Cheng[MSFT], Apr 24, 2004
    1. Advertisements

  3. THanks, Steve. I think that answers it all.

    =?Utf-8?B?QWxleCBNYWdoZW4=?=, Apr 24, 2004
    1. Advertisements

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

    usercontrols + code-behind

    wh, Feb 13, 2004, in forum: ASP .Net
    Alvin Bruney [MVP]
    Mar 2, 2004
  2. Ryan Taylor

    Accessing a code behind variable from ASPX

    Ryan Taylor, Nov 24, 2004, in forum: ASP .Net
    Ben Strackany
    Nov 24, 2004
  3. Paolo Pignatelli
    Paolo Pignatelli
    Feb 21, 2005
  4. wh

    usercontrols + code-behind

    wh, Feb 13, 2004, in forum: ASP .Net Web Controls
    Alvin Bruney [MVP]
    Mar 2, 2004
  5. RadioSignal

    passing variables from .aspx.cs code pages to .aspx pages

    RadioSignal, Sep 12, 2005, in forum: ASP .Net Web Controls
    Oct 22, 2005

Share This Page