ASP.NET 2.0 Dynamically Casting User Controls

R

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
 
M

Maarten

Ron,

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

S. Justin Gengo [MCP]

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
 

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,989
Messages
2,570,207
Members
46,783
Latest member
RickeyDort

Latest Threads

Top