如何以编程方式将内容添加到contentPlaceHolder? [英] How to programmatically add stuff to contentPlaceHolder?

查看:92
本文介绍了如何以编程方式将内容添加到contentPlaceHolder?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个母版页,我的所有页都在继承它. 对于格式化,我想将一个页面与另一个页面的内容放置在ContentPlaceHolder中.

I have a master page and all of my pages are inheriting it. For formatting, I thought to place the content that differs from one page to another in a ContentPlaceHolder.

现在,如何将所有内容插入其中?由于我计划用数据库中的内容填充ContentPlaceHolder,所以我想我必须以编程方式进行操作.

Now, how can I insert everything into that? Since I am planning to populate the ContentPlaceHolder with stuff from a database I suppose I will have to do it programmatically.

  1. 如何将控件添加到ContentPlace Holder? 我检查了其他答案,但是我无法通过其ID来访问它.

  1. How can I add controls to ContentPlace Holder? I checked other answers, but I cannot access it by its ID.

我应该从一开始就使用多个ContentPlaceHolders吗?假设我想放电影.所有图像,描述和等级是否应该只有一个,或者每件事都需要一个ContentPlaceHolder?

Should I use multiple ContentPlaceHolders from the beginning? Let's say I want to put movies. Should there be only one with all the images and descriptions and ratings, ore one ContentPlaceHolder for each thing?

我对其他解决方案持开放态度,因为我没有使用ASP的经验.

I am opened to other solutions, as I have no experience with ASP.

推荐答案

旧问题...但是我遇到了这个问题,这是Google上排名第一的帖子,所以我要添加我的回答,因为其他人在我的案例中不起作用.

Old question... but I just ran into this issue and this was the #1 post that kept coming up on Google, so figure I'd add my answer since the others didn't work in my case.

这是当常规<asp:Content无法正常工作时的处理方式(尽管在正常使用中,答案@JayC是您的处理方式):

Here is how I did it when a regular <asp:Content wouldn't work (though in normal use, the answer @JayC is how you do it):

MasterPage具有此ContentPlaceHolder:

MasterPage has this ContentPlaceHolder:

<asp:ContentPlaceHolder ID="ScriptsPlace" runat="server"></asp:ContentPlaceHolder>

必须从用户控件动态添加一些JavaScript.尝试直接使用ContentPlaceHolder会出现此错误:

Had to dynamically add some JavaScript from a User Control. Trying to use the ContentPlaceHolder directly gives this error:

解析器错误消息:内容控件必须是顶级控件 在内容页面或引用母版的嵌套母版页中 页面.

Parser Error Message: Content controls have to be top-level controls in a content page or a nested master page that references a master page.

所以我想从后面的代码中添加脚本.这是.ascx文件的页面加载:

So I wanted to add the script from the code-behind. Here is the Page Load for the .ascx file:

protected void Page_Load(object sender, EventArgs e)
{
    ContentPlaceHolder c = Page.Master.FindControl("ScriptsPlace") as ContentPlaceHolder;
    if (c != null)
    {
        LiteralControl l = new LiteralControl();
        l.Text="<script type=\"text/javascript\">$(document).ready(function () {js stuff;});</script>";
        c.Controls.Add(l);
    }
}

更新:因此,事实证明,我不得不在比我预期的更多的地方使用它,并最终使用了一种更加灵活/可读性强的方法.在用户控件本身中,我只是包装了javascript和需要用常规div移动的其他任何东西.

UPDATE: So it turns out I had to use this in more places than I expected, and ended up using a way that was much more flexible / readable. In the user control itself, I just wrapped the javascript and anything else that needed to be moved with a regular div.

<div id="_jsDiv" runat="server">
    $(document).ready(function() {
         //js stuff
    });
    Other server controls or HTML junk
</div>

然后,后面的代码将找到该div,然后将其移至ContentPlaceHolder.

And then the code behind will find that div, and then move it into the ContentPlaceHolder.

protected void Page_Load(object sender, EventArgs e)
{
    ContentPlaceHolder c = Page.Master.FindControl("ScriptsPlace") as ContentPlaceHolder;
    HtmlGenericCOntrol jsDiv = this.FindControl("_jsDiv") as HtmlGenericControl;
    if (c != null && jsDiv != null)
    {
        c.Controls.Add(jsDiv);
    }
}

我实际上将此代码放在了自定义用户控件中,而我的常规用户控件仅继承自该自定义用户控件,因此,一旦用<div id="_jsDiv" runat="server">包装javascript/etc,该自定义用户控件就可以处理其余的操作,我无需在用户控件后面的代码中做任何事情.

I actually put this code in a custom user control, and I just have my regular user controls inherit from the custom user control, so once I wrap the javascript/etc with a <div id="_jsDiv" runat="server">, the custom user control takes care of the rest and I don't have to do anything in the code behind of the user control.

这篇关于如何以编程方式将内容添加到contentPlaceHolder?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆