How to Determine asp: control ID Name at Runtime in ASP.NET 2.0?

G

Guest

I am working on developing a generic Web framework using Master Pages in
ASP.NET 2.0. What I have done is created a PageRenderer class which has a
public method which will retrieve the path of the content I want to execute
based on the name of the asp:Content control. As shown in the code snippet
below, to get the content I want to display, I call the GetContentPagePath
public method in PageRenderer passing a string duplicating the value in the
ContentPlaceHolderID, i.e.:
Server.Execute(PageRenderer.GetContentPagePath("ContentTopMenu"));

However, I would prefer not to use a hard coded value, but rather
programmatically determine ContentPlaceHolderID value for the current
asp:Content control and pass that as a parameter. By doing this, I can use
same method call into each asp:Content control in the .master file. However,
my initial research didn't yield a way to programmatically at runtime
determine the ContentPaceHolderID value of the current actively asp:Content
control. The Master object looked promising, however, as far as I could
tell, there wasn't anything to indicate which was the currently actively
control.

In playing around in the debugger, I saw that in the Locals window, there
was parameterContainer object available and it had a property called ID which
has the value that I need. However, I can't find this object documented and
I am hesitant to use even though it provides me what I need. Does anyone
know of a way to determine at runtime the ContentPlaceHolderID value of the
asp:Content control that is currently running the ASPX code?

Thanks.

<%@ Page Language="C#"
MasterPageFile="~/_framework/masterlayouts/masterlayout1/masterlayout1.master"
AutoEventWireup="true" CodeFile="masterlayout1_content.aspx.cs"
Inherits="_framework_masterlayouts_masterlayout1_content" %>

<asp:Content ID="Content1" ContentPlaceHolderID="ContentTopMenu"
Runat="Server">
<%
Server.Execute(PageRenderer.GetContentPagePath("ContentTopMenu"));
OR
Server.Execute(PageRenderer.GetContentPagePath(parameterContainer.ID));

%>
</asp:Content>
 
G

Guest

Phillip,

Thanks for responding to my post. The design that I am working on requires
only a single "master" content page to be associated with each master page.
In other words, although I have may 100 pages that that use a master layout
page (.master file), I have only one "master" content page (.aspx) associated
with that master page. This single "master" content page is then used to
dynamically build the 100 content pages that come from that master layout.
The obvious benefit of this is that if there is a significant change to the
master layout, like the addition or removal of asp:Content controls, I don't
have to change 100 individual content .aspx pages, I just have to change the
single "master" content page.

Another beauty of this design is that the code for the "master" content page
for any master layout is identical. The only thing that differs from the
"master" content pages is the name and number of the asp:Content controls.
So you can see why I would prefer to use be able to determine the asp:Content
control name at run time instead of using a hard code strings.

As I noted, it is possible to get the asp:Content unique ID at runtime
because there is a parameterContainer object that is available that has
exactly what I need. However, I couldn't find this object documented and I
am hesitant to use it because who know if in future implementations of
ASP.NET if this object will continue to exist.

Thanks. Gary
 
S

Steven Cheng[MSFT]

Hi Gary,

So at what time are you going to do the customization? Based on my
research, the master page only expose a Contenttemplate collection which is
the ITemplate implemenation of each associated Content control(in the
content page). And master page also doesn't provide a event to capture each
contentplaceholder's initialization. One possible means is add the Init or
Load event handler for each contentPlaceHolder in the master page, then we
can do some work at each contentplaceholder's initialization time:

<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server"
OnInit="ContentPlaceHolder1_Init" OnLoad="ContentPlaceHolder1_Load">


Regards,

Steven Cheng
Microsoft Online Community Support


==================================================

When responding to posts, please "Reply to Group" via your newsreader so
that others may

learn and benefit from your issue.

==================================================


This posting is provided "AS IS" with no warranties, and confers no rights.
 

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,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top