troubleshooting Question

Design time controls return nothing / null at runtime in ascx.

Avatar of cme0015
cme0015 asked on
.NET ProgrammingASP.NET
5 Comments1 Solution435 ViewsLast Modified:
Good afternoon,

Here is my problem.  I am working on a site wherein I need to be able to add complex objects at run-time to the aspx page.  To that end, I have been working with ASCX files and it seems to be the right direction, however, elements that I add at design time return nothing once I call a new UserControl from codebehind.  In the sample below, the control never shows in the browser and the uc.Label1 = nothing at runtime, please help and thank you in advance.

Simple code-sample below.

WebUserControl1.ascx
<%@ Control Language="vb" AutoEventWireup="false" CodeBehind="WebUserControl1.ascx.vb" Inherits="WebApplication3.WebUserControl1" %>
<asp:Label ID="Label1" runat="server" Text="Hi am on a control"></asp:Label>

Default.aspx.vb
Public Class _Default
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
            Dim uc As New WebUserControl1
            Panel1.Controls.Add(uc)
        End If
    End Sub

End Class
Default.aspx
<%@ Page Title="Home Page" Language="vb" MasterPageFile="~/Site.Master" AutoEventWireup="false"
    CodeBehind="Default.aspx.vb" Inherits="WebApplication3._Default" %>

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">

    <asp:Panel ID="Panel1" runat="server">
    </asp:Panel>
</asp:Content>
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 5 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 5 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros