如何以编程方式添加东西的ContentPlaceHolder? [英] How to programatically add stuff to 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 programatically.
-
我怎样才能添加控件ContentPlace持有人?
我检查了其他<一href=\"http://stackoverflow.com/questions/291037/set-aspcontentplaceholder-content-programatically\">answers,但我无法通过ID访问它。
How can I add controls to ContentPlace Holder? I checked other answers, but I cannot access it by its ID.
我应该使用多个ContentPlaceHolders从beggining?比方说,我想把电影。如果有只有一个所有的图像和描述和等级,矿石1的ContentPlaceHolder每一件事?
Should I use multiple ContentPlaceHolders from the beggining? 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.
推荐答案
老问题......但我只是碰到了这个问题,这是排名第一的职位,不断涌现在谷歌,所以图我加我因为别人的答案并没有我的情况下工作。
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.
下面是我做的,当一个普通&LT; ASP:内容
是行不通的(虽然在正常使用中,答案是@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):
母版有这个的ContentPlaceHolder
:
<asp:ContentPlaceHolder ID="ScriptsPlace" runat="server"></asp:ContentPlaceHolder>
不得不动态地从用户控件添加一些JavaScript。尝试使用的ContentPlaceHolder
直接给出这个错误:
分析器错误信息:内容控件必须是顶级的控制
在内容页或引用主嵌套母版页
页。
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.
所以我想从$ C $添加脚本C-后面。下面是的.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>
然后是code后面会发现,DIV,然后将其移动到的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);
}
}
我居然把一个自定义用户控件此code,我只是有我的普通用户控件从自定义用户控件继承,所以一旦我包的JavaScript /等用&LT; DIV ID =_ jsDiv=服务器&GT;
,自定义用户控件需要处理剩下的事情,我没有做在code任何用户控件的背后
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屋!