473,399 Members | 3,888 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

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

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.
Nov 19 '05 #1
7 3170
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?= <pr********@nospam.nospam>
| 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: <7D**********************************@microsoft.co m>
| 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.
|

Nov 19 '05 #2
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 <pr********@nospam.nospam>
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.


Nov 19 '05 #3
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!
Nov 19 '05 #4
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?= <pr********@nospam.nospam>
| References: <7D**********************************@microsoft.co m>
<k5********************************@4ax.com>
| 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: <00**********************************@microsoft.co m>
| 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!
|

Nov 19 '05 #5
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!
Nov 19 '05 #6
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?= <pr********@nospam.nospam>
| References: <7D**********************************@microsoft.co m>
<k5********************************@4ax.com>
<00**********************************@microsoft.co m>
<AR**************@TK2MSFTNGXA01.phx.gbl>
| 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: <DC**********************************@microsoft.co m>
| 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!
|

Nov 19 '05 #7
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 <pr********@nospam.nospam>
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!


Nov 19 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Pham Nguyen | last post by:
Has anyone seen an example of a textbox server control that has built-in client-side validation? I'd like to build a server control that extends the System.Web.UI.WebControls.TextBox class to allow...
3
by: Eric | last post by:
I have built a composite user web control that I want to create dynamically. The form will contain a variable number of these controls and as well some of the contents of the user web control...
4
by: Jordan | last post by:
I need to dynamically add an ImageButton control to a user control and and do some server-side processing when the user clicks it. While I the ImageButton is added to the user control at runtime,...
9
by: Sridhar | last post by:
Hi, I have created a web page which includes a place holder. I also have a dropdown list in that webpage. when I select one of the choices in that dropdown list, It will load a user control...
9
by: Chris | last post by:
I am dynamically adding a user control to each row in a gridview. The reason I am doing it dynamically is the user control is different depending on certain data in the gridview. The gridview...
1
by: jelle.huygen | last post by:
Hello, I have a problem in ASP.NET 2.0 with the viewstate of my dynamically added user control. I have reproduced the problem with a very simple user control and a very simple page. On my...
4
by: Randy | last post by:
My situation is that I have a form on which a number of textboxes and comboboxes are added dynamically based on interaction with the user. As these controls are added, they are given names based on...
4
by: =?Utf-8?B?UmljaEI=?= | last post by:
I am trying to create a project using the ASP.NET AJAX accordion control. I would like to dynamically add panes to the control with a form template added when the pane is added. I have tried...
7
by: RichB | last post by:
I am trying to get to grips with the asp.net ajaxcontrol toolkit, and am trying to add a tabbed control to the page. I have no problems within the aspx file, and can dynamically manipulate a...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.