When I run ASP.NET user control at runtime, why does it ignore the ASCX file?

I created a regular ASP.NET user control, including an ascx file. For example:

MyUserControl.ascx
MyUserControl.ascx.cs

Then I try to do the manual control at runtime using code similar to the following code:

var testMyUserControl = new MyUserControl();
var textWriter = 
    new HtmlTextWriter(
        new System.IO.StringWriter(new StringBuilder()));
testMyUserControl.RenderControl(textWriter);
Console.Write(textWriter.InnerWriter.ToString());

Nothing I put in an ASCX file seems to be static HTML or any .NET controls.

However, if I override the Render () method in the contents of the code and output manually, it does the rendering.

What am I missing here?

+3
source share
1 answer

. , :

UserControl myControl = (UserControl) Page.LoadControl("~/Controls/MyControl.ascx");
Page.Controls.Add(myControl);

, , Page.Controls placeholder:

<asp:Placeholder ID="myPlaceHolder" runat="server" />

:

myPlaceholder.Controls.Add(myControl);

, , .

<%@ Register TagPrefix="my" TagName="Control" Src="~/Controls/MyControl.ascx" %>

<my:Control ID="myControl" runat="server" />

: , .

+8

All Articles