1

I have a user control that programmatically includes a second user control several times, but the end result is that no HTML is generated for the controls at all.

Default.aspx

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Default.aspx.vb" Inherits="TestApplication._Default" %>
<%@ Register TagName="Ten" TagPrefix="me" Src="~/Ten.ascx" %>

<html>
<head runat="server"></head>
<body>
    <form id="form1" runat="server">
        <me:Ten ID="thisTen" runat="server" />
    </form>
</body>
</html>

Ten.ascx

<%@ Control Language="vb" AutoEventWireup="false" CodeBehind="Ten.ascx.vb" Inherits="TestApplication.Ten" %>

<asp:Panel ID="List" runat="server"></asp:Panel>

Ten.ascx.vb

Public Class Ten
    Inherits System.Web.UI.UserControl
    Protected Sub Page_Init() Handles Me.Init
        For I As Integer = 0 To 11
            List.Controls.Add(New One(I.ToString))
        Next
    End Sub
End Class

One.ascx

<%@ Control Language="vb" AutoEventWireup="false" CodeBehind="One.ascx.vb" Inherits="TestApplication.One" %>

<asp:Button ID="OneButton" Text="Press ME!" runat="server" />

One.ascx.vb

Public Class One
    Inherits System.Web.UI.UserControl
    Private _number As String
    Sub New(ByVal number As String)
        _number = number
    End Sub
    Protected Sub OneButton_Click(ByVal sender As Object, ByVal e As EventArgs) Handles OneButton.Click
        Dim script As String = "<script type=""text/javascript"">" +
                               "alert('Button " + _number + "');" +
                               "</script>"
        ScriptManager.RegisterStartupScript(Me, Me.GetType(), "ServerControlScript", script, True)
    End Sub
End Class

Edit:

Using load control (Ten.aspx)

Dim p() As String = {I.ToString}
Dim o As One = Me.LoadControl(New One("").GetType, p)
List.Controls.Add(o)

Edit 2:

Dim o As One = Me.LoadControl("~/One.ascx")
o._number = I.ToString
List.Controls.Add(o)

1 Answer 1

1

I wouldn't perform this operation in the OnInit event, but this may or may not be related to your issue. Instead, I would load the controls in the OnLoad event.

However, using the new keyword is not typically how user controls are loaded. Sorry, but I only know C# and don't know the exact translation:

In C#:

One one = (One)this.LoadControl("~/Controls/One.ascx");

Does this look right in VB.NET?

Dim one As One = Me.LoadControl("~/Controls/One.ascx")

You'll likely have to delete the constructor and just set the property after you load the control.

Sign up to request clarification or add additional context in comments.

2 Comments

Yes, this would work except that the constructor has a parameter. This causes a HttpCompileException. I was able to get past this by passing in the parameters in an array. However this does not solve the problem there is still no html generated.
One note, if you want to handle events generated in the child control from the parent, you have to use the Page_Init event to create them. Otherwise the Page_Load event will work fine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.