Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > reference web control in dynamically added user control, ASP.NET 2

Reply
Thread Tools

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

 
 
=?Utf-8?B?U2FtdWVs?=
Guest
Posts: n/a
 
      08-08-2005
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.
 
Reply With Quote
 
 
 
 
Steven Cheng[MSFT]
Guest
Posts: n/a
 
      08-08-2005
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?= <(E-Mail Removed)>
| 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: <(E-Mail Removed)>
| 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.
|

 
Reply With Quote
 
 
 
 
Scott Allen
Guest
Posts: n/a
 
      08-08-2005
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:
http://odetocode.com/Blogs/scott/arc...8/01/2030.aspx

--
Scott
http://www.OdeToCode.com/blogs/scott/

On Mon, 8 Aug 2005 00:01:01 -0700, Samuel <(E-Mail Removed)>
wrote:

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


 
Reply With Quote
 
=?Utf-8?B?U2FtdWVs?=
Guest
Posts: n/a
 
      08-08-2005
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!
 
Reply With Quote
 
Steven Cheng[MSFT]
Guest
Posts: n/a
 
      08-09-2005
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?= <(E-Mail Removed)>
| References: <(E-Mail Removed)>
<(E-Mail Removed)>
| 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: <(E-Mail Removed)>
| 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!
|

 
Reply With Quote
 
=?Utf-8?B?U2FtdWVs?=
Guest
Posts: n/a
 
      08-09-2005
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_ma in_page")?

Thanks in advance!
 
Reply With Quote
 
Steven Cheng[MSFT]
Guest
Posts: n/a
 
      08-10-2005
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_ma in_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?= <(E-Mail Removed)>
| References: <(E-Mail Removed)>
<(E-Mail Removed)>
<(E-Mail Removed)>
<(E-Mail Removed)>
| 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: <(E-Mail Removed)>
| 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_ma in_page")?
|
| Thanks in advance!
|

 
Reply With Quote
 
Scott Allen
Guest
Posts: n/a
 
      08-10-2005
Sorry I missed your follow up question, Samual. Thanks to Steven for
following up. Let me know if I can answer anything else.

--
Scott
http://www.OdeToCode.com/blogs/scott/

On Mon, 8 Aug 2005 21:33:03 -0700, Samuel <(E-Mail Removed)>
wrote:

>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_m ain_page")?
>
>Thanks in advance!


 
Reply With Quote
 
 
 
Reply

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
Dynamically Added User Control Jenni.Haughton@googlemail.com ASP .Net 3 06-08-2007 05:58 PM
Dynamically added user control Chris ASP .Net 9 04-01-2007 06:34 PM
Dynamically added user control throws object not set andyrich_1@hotmail.com ASP .Net 1 01-03-2006 01:58 PM
Handle Event of Dynamically Added User Control Jon B ASP .Net 5 04-30-2005 12:33 PM
how come user control added to page doesn't get added to codebehind file? Bennett Haselton ASP .Net 1 11-08-2004 09:26 PM



Advertisments