Content control not available on content page?

My content page is as follows:

<%@ 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>

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

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

The problem is that the control is Content2not displayed by the compiler, and I get a message that there is no directive or assembly directive.

Any solution?

+3
source share
2 answers

, asp:Content, , , . ASP asp:Content ContentPlaceholder .

MSDN : . Content ContentPlaceHolder.

, , ContentPlaceholder . - :

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

, ContentPlaceHolderID, ID asp:Content.

+8

, . ,

<%@ 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:Placeholder runat="server" ID="Content1Controls" />
</asp:Content>

..

  protected void Page_Load(object sender, EventArgs e)
  {
     Content1Controls.Controls.Add(...
  }
+2

Source: https://habr.com/ru/post/1774042/


All Articles