0

I created this asp.net user control (just a sample for this stack overflow post)

HTML:

<%@ Control Language="vb" AutoEventWireup="false" CodeBehind="ucBTN.ascx.vb" Inherits="EBC_SAK_MVV_BTN.web.ucBTN" %>

<div>
    <asp:Button ID="btnLeft" runat="server" Width="200" Height="100" CssClass="btn btn-sq-lg btn-primary" Text="Button-Left" />
    <asp:Button ID="btnMiddle" runat="server" Width="200" Height="100" CssClass="btn btn-sq-lg btn-primary" Text="Button-Middle" />
    <asp:Button ID="btnRight" runat="server" Width="200" Height="100" CssClass="btn btn-sq-lg btn-primary" Text="Button-Right" />
</div>

Code behind:

Public Class ucBTN
    Inherits System.Web.UI.UserControl
    Public Property TextLeft As String
    Public Property TextMiddle As String
    Public Property TextRight As String
End Class

When I use this user control in a page it works (test.aspx):

<%@ Page Title="" Language="vb" AutoEventWireup="false" MasterPageFile="~/EBC.Master" CodeBehind="TEST.aspx.vb" Inherits="EBC_SAK_MVV_BTN.web.TEST" %>

<%@ Register Src="~/ucBTN.ascx" TagPrefix="uc1" TagName="ucBTN" %>
<uc1:ucBTN runat="server" ID="myBTN" />

But when I add a panel to my page and add my user control to this panel it keeps blank:

HTML:

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

Code behind:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim BTN As New ucBTN
BTN.TextLeft = "LEFT"
BTN.TextMiddle = "MIDDLE"
BTN.TextRight = "RIGHT"
Panel1.Controls.Add(BTN)
End Sub

I want to create dozends of these controls during runtime - how do I add them to my panel?

1 Answer 1

2

Use PlaceHolder instead of panel

in user control use:

  <asp:PlaceHolder ID="PlaceHolder1" runat="server"> 

in code behind use:

 PlaceHolder1.Controls.Add(BTN)
Sign up to request clarification or add additional context in comments.

2 Comments

Well, does not work either - although I understand that I have to use a PlaceHolder if I want to add controls. When adding a new button AND my user control to the placeholder only the button is shown.
string _ascxPath = @"~/ucBTN.ascx"; Control control = Page.LoadControl(_ascxPath); Panel1.Controls.Add(control);

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.