内容控制不是从内容页accessbile? [英] Content control not accessbile from content page?

查看:194
本文介绍了内容控制不是从内容页accessbile?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的内容网页看起来是这样的:

My content page looks like this:

<%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Basket.aspx.cs" Inherits="Basket" Title="Untitled Page" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
</asp:Content>

现在,我想动态地添加一些控件,当页面加载的内容,所以我尝试以下code:

Now, I would like to add some controls dynamically to the content when page loads, so I am trying the following code:

  protected void Page_Load(object sender, EventArgs e)
  {
     Content2. // I want to add controls to it dynamically
  }

问题是,内容2 控制不是由编译器可见光和我正在错过指令或程序集引用错误。

The problem is that the Content2 control is not visible by the compiler and I am getting error about missing directive or assembly reference.

任何解决方案?

推荐答案

您不能得到一个参考的原因 ASP:内容控件是因为它不停留在当页面与母版相结合。基本上,ASP需要从内这些 ASP所有的控件:内容部分,并让他们的的ContentPlaceHolder 控制孩子的母版内。

The reason you can't get a reference to that asp:Content control is because it does not stay around when the page is combined with the masterpage. Basically ASP takes all the controls from inside these asp:Content sections and makes them children of the ContentPlaceholder controls inside the masterpage.

由于 MSDN 说:一个内容控制不被添加到在运行时控制层次。而是内容控件中的内容直接合并到相应的ContentPlaceHolder控件。

这意味着,如果你想要更多的控件添加到该部分中,您将获得在母版的的ContentPlaceHolder 控件的引用,并将它们添加到它。是这样的:

That means that if you want to add more controls to that section, you will have to get a reference to the ContentPlaceholder control in the masterpage and add them to it. Something like:

ContentPlaceHolder myContent = (ContentPlaceHolder)this.Master.FindControl("ContentPlaceHolder1");
myContent.Controls.Add(??);

注意,你所使用的 ContentPlaceHolderID 值,而不是 ID 的asp:内容部分。

这篇关于内容控制不是从内容页accessbile?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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