Avatar of miguel_j
miguel_j
Flag for Australia asked on

how do i dynamically add a user control

hello

this website applies a different masterpage based on the domain.
once the masterpage is applied i want to add different content templates that load different user controls dynamically

error is in my class file

Compilation Error Type 'BreadCrumb' is not defined.

below is my code

--------------
test.aspx

<%@ Page Language="vb" EnableViewState="false" AutoEventWireup="false" MasterPageFile="~/MasterPages/Website.Master" CodeBehind="test.aspx.vb" Inherits="Website.test" %>

<%@ Reference Control="~/Controls/BreadCrumb.ascx" %>

---------------
test.aspx.vb

Public Partial Class test
    Inherits System.Web.UI.Page

    Protected Sub Page_PreInit(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreInit

        'code to determine domain and apply correct masterpage (irrelevant to the question)
        Dim m As New ApplyMasterPage
        Me.Page.MasterPageFile = m.MasterLocation.ToString()

        MyBase.AddContentTemplate("BreadCrumbContent", New ApplyBreadCrumbContent)

    End Sub

End Class

--------------
App_Code/ApplyBreadCrumbContent.vb

Public Class ApplyBreadCrumbContent
    Implements System.Web.UI.ITemplate

    Dim b As BreadCrumb 'error here

    Dim p As Page

    Sub InstantiateIn(ByVal container As Control) Implements System.Web.UI.ITemplate.InstantiateIn

        b = CType(p.LoadControl("~/Controls/BreadCrumb.ascx"), BreadCrumb)

        container.Controls.Add(b)

    End Sub

End Class

---------------
BreadCrumb.ascx
<%@ Control Language="vb" AutoEventWireup="false" CodeBehind="BreadCrumb.ascx.vb" Inherits="Website.BreadCrumb" %>

'contains a repeater control

--------------------------
BreadCrumb.ascx.vb

Public Partial Class BreadCrumb
    Inherits System.Web.UI.UserControl

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    'contains data to render the repeater

    End Sub

End Class

------------------
MasterPage contains a ContentPlaceHolder
<asp:ContentPlaceHolder ID="BreadCrumbContent" runat="server"></asp:ContentPlaceHolder>
.NET ProgrammingASP.NET

Avatar of undefined
Last Comment
miguel_j

8/22/2022 - Mon
Kumaraswamy R

Amandeep Singh Bhullar

madhevan_pillai

Try this sample you will get an idea.


Master pages (aspx)

<%@ Master Language="VB" AutoEventWireup="false" CodeBehind="Common.master.vb" Inherits="Test.Common" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title></title>
    <asp:ContentPlaceHolder ID="head" runat="server">
    </asp:ContentPlaceHolder>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
       
        </asp:ContentPlaceHolder>
    </div>
    </form>
</body>
</html>




<%@ Master Language="VB" AutoEventWireup="false" CodeBehind="Master1.master.vb" Inherits="Test.Master1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title></title>
    <asp:ContentPlaceHolder ID="head" runat="server">
    </asp:ContentPlaceHolder>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    Master1
        <asp:ContentPlaceHolder ID="Test1" runat="server">
       
        </asp:ContentPlaceHolder>
    </div>
    </form>
</body>
</html>



<%@ Master Language="VB" AutoEventWireup="false" CodeBehind="Master2.master.vb" Inherits="Test.Master2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title></title>
    <asp:ContentPlaceHolder ID="head" runat="server">
    </asp:ContentPlaceHolder>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    Master2
        <asp:ContentPlaceHolder ID="Test2" runat="server">
       
        </asp:ContentPlaceHolder>
    </div>
    </form>
</body>
</html>



Template

Public Class Template1
    Implements ITemplate

    Public Sub InstantiateIn(ByVal container As System.Web.UI.Control) Implements System.Web.UI.ITemplate.InstantiateIn
        Dim p As New Page
        container.Controls.Add(p.LoadControl("~/MasterPages/User1.ascx"))
    End Sub
End Class



Public Class Template2
    Implements ITemplate


    Public Sub InstantiateIn(ByVal container As System.Web.UI.Control) Implements System.Web.UI.ITemplate.InstantiateIn
        container.Controls.Add(container.Page.LoadControl("~/MasterPages/User2.ascx"))
    End Sub
End Class



Usercontrol

<%@ Control Language="vb" AutoEventWireup="false" CodeBehind="User1.ascx.vb" Inherits="Test.User1" %>
<asp:Label ID="Label1" runat="server" BackColor="#FF5050" Text="UserControl1"></asp:Label>



<%@ Control Language="vb" AutoEventWireup="false" CodeBehind="User2.ascx.vb" Inherits="Test.User2" %>
<asp:Label ID="Label1" runat="server" BackColor="#FF6600" Text="UserControl2"></asp:Label>




Test page aspx

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Test.aspx.vb" Inherits="Test.Test"  MasterPageFile="~/MasterPages/Common.Master"%>

 




codebehind

Public Partial Class Test
    Inherits System.Web.UI.Page
    Private Const masterid As Integer = 1
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
   

    End Sub

    Private Sub Test_PreInit(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreInit
        If masterid = 0 Then
            Me.Page.MasterPageFile = "Master1.Master"
            MyBase.AddContentTemplate("Test1", New Template1)
        Else
            Me.Page.MasterPageFile = "Master2.Master"
            MyBase.AddContentTemplate("Test2", New Template1)
        End If


    End Sub
End Class
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
ASKER CERTIFIED SOLUTION
madhevan_pillai

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
miguel_j

ASKER
thank you for your help and also thanks for the other additions experts