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

G

Guest

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.EventArgs) Handles Me.Init
Dim loginURControl As Control = LoadControl("~/UC/" + Page.Theme +
"/skin_login.ascx")
loginFormSkinHolder.Controls.Add(loginURControl)
End Sub
===========

loginFormSkinHolder 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("SetTheme")

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 typedLoginURControl as ? = Ctype(loginURControl , ?)
==========

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

Steven Cheng[MSFT]

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+qBz7cDJaZSd2h+YOtipRuuA==
| X-WBNR-Posting-Host: 64.180.16.145
| From: =?Utf-8?B?U2FtdWVs?= <[email protected]>
| 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: <[email protected]>
| 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.public.dotnet.framework.aspnet
| NNTP-Posting-Host: TK2MSFTNGXA03.phx.gbl 10.40.2.250
| Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGXA03.phx.gbl
| Xref: TK2MSFTNGXA01.phx.gbl
microsoft.public.dotnet.framework.aspnet:116624
| X-Tomcat-NG: microsoft.public.dotnet.framework.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.EventArgs) Handles Me.Init
| Dim loginURControl As Control = LoadControl("~/UC/" + Page.Theme
+
| "/skin_login.ascx")
| loginFormSkinHolder.Controls.Add(loginURControl)
| End Sub
| ===========
|
| loginFormSkinHolder 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("SetTheme")
|
| 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 typedLoginURControl as ? = Ctype(loginURControl , ?)
| ==========
|
| 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.
|
 
S

Scott Allen

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/archive/2005/06/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:
http://odetocode.com/Blogs/scott/archive/2005/08/01/2030.aspx
 
G

Guest

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.findcontrol method, where parent is the placeholder for the user
control.

Thanks in advance!
 
S

Steven Cheng[MSFT]

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: AcWcatRYhbGZL0muQ7q9v8dS7ULiIw==
| X-WBNR-Posting-Host: 64.180.16.145
| From: =?Utf-8?B?U2FtdWVs?= <[email protected]>
| References: <[email protected]>
<[email protected]>
| 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: <[email protected]>
| 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.public.dotnet.framework.aspnet
| NNTP-Posting-Host: TK2MSFTNGXA03.phx.gbl 10.40.2.250
| Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGXA03.phx.gbl
| Xref: TK2MSFTNGXA01.phx.gbl
microsoft.public.dotnet.framework.aspnet:116812
| X-Tomcat-NG: microsoft.public.dotnet.framework.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.findcontrol method, where parent is the placeholder for the user
| control.
|
| Thanks in advance!
|
 
G

Guest

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.findcontrol("the_control_name_defined_in_main_page")?

Thanks in advance!
 
S

Steven Cheng[MSFT]

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.findcontrol("the_control_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?= <[email protected]>
| References: <[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
| 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: <[email protected]>
| 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.public.dotnet.framework.aspnet
| NNTP-Posting-Host: TK2MSFTNGXA03.phx.gbl 10.40.2.250
| Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGXA03.phx.gbl
| Xref: TK2MSFTNGXA01.phx.gbl
microsoft.public.dotnet.framework.aspnet:116836
| X-Tomcat-NG: microsoft.public.dotnet.framework.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.findcontrol("the_control_name_defined_in_main_page")?
|
| Thanks in advance!
|
 
S

Scott Allen

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top