开发者

Can a ContentPlaceHolder be placed in a Web User Control?

I want to do something like this:

I got this User Control called Base:

<div>
some content...

<asp:ContentPlaceHolder id="baseContentPlaceHolder" runat="server"></asp:ContentPlaceHolder>

</div>

Then i inherit the Base in another User Control and add stuff to the content:

<asp:Content id="subBase" contentplaceholderid="baseContentPlaceHolder" runat"server">
    stuff to be added...
</asp:Content>
开发者_如何学Go

Is it possible to do with ContentPlaceHolder ?

How can this behavior be solved?


I don't think you can do that.

A ContentPlaceHolder can only go in the MasterPage itself.

In the pages that inherit from the MasterPage, you put in a Content tag.

My best guess would be to add in a PlaceHolder, expose that on the control, and then add in your stuff to that.


You can loop through the Master pages until you find the ContentPlaceHolder and insert it there:

var scriptTag = new HtmlGenericControl("script");
scriptTag.Attributes.Add("type", "text/javascript");
scriptTag.Attributes.Add("src", Page.ResolveUrl($"~/somescript.js"));

Control control = null;
MasterPage master = Page.Master;

while (master != null)
{
    control = master.FindControl("FooterContent");
    if (control == null)
        master = master.Master;
    else
        break;
}
control.Controls.Add(scriptTag);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜