C# 如何以编程方式向 contentPlaceHolder 添加内容?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10490417/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 14:02:52  来源:igfitidea点击:

How to programmatically add stuff to contentPlaceHolder?

c#asp.netmaster-pagescontentplaceholder

提问by Ciprian Tomoiag?

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.

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

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

  2. 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?

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

  2. 我应该从一开始就使用多个 ContentPlaceHolders 吗?假设我想放电影。是否应该只有一个包含所有图像、描述和评级,还是每个内容都有一个 ContentPlaceHolder?

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

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

回答by JayC

What normally happens is

通常发生的是

  1. you set up your master pages with the proper html and ContentPlaceHolders
  2. you create pages based off that master page. If you use Visual Studio, and tell it to create a new page based upon a existing Master page, it will add the Contentareas for you.
  3. you add things to the Content areas in the newly created page.
  1. 您使用适当的 html 和ContentPlaceHolders设置母版页
  2. 您根据该母版页创建页面。如果您使用 Visual Studio,并告诉它基于现有母版页创建一个新页面,它将Content为您添加区域。
  3. 您将内容添加到新创建的页面中的内容区域。

If you want to dynamically add controls to the master (or any) page, you could add controls to any existing control. If it shouldn't be wrapped in any way, just add a Placeholder(it is an asp.net control).

如果您想动态地向母版(或任何)页面添加控件,您可以向任何现有控件添加控件。如果它不应该以任何方式包装,只需添加一个Placeholder(它是一个 asp.net 控件)。

回答by Registered User

I did like this

我喜欢这个

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
<asp:Literal ID="jsstuff" runat="server"></asp:Literal>
</asp:Content>

And this went into code behind:

这进入了后面的代码:

string stuff =  @"<script type=""text/javascript"">
                                        var searchBox = 0;
                                         var currentCountry = '';
                                         </script>";
                    jsstuff.Text = stuff;

回答by MikeSmithDev

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.

老问题......但我刚刚遇到了这个问题,这是谷歌上不断出现的#1帖子,所以我想我会添加我的答案,因为其他人在我的情况下不起作用。

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

这是当常规<asp:Content无法正常工作时我是如何做到的(尽管在正常使用中,答案@JayC 是您如何做到的):

MasterPage has this ContentPlaceHolder:

MasterPage 有这个ContentPlaceHolder

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

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

必须从用户控件动态添加一些 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.

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

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

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

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);
    }
}

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.

更新:所以事实证明我不得不在比我预期的更多的地方使用它,并最终使用了一种更灵活/可读的方式。在用户控件本身中,我只是将 javascript 和其他任何需要使用常规div.

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

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

然后后面的代码会找到那个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);
    }
}

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.

我实际上把这段代码放在一个自定义用户控件中,我只是让我的常规用户控件从自定义用户控件继承,所以一旦我用 a 包装了 javascript/etc <div id="_jsDiv" runat="server">,自定义用户控件就会处理其余的事情,我不不必在用户控件背后的代码中做任何事情。

回答by Mahir

If the namespace for content Page and Master page is not same then the content page control not accessible in Codebehind in content page.

如果内容页和母版页的命名空间不同,则内容页中的代码隐藏中无法访问内容页控件。

Also, check your designer files. if the control not listed in designer file then delete the file and recreate (project->convert to web application)

另外,请检查您的设计器文件。如果控件未在设计器文件中列出,则删除该文件并重新创建(项目-> 转换为 Web 应用程序)