如何将Web用户控件呈现到PlaceHolder中

Max*_*mus 1 c# asp.net user-controls render placeholder

有人可以帮助我创建我的自定义Web用户控件,以动态呈现到占位符?

我刚刚创建了一个Web用户控件,它在服务器端没有代码只是在aspx中设计,因为所有的功能都是客户端的.

网络控制

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="WebControlDemo.ascx.cs" Inherits="Project1.Web.UI.WebControlDemo" %>
<div>
Blah blah blah blah
</div>
Run Code Online (Sandbox Code Playgroud)

在另一页

protected void button1_Click(object sender, EventArgs e)
{
   WebControlDemo wcd = new WebControlDemo();
   this.placeHolder1.Controls.Add(wcd);
}
Run Code Online (Sandbox Code Playgroud)

但我看不到任何东西.那么我需要添加的内容最终会被渲染出来?

Ser*_*nov 7

您不应该在代码中手动创建用户控件.您应该使用Page对象中的LoadControl方法.然后它会正确初始化它.像这样

protected void button1_Click(object sender, EventArgs e)
{
   WebControlDemo wcd = this.LoadControl("~/SomePath/WebControlDemo.ascx") as WebControlDemo;
   this.placeHolder1.Controls.Add(wcd);
}
Run Code Online (Sandbox Code Playgroud)