473,471 Members | 1,744 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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

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("Co ntentTopMenu"));

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_c ontent" %>

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

%>
</asp:Content>

--
Gary
Mar 20 '06 #1
3 11555
"A Content control is not added to the control hierarchy at runtime. Instead,
the contents within the Content control are directly merged into the
corresponding ContentPlaceHolder control."
http://msdn2.microsoft.com/en-us/lib...nt(VS.80).aspx

If you want to decide on the content based on a parameter associated with
the page why not use the page URL since it is unique and that way you avoid
hard coding IDs?
--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com
"Developer in California" wrote:
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("Co ntentTopMenu"));

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_c ontent" %>

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

%>
</asp:Content>

--
Gary

Mar 21 '06 #2
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

"Phillip Williams" wrote:
"A Content control is not added to the control hierarchy at runtime. Instead,
the contents within the Content control are directly merged into the
corresponding ContentPlaceHolder control."
http://msdn2.microsoft.com/en-us/lib...nt(VS.80).aspx

If you want to decide on the content based on a parameter associated with
the page why not use the page URL since it is unique and that way you avoid
hard coding IDs?
--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com
"Developer in California" wrote:
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("Co ntentTopMenu"));

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_c ontent" %>

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

%>
</asp:Content>

--
Gary

Mar 21 '06 #3

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.

Mar 23 '06 #4

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

Similar topics

0
by: ann | last post by:
Hi, I am trying to add an ActiveX control on my ASP.NET web form (the language for code behind is C#). I have some client-side vbscript to handle events from the html input buttons. In the...
8
by: dikeyus | last post by:
I must use a component in my ASP.NET project but couldn't do it. There is a mistake anywhere . I use Microsoft Visual Studio.NET 2003 I'm making a new project and add MSFlexGrid to my WebForm1.aspx...
3
by: Jordan | last post by:
I am dynamically inserting an html <input> tag as text (equivalent of an image button) into a page via a Literal control. Something like this gets inserted: <input type="image"...
2
by: Kamal Ahmed | last post by:
Hi all, I want to execute a String that is generated at Runtime and fills data in DropDownList Controls at Runtime. I want to do this at server side. As Eval functions works as client side...
5
by: Ole M | last post by:
I'm having some trouble using the ObjectDataSource in ASP.NET 2.0. I have a wrapper that contains the static methods for Select and Update. The Update-method takes the business object as...
0
by: shamirza | last post by:
· When was .NET announced? Bill Gates delivered a keynote at Forum 2000, held June 22, 2000, outlining the .NET 'vision'. The July 2000 PDC had a number of sessions on .NET technology, and...
0
by: shamirza | last post by:
· What is view state and use of it? The current property settings of an ASP.NET page and those of any ASP.NET server controls contained within the page. ASP.NET can detect when a form is requested...
1
by: Cgf39 | last post by:
We've noticed some strange errors on our production servers running IIS Event Type: Warning Event Source: ASP.NET 2.0.50727.0 Event Category: Web Event Event ID: 1309 Date: 17-04-2007 Time:...
4
by: Bill Fuller | last post by:
I am trying to determine the type for ActiveControls using 3rd party controls (Infragistics in this case) during runtime and getting a rather odd return type at runtime for the UltraWinEditor. ...
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.