473,385 Members | 1,661 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,385 software developers and data experts.

ASP.NET 2.0 Dynamically Casting User Controls

Ron
Hi all,

I've come across a problem when casting user controls in ASP.NET 2.0,
hopefully someone can help. I have a master page with a user control on
it (myMenu) that I wish to programmatically access from a content page.
Below is how I am trying to do this (this would work in ASP.NET 1.1).

DynamicMenu myMenu =
(DynamicMenu)((MasterPage)this.Master).FindControl ("myMenu");

myMenu.initialise(enterpriseId);

However it doesn't work in 2.0 because I can't reference the user
control type DynamicMenu in code. I've tried adding the control to a
namespace, through which I could reference it, but apparently you can't
do this now.

Does anyone know how I could get this to work, or another way of doing
the same thing?

Thanks and Regards,

Ross

Jan 24 '06 #1
3 6179
Ron,

I have this and works okey.
cMenu NewMenu = ((cMenu)this.Master.FindControl("Menu1"));
Hope this helps.

"Ron" <ro******@web2office.biz> wrote in message
news:11*********************@z14g2000cwz.googlegro ups.com...
Hi all,

I've come across a problem when casting user controls in ASP.NET 2.0,
hopefully someone can help. I have a master page with a user control on
it (myMenu) that I wish to programmatically access from a content page.
Below is how I am trying to do this (this would work in ASP.NET 1.1).

DynamicMenu myMenu =
(DynamicMenu)((MasterPage)this.Master).FindControl ("myMenu");

myMenu.initialise(enterpriseId);

However it doesn't work in 2.0 because I can't reference the user
control type DynamicMenu in code. I've tried adding the control to a
namespace, through which I could reference it, but apparently you can't
do this now.

Does anyone know how I could get this to work, or another way of doing
the same thing?

Thanks and Regards,

Ross

Jan 24 '06 #2
You need to @reference the user control in your aspx file

<%@ Reference Control="blah.ascx" %>

http://msdn2.microsoft.com/en-us/library/w70c655a.aspx

it compiles the user control with the page and links it in, allowing you to
access the type in your codebehind.

Karl

--
http://www.openmymind.net/

"Ron" <ro******@web2office.biz> wrote in message
news:11*********************@z14g2000cwz.googlegro ups.com...
Hi all,

I've come across a problem when casting user controls in ASP.NET 2.0,
hopefully someone can help. I have a master page with a user control on
it (myMenu) that I wish to programmatically access from a content page.
Below is how I am trying to do this (this would work in ASP.NET 1.1).

DynamicMenu myMenu =
(DynamicMenu)((MasterPage)this.Master).FindControl ("myMenu");

myMenu.initialise(enterpriseId);

However it doesn't work in 2.0 because I can't reference the user
control type DynamicMenu in code. I've tried adding the control to a
namespace, through which I could reference it, but apparently you can't
do this now.

Does anyone know how I could get this to work, or another way of doing
the same thing?

Thanks and Regards,

Ross

Jan 24 '06 #3
Ron,

On your page which is utilizing the master page you can set the master
page's type. After doing so you can access all shared/public properties,
components, etc. on your master page referencing them using
Master.[control/property]

So, here's what you do:

Add this to your .aspx page (just below the page declaration):

<%@ Page Language="VB" MasterPageFile="~/MasterPage.master"
AutoEventWireup="false" CodeFile="Auction.aspx.vb" Inherits="Auction"
title="Untitled Page" %>
<%@ MasterType VirtualPath="~/MasterPage.master" %>
And here's an excerpt from an article about it:

Accessing a Master Page from Code
In addition to overriding content, it is possible for a Content Page to
programmatically access its Master Page. A Content Page creates a
strongly-typed reference to the Master Page using the <%@ MasterType %>
directive, specifying the virtual path to the master page:

<%@ MasterType VirtualPath="Site.master" %>

The Content Page can then reference the Master Page using the Master
property of the Page class:

C#
Master.FooterText = "This is a custom footer";
AdRotator ad = (AdRotator)Master.FindControl("MyAdRotator");

VB
Master.FooterText = "This is a custom footer"
Dim ad As AdRotator = Master.FindControl("MyAdRotator")
--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
"Ron" <ro******@web2office.biz> wrote in message
news:11*********************@z14g2000cwz.googlegro ups.com...
Hi all,

I've come across a problem when casting user controls in ASP.NET 2.0,
hopefully someone can help. I have a master page with a user control on
it (myMenu) that I wish to programmatically access from a content page.
Below is how I am trying to do this (this would work in ASP.NET 1.1).

DynamicMenu myMenu =
(DynamicMenu)((MasterPage)this.Master).FindControl ("myMenu");

myMenu.initialise(enterpriseId);

However it doesn't work in 2.0 because I can't reference the user
control type DynamicMenu in code. I've tried adding the control to a
namespace, through which I could reference it, but apparently you can't
do this now.

Does anyone know how I could get this to work, or another way of doing
the same thing?

Thanks and Regards,

Ross

Jan 24 '06 #4

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

Similar topics

7
by: Tim T | last post by:
Hi, I have the need to use dynamically loaded user controls in a webform page. I have the controls loading dynamically, and that part works fine. this is the code used in a webform to dynamically...
0
by: smccreadie | last post by:
I'm attempting to convert an application from 1.1 to 2.0 and am running into some issues with dynamically loaded user controls. Our 1.1 code is Private uc1 As Control =...
1
by: npaulus | last post by:
Hi, I am trying to dynamically add user controls on to my web form but for some reason my form isnt displaying the user control. form1.cs: using System; using System.Drawing; using...
1
by: Bob Rock | last post by:
Hello, I'm new to ASP.NET and I've been looking into the topic of dynamically loading (typically accomplished with a LoadControl followed by a MyControl.Controls.Add()) both user controls and...
2
by: HDI | last post by:
Hi, I used this a long time ago in VB6 How can I dynamically add user controls to a listview ? Thx
6
by: | last post by:
I have made some user controls with custom properties. I can set those properties on instances of my user controls, and I have programmed my user control to do useful visual things in response to...
0
abehm
by: abehm | last post by:
I have a couple placeholders in which I want to dynamically load user controls into a multi-pageview based on db data. The database returns the filename of the user control (i.e....
1
by: Dinu | last post by:
How to Dynamically program User controls in a Master page from a content page Thanks
1
by: Christian Resma Helle | last post by:
Hey guys, I'm working on an AJAX Enabled ASP.NET Web application. I have a TreeView web control and an PlaceHolder web control. My PlaceHolder is inside an UpdatePanel and AsyncPostBacks are...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
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...

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.