Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Re: Accessing UserControls in ASPX code-behind pages

Thread Tools

Re: Accessing UserControls in ASPX code-behind pages

Mark Fitzpatrick
Posts: n/a
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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

Reply With Quote
Steven Cheng[MSFT]
Posts: n/a
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

Reply With Quote
Posts: n/a
THanks, Steve. I think that answers it all.

Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Using Web.config's <system.web><pages><controls><add /></controls></pages></system.web> To Register UserControls Nathan Sokalski ASP .Net 5 01-10-2007 10:50 AM
Using Web.config's <system.web><pages><controls><add /></controls></pages></system.web> To Register UserControls Nathan Sokalski ASP .Net Web Controls 4 12-21-2006 02:50 AM
Using Web.config's <system.web><pages><controls><add /></controls></pages></system.web> To Register UserControls Nathan Sokalski ASP .Net Building Controls 4 12-21-2006 02:50 AM
passing variables from .aspx.cs code pages to .aspx pages RadioSignal ASP .Net Web Controls 7 10-21-2005 11:41 PM
adding main.aspx.vb & main.aspx.resx under aspx John M ASP .Net 1 05-29-2005 09:27 PM