문제

Can someone help me to create my custom web user control to be render into a placeholder dynamically?

I just created a web user control which does not have code in the server side just design in the aspx because all the functionallity is client-side.

Web Control

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="WebControlDemo.ascx.cs" Inherits="Project1.Web.UI.WebControlDemo" %>
<div>
Blah blah blah blah
</div>

In another page

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

But I can't see anything. So what so I need to add that can finally becomes rendered?

도움이 되었습니까?

해결책

You shouldn't create User controls manually in code. You should use LoadControl method from Page object. Then it will initialize it correctly. Like this

protected void button1_Click(object sender, EventArgs e)
{
   WebControlDemo wcd = this.LoadControl("~/SomePath/WebControlDemo.ascx") as WebControlDemo;
   this.placeHolder1.Controls.Add(wcd);
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top