Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Controls do not appear on dynamically created user control

I have user control named DateTimeUC which has two textboxes on its markup:

<asp:TextBox ID="dateTextBox" runat="server"></asp:TextBox>
<asp:TextBox ID="timeTextBox" runat="server"></asp:TextBox>

I am dynamically creating this control in another user control:

Controls.Add(GenerateDateTime(parameter));
private DateTimeUC GenerateDateTime(SomeParameter parameter)
{
    DateTimeUC uc = new DateTimeUC();
    uc.ID = parameter.Name;
    return uc;
}

But when I render the page, DateTimeUC renders nothing. I checked it like this:

protected override void Render(HtmlTextWriter writer)
{
    base.Render(writer);

    StringBuilder builder = new StringBuilder();
    StringWriter swriter = new StringWriter(builder);
    HtmlTextWriter hwriter = new HtmlTextWriter(swriter);
    base.Render(hwriter);
    string s = builder.ToString();
}

s is empty and Controls.Count is 0. What am I doing wrong?

like image 746
Serhat Ozgel Avatar asked Sep 24 '08 14:09

Serhat Ozgel


1 Answers

You must use the LoadControl( "your_user_control_app_relative_path.ascx" ) method instead of "DateTimeUC uc = new DateTimeUC();"

like image 163
TcKs Avatar answered Oct 31 '22 19:10

TcKs