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
    news:...
    > 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
    NOTE THAT THIS ASSUMES THAT VISUAL STUDIO IS *NOT* BEING USED]:
    >
    > I have a UserControl, "SomeUserControl." It works fine. It consists of two

    files:
    > 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
    1. Advertising

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

    Well, in fact, this behavior is the same with the asp.net '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
    http://msdn.microsoft.com/library/en-us/dnaspp/html/aspnet-pageobjectmodel.a
    sp?frame=true

    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.

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

    Get Preview at ASP.NET whidbey
    http://msdn.microsoft.com/asp.net/whidbey/default.aspx
     
    Steven Cheng[MSFT], Apr 24, 2004
    #2
    1. Advertising

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

    Alex
     
    =?Utf-8?B?QWxleCBNYWdoZW4=?=, Apr 24, 2004
    #3
    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. wh

    usercontrols + code-behind

    wh, Feb 13, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    352
    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
    Replies:
    3
    Views:
    7,914
    Ben Strackany
    Nov 24, 2004
  3. Paolo Pignatelli
    Replies:
    6
    Views:
    2,223
    Paolo Pignatelli
    Feb 21, 2005
  4. wh

    usercontrols + code-behind

    wh, Feb 13, 2004, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    144
    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
    Replies:
    7
    Views:
    2,340
    Mirronelli
    Oct 22, 2005
Loading...

Share This Page