is there a way to get element ID of an Content element loaded in the master page PlaceHolder?
For EX: My master page,
<form id="form1" runat="server">
<div>
<asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>
</div>
</form>
Below is Other Page using Master page:
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
I am the child page
<asp:Label ID="lbl" runat="server"></asp:Label>
</asp:Content>
how to get the Content ID ("Content2") in Master PageLoad.
Can Any one please sugg开发者_如何学Cest.
Thanks in advance
If you've overriden MasterPage then you can access a protected ContentPlaceHolders collection:
public class YourMasterPage : MasterPage
{
// in Page_Load try out
foreach(var placeHolder in this.ContentPlaceHolders)
{
var contentPlaceHolder = placeHolder as ContentPlaceHolder;
if(contentPlaceHolder != null)
{
Debug.WriteLine("ID: " + contentPlaceHolder.ID);
Debug.WriteLine("Client Id" + contentPlaceHolder.ClientID);
}
}
}
What you want to do is not possible. The asp:Content
controls are not actually part of the page's control hierarchy, as it says in this MSDN page:
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.
That's why I asked about whether your ultimate goal was to access the controls in that section. If it is, you can get to them through the ContentPlaceHolder
control, because at runtime all the controls that were in the Content section are inside of it. You can do this in your masterpage:
Label lbl = ContentPlaceHolder1.FindControl("lbl") As Label;
You can get like..
((ContentPlaceHolder)Master.FindControl("ContentPlaceHolder1")).ClientID
精彩评论